题目地址:1936. Knight Moves

思路:

这道题一开始不理解题意…orz...囧,看大神们理解的。

题意是说一个8*8的国际象棋,骑士以马的形式走动(“日”字型),指定两个点,输出最小的步骤。

可以利用广度搜索解决。

具体代码如下:

 #include <iostream>
#include <queue>
#include <cstring>
#include <string>
using namespace std; int dx[] = {-, -, -, -, , , , }; //可以走八个方向
int dy[] = {-, -, , , , , -, -}; bool visited[]; int main() {
int t;
cin >> t;
while (t--) {
memset(visited, false, sizeof(visited));
int distance[] = {}; string node1, node2;
cin >> node1 >> node2; int X = (node1[]-'a')* + node1[]-'';
int Y = (node2[]-'a')* + node2[]-''; queue<int> store;
store.push(X);
while (!store.empty()) {
if (store.front() == Y)
break; int x = store.front()/;
int y = store.front()%; for (int i = ; i < ; i++) {
int nx = x+dx[i];
int ny = y+dy[i]; if (nx < ||nx > ||ny < ||ny > )
continue;
int temp = nx* + ny; if (!visited[temp]) {
store.push(temp);
visited[temp] = true;
distance[temp] = distance[store.front()] + ;
}
}
store.pop();
}
cout << "To get from " << node1
<< " to " << node2 << " takes "
<< distance[Y] << " knight moves.\n";
} return ;
}

Sicily 1936. Knight Moves的更多相关文章

  1. Knight Moves

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  2. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  3. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  4. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  5. UVA 439 Knight Moves --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

  6. 【POJ 2243】Knight Moves

    题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...

  7. hdu Knight Moves

    这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...

  8. HDU 1372 (搜索方向稍有改变) Knight Moves

    其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...

  9. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

随机推荐

  1. XBox360自制系统的更新(Update)

    升级和更新 升级(Upgrade):从Windows XP到Windows 10,这叫升级,不叫更新.XBox360升级失败的话,后果可能会比较严重,直接就无法开机了. 更新(Update):在Win ...

  2. freemarker对数字的处理

    freemark会默认对数字进行格式化处理,例如price = 12000,  通过${price}显示为12,000,  但其实有些场景会有问题: 比如编辑一条记录, 再保存,容易将12,000传到 ...

  3. tomcat : Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException:

    错误 严重: Error configuring application listener of class org.springframework.web.context.ContextLoader ...

  4. Java多线程编程(一)

    1.Java创建多线程的方法一:(1)实现Runnable接口并实现其中的run()方法:(2)将Runable对象提交给一个Thread构造器,调用start()方法. [程序实例]单线程 publ ...

  5. XML (DOM解析) 看看就行

    000000000000000000000000000000000000000 ------------------------------------------------------------ ...

  6. 安装redis,含安装步骤和安装中出现的详细错误分析

    1.wget http://download.redis.io/releases/redis-2.8.13.tar.gz 2.解压文件 tar -zxvf redis-2.8.13.tar.gz 3. ...

  7. UITouch附加

    框架 /System/Library/Frameworks/SpriteKit.framework 可用性 可用于iOS 7.0或者更晚的版本 声明于 SKNode.h 概览 重要提示:这是一个初步的 ...

  8. Mysql大小写敏感的问题 --转

    一.1 CREATE TABLE NAME(name VARCHAR(10)); 对这个表,缺省情况下,下面两个查询的结果是一样的: SELECT * FROM TABLE NAME WHERE na ...

  9. linux下查看文件系统类型

    1. df -hT命令   -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G) -T, --pr ...

  10. 2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】

    2015 UESTC Winter Training #8 The 2011 Rocky Mountain Regional Contest Regionals 2011 >> North ...