POJ Knight Moves 2243 x
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13974 | Accepted: 7797 |
Description
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
Output
Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
Source
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring> using namespace std; char s[];
bool v[][];
int ex,ey,sx,sy,ans;
int dx[]={,,-,-,-,-,,};
int dy[]={-,-,-,-,,,,};//八个方向 struct node
{
int x,y,step;
}cur,nxt; queue<node>q; void bfs()
{
if(ex==sx&&ey==sy) //特判起点等于终点 ,找到后进行输出就一定是最小的
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",char(ex+'a'-),ey,char(sx+'a'-),sy,);
return;//格式
}
while(!q.empty()) q.pop(); // 多组数据初始化
memset(v,,sizeof(v)); // 同上
cur.x=ex,cur.y=ey; cur.step=; //起点
v[ex][ey]=true; //不要漏了标记起点
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop(); //不要漏了当前出队
//v[cur.x][cur.y]=false; 出队,清除标记,是否需要?不需要,为什么?
for(int i=;i<;i++) //八方位搜索
{
int xx=cur.x+dx[i],yy=cur.y+dy[i];
if(xx>&&xx<=&&yy>&&yy<=&&!v[xx][yy])
{
if(xx==sx&&yy==sy) //找到了,第一个找到的一定就是最近的,why?
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",char(ex+'a'-),ey,char(sx+'a'-),sy,cur.step+);
return ;
}
nxt.x=xx, nxt.y=yy; nxt.step=cur.step+;
v[nxt.x][nxt.y]=true;
q.push(nxt); //扩展出的状态入队
}
}
}
} int main()
{
while(scanf("%s",s)!=EOF) //注意输入,scanf读到空格
{
ex=s[]-'a'+; ey=s[]-'';
scanf("%s",s);
sx=s[]-'a'+; sy=s[]-'';
bfs();
}
}
POJ Knight Moves 2243 x的更多相关文章
- POJ 2243 Knight Moves(BFS)
POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
- 【POJ 2243】Knight Moves
题 Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are ...
- POJ 2243 Knight Moves
Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13222 Accepted: 7418 Des ...
- OpenJudge/Poj 1915 Knight Moves
1.链接地址: http://bailian.openjudge.cn/practice/1915 http://poj.org/problem?id=1915 2.题目: 总Time Limit: ...
- POJ 1915 Knight Moves
POJ 1915 Knight Moves Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 29 ...
- POJ 1915 Knight Moves(BFS+STL)
Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 20913 Accepted: 9702 ...
- HDU 2243 Knight Moves
题目: A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find th ...
- POJ---2243 Knight Moves 使用A*算法的广度优先搜索
题目链接:http://poj.org/problem?id=2243 启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省 ...
- poj2243 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...
随机推荐
- springboot(3) 页面到服务器
第一讲实现了spring boot 环境的下载及配置. 第二讲实现了,从服务器,到页面. 第三讲打算从页面到服务器. 比如,我们希望 从页面,点击一个按钮,传递信息到服务器. 就拿传递用户名和密码来简 ...
- linux/work
0.切换用户 //默认root用户是无固定密码的,并且是被锁定的,如果想给root设置一个密码 sudo passwd root //输入密码 & 确认密码 //切换root用户 su roo ...
- linux 通配符与正则表达式
linux通配符和三剑客(grep.awk.sed)正则表达式是不一样的 通配符一般用户命令行bash环境,而linux正则表达式用于awk.grep.sed
- [2019上海网络赛J题]Stone game
题目链接 CSLnb! 题意是求出给定集合中有多少个合法子集,合法子集的定义为,子集和>=总和-子集和$\& \&$子集和-(子集的子集和)<=总和-子集和. 其实就是很简 ...
- C++------流星雨
用C++实现模拟数字.字母流星雨,其主要用到链表.win32编程基础. demo实例: // DataRainDemo.cpp : 定义应用程序的入口点. // #include "stda ...
- java字符流读取文件
package ba; import java.io.*; public class zifuTest { public static void main(String[] args) { FileI ...
- htm、html、shtml区别。(web服务器配置ssi)
转载源:http://www.divcss5.com/html/h59.shtml 首先htm.html.shtml都是静态网页的后缀,三者也可以说都是只是扩展名不同,其他一样,都是静态的网页.Htm ...
- Hive常用非交互式命令
[hadoop@hadoop hive-0.13.1]$ bin/hive -help usage: hive -d,--define <key=value> Variable subsi ...
- Oracle 赋予指定用户dba权限
指定用户赋权限 1.链接数据库地址,使用ssh登录 2.sudo -i 3.grant dba to 用户名;
- linux Apache 日志轮询
安装日志轮询工具 cronolog [root@Nagios-Server tools]# wgethttp://cronolog.org/download/cronolog-1.6.2.tar.gz ...