跳马(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 ...
随机推荐
- debian下安装mysql 5.1.34
#cd /usr/local/src # tar xvzf mysql-5.1.34.tar.gz # cd mysql-5.5.1.34 配置和编译 #chmod +x configure # ./ ...
- python 并发编程 查看进程的id pid与父进程id ppid
查看进程id pid 不需要传参数 from multiprocessing import Process import time import os def task(): print(" ...
- 最小配置启动SQL SERVER,更改SQL Server最大内存大小导致不能启动的解决方法
如果存在配置问题而无法启动服务器,则可以使用最小配置启动选项来启动 Microsoft SQL Server 实例. 这就是启动选项 -f. 使用最小配置启动 SQL Server 实例会自动将服务器 ...
- Spring中的常见注解
@Controller 标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象. @RestController Spring4之后加入的注解,原来在@Co ...
- Windows 下安装 ElasticSearch 修改 elasticsearch.yml的坑
注意: ElasticSerach 集成 IK分词器 的时候,整个路径不能有空格!!! 1. 文件后加入 严格复制粘贴,否则入坑 http.cors.enabled : true http.cors ...
- 安装CentOS7虚拟机
基础环境 Windows 10 VMware Workstation 1.下载CentOS7镜像 https://www.centos.org/download/ 此次安装使用的版本为: CentOS ...
- P1536村村通
这是一个并查集的题,被洛谷评为提高—. 拿到这个题便看出了这是一个裸的并查集,于是就写了一个模板,结果发现连输入都输不进去,一看竟然是多组数据,,然后看到N==0结束,于是便加了一层while.之后提 ...
- Linux——临界段,信号量,互斥锁,自旋锁,原子操作
一. linux为什么需要临界段,信号量,互斥锁,自旋锁,原子操作? 1.1. linux内核后期版本是支持多核CPU以及抢占式调度.这里就存在一个并发,竞争状态(简称竟态). 1.2. 竞态条件 发 ...
- js获取url中的参数(解决中文乱码)
这个是封装好的方法: function getQueryString(name) { var reg = new RegExp("(^|&)" + name + " ...
- 6-3 如何解析简单的XML文档
元素节点.元素树 >>> from xml.etree.ElementTree import parse >>> help(parse) Help on funct ...