poj2243 && hdu1372 Knight Moves(BFS)
转载请注明出处: viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents
题目链接:
POJ:http://poj.org/problem?id=2243
HDU: pid=1372">http://acm.hdu.edu.cn/showproblem.php? pid=1372
the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
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.
the row on the chessboard.
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.
题意:用象棋中跳马的走法。从起点到目标点的最小步数;
代码例如以下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
#define M 1017
struct node
{
int x, y;
int step;
};
int xx[8] = {1,2,2,1,-1,-2,-2,-1};
int yy[8] = {2,1,-1,-2,-2,-1,1,2};
bool vis[M][M];
int n, ansx, ansy;
queue<node>q;
int BFS(int x, int y)
{ if(x == ansx && y == ansy)
return 0;
int dx, dy, i;
node front, rear;
front.x = x, front.y = y, front.step = 0;
q.push(front);
vis[x][y] = true;
while(!q.empty())
{
front = q.front();
q.pop();
for(i = 0; i < 8; i++)
{
dx = front.x+xx[i];
dy = front.y+yy[i];
if(dx>=1&&dx<=8&&dy>=1&&dy<=8&&!vis[dx][dy])
{
vis[dx][dy] = true;
if(dx == ansx && dy == ansy)
{
return front.step+1;
}
rear.x = dx, rear.y = dy, rear.step = front.step+1;
q.push(rear);
}
}
} }
int main()
{
char s,e;
int a1,a2;
while(~scanf("%c%d %c%d",&s,&a1,&e,&a2))
{
getchar();
while(!q.empty())
q.pop();
memset(vis,0,sizeof(vis));
int s1 = s-'a'+1;
int e1 = e-'a'+1;
ansx = e1, ansy = a2;
int ans = BFS(s1,a1);
printf("To get from %c%d to %c%d takes %d knight moves.\n",s,a1,e,a2,ans);
}
return 0;
}
poj2243 && hdu1372 Knight Moves(BFS)的更多相关文章
- HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏
Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...
- poj2243 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...
- HDU-1372 Knight Moves (BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- ZOJ 1091 (HDU 1372) Knight Moves(BFS)
Knight Moves Time Limit: 2 Seconds Memory Limit: 65536 KB A friend of you is doing research on ...
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- ZOJ 1091 Knight Moves(BFS)
Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where you are t ...
- poj1915 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=1915 题意 输入正方形棋盘的边长.起点和终点的位置,给定棋子的走法,输出最少经过多少步可以从起点走到终点. 思路 经典bfs题目. 代 ...
- HDU1372:Knight Moves(经典BFS题)
HDU1372:Knight Moves(BFS) Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
随机推荐
- React ----- 路由懒加载的几种实现方案
传统的两种方式 1.import() 符合ECMAScript提议的import()语法,该提案与普通 import 语句或 require 函数的类似,但返回一个 Promise 对象.这意味着模块 ...
- MetaSploit攻击实例讲解------Metasploit自动化攻击(包括kali linux 2016.2(rolling) 和 BT5)
不多说,直接上干货! 前期博客 Kali linux 2016.2(Rolling)里Metasploit连接(包括默认和自定义)的PostgreSQL数据库 Kali linux 2016.2(Ro ...
- Android-加载大图避免OOM
高效加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有不同的形状.不同的大小,但在大多数情况下,这些图片都会大于我们程序所需要的大小.比如说系统图片库里展示的图片大都是 ...
- 联想服务器thinkserver TS550 Raid5制作及winserver2012R2 安装过来
一. 联想服务器thinkserver TS550 Raid5制作 1.开机后按ctrl+i 进入raid配置模式 2.选择“1”配置所需Raid模式(这次配的是raid5) 3.按提示确认后退出 ...
- 洛谷P5082 成绩
原来的空间限制是5MB,其实是很足够的,现在调成128MB,变成了没有思维难度的大水题. 不过,我还是想说一下空间限制为5MB的解题思路. 题目要求的是(每一科的满分之和*3-每一科的实际得分之和*2 ...
- 7.包含(ng-Include)
转自:https://www.cnblogs.com/best/tag/Angular/ 获取.编译并引用一个外部HTML片段(也可以是内部的) 默认情况下,模板URL被强制为使用与应用文档相同的域名 ...
- BZOJ 3196 线段树套平衡树
(代码无比丑陋) //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int ...
- HDU 5889 Barricade (Dijkstra+Dinic)
思路: 首先 先Dijkstra一遍 找出来最短路 不是最短路上的边都不要 然后呢 套个Dinic模板就好了-- 求个最小割 输出 大功告成~~ //By SiriusRen #include < ...
- Android 使用Retrofit获取JSON数据
在大家使用网络请求的时候,往往会出现一种情况:需要拿到服务器返回来的JSON字符串,而Retrofit会默认将Json解析,而又没有直接暴露出拿到Json字符串的方法: 今天测接口的时候,发现当数据正 ...
- 定时器函数SetTimer
原文链接:http://www.cnblogs.com/zhangpengshou/archive/2009/04/05/1429770.html 一.SetTimer表示的是定义个定时器.根据定义指 ...