跳马(Knight Moves), ZOJ1091, POJ2243 x
跳马(Knight Moves), ZOJ1091, POJ2243
题目描述:
给定象棋棋盘上两个位置 a 和 b,编写程序,计算马从位置 a 跳到位置 b 所需步数的最小值。
输入描述:
输入文件包含多个测试数据。每个测试数据占一行,为棋盘中的两个位置,用空格隔开。棋盘位置为两个字符组成的串,第 1 个字符为字母 a~h,代表棋盘中的列;第 2 个字符为数字字符1~8,代表棋盘中的行。
输出描述:
对输入文件中的每个测试数据,输出一行"To get from xx to yy takes n knight moves.", xx 和yy 分别为输入数据中的两个位置, n 为求得的最少步数。
|
样例输入: |
样例输出: |
|
e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6 |
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. |
这道题其实就是裸的bfs
不过需要注意的是:输入输出的形式!
代码如下:
1)注释版:
#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) //如果找到了,第一个找到的一定就是最近的
{
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 ;//必须用return,或者也可以用多次break
}
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)
{
ex=s[]-'a'+; ey=s[]-'';
scanf("%s",s);
sx=s[]-'a'+; sy=s[]-'';
bfs();
}
}
2)非注释版:(另一种方法,表示方向的换为一个二维数组)
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm> using namespace std; struct node
{
int x,y;
};
int dir[][]={-,-,-,,-,-,-,,,-,,,,-,,};
int dis[][];
int ex,ey;
bool OK(node &b)
{
if(b.x<||b.x>||b.y<||b.y>||dis[b.x][b.y])
return false;
return true;
}
void BFS(int x,int y)
{
node a,b;
queue<node> Q;
a.x=x;a.y=y;
Q.push(a);
int i;
while(!Q.empty())
{
a=Q.front();Q.pop();
for(i=;i<;i++)
{
b.x=a.x+dir[i][];
b.y=a.y+dir[i][];
if(b.x==x&&b.y==y) continue;
if(OK(b))
{
dis[b.x][b.y]=dis[a.x][a.y]+;
Q.push(b);
}
if(b.x==ex&&b.y==ey) return;
}
} }
int main()
{
char op1[],op2[];
while(scanf("%s %s",op1,op2)!=EOF)
{
memset(dis,,sizeof(dis));
int x=op1[]-'a'+,y=op1[]-'';
ex=op2[]-'a'+;ey=op2[]-'';
// printf("%d %d==\n",ex,ey);
if(x!=ex||y!=ey)
BFS(x,y);
printf("To get from %s to %s takes %d knight moves.\n",op1,op2,dis[ex][ey]); }
}
跳马(Knight Moves), ZOJ1091, POJ2243 x的更多相关文章
- poj2243 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...
- poj2243 && hdu1372 Knight Moves(BFS)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...
- POJ---2243 Knight Moves 使用A*算法的广度优先搜索
题目链接:http://poj.org/problem?id=2243 启发式搜索:启发式搜索就是在状态空间中的搜索对每一个搜索的位置进行评估,得到最好的位置,再从这个位置进行搜索直到目标.这样可以省 ...
- POJ2243 Knight Moves —— A*算法
题目链接:http://poj.org/problem?id=2243 Knight Moves Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- Knight Moves
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU 1372 Knight Moves (广搜)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- 【MM系列】SAP 关于物料间的替代问题
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 关于物料间的替代问题 前 ...
- 操作系统安全 - 提权 - Windows提权 - 汇总
CVE_2019-1388 Date: -- 影响范围: SERVER ====== Windows 2008r2 ** link OPENED AS SYSTEM ** Windows 2012r2 ...
- springcloud(一)
Spring Cloud是什么鬼? Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线. ...
- @Scheduled(cron = "0/5 * * * * *")将时间改为配置
有两种方法:第一种当然你可以把Scheduled写到xml文件中进行配置. 第二种在你的类前面添加 此处讲解第二种写法 第二种在你的类前面添加@PropertySource("classpa ...
- SpringBoot 单元测试junit test
pom引用 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...
- python 生成list的所有的子集 (不使用递归且不引入标准库)
不使用递归且不引入标准库,单纯用两个for循环即可得出一个list的所有子集 L = [1, 2, 3, 4] List = [[]] for i in range(len(L)): ...
- 搜索专题: HDU1312Red and Black
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 【LGR-063】洛谷11月月赛 I & MtOI2019 Ex Div.2 (A-C)
[MtOI2019]黑蚊子多 : 按题意模拟 #include<iostream> #include<cstdio> #include<cstring> using ...
- 05-Django-session-admin
# session- 为了应对HTTP协议的无状态性- 用来保存用户比较敏感的信息- 属于request的一个属性- 常用操作: - request.session.get(key, defaultV ...
- switch语句能否作用在byte,long,string上
switch是java中的多分支结构.在switch(expr)中,expr只能是一个整数表达式,或者是枚举常量,整数表达式可以是int基本类型也可以是Integer包装类型,由于byte,short ...