【POJ 2243】Knight Moves
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.
题意:骑士走法是日字型,或说L型,给出起点和终点,求起点到终点的最少步数
分析:广搜(bfs)
#include<stdio.h>
#include<string.h>
int vis[][];
int fx[]= {,,-,-,,,-,-},
fy[]= {,-,,-,,-,,-};
struct node
{
int x,y,step;
bool operator == (const node b)
{
return x==b.x&&y==b.y;
}
bool ok()
{
return x<&&x>=&&y<&&y>=&&vis[x][y]==;
}
} fr,en,que[];
int bfs()
{
if(fr==en)return ;
int coun=,top=;
memset(que,,sizeof(que));
memset(vis,,sizeof(vis));
que[++coun]=fr;
while(top<=coun)
{
node d=que[top];
top++;
node p;
for(int i=; i<; i++)
{
p.x=d.x+fx[i];
p.y=d.y+fy[i];
p.step=d.step+;
if(p.ok())
{
if(p==en)return p.step;
vis[d.x][d.y]=;
que[++coun]=p;
}
}
}
}
int main()
{
int fry,eny;
char frx,enx;
while(~scanf("%c%d %c%d",&frx,&fry,&enx,&eny))
{
fr.x=frx-'a';
fr.y=fry-;
en.x=enx-'a';
en.y=eny-;
printf("To get from %c%d to %c%d takes %d knight moves.\n",frx,fry,enx,eny,bfs());
getchar();
}
return ;
}
【POJ 2243】Knight Moves的更多相关文章
- 1450:【例 3】Knight Moves
1450:[例 3]Knight Moves 题解 这道题可以用双向宽度搜索优化(总介绍在 BFS ) 给定了起始状态和结束状态,求最少步数,显然是用BFS,为了节省时间,选择双向BFS. 双向B ...
- 【广搜】Knight Moves
题目描述 Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights fr ...
- 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)
[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS Memory Limit: 65536K Total Su ...
- 【POJ 2195】 Going Home(KM算法求最小权匹配)
[POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
随机推荐
- 纯js和纯css+html制作的手风琴的效果
一:纯css+html的手风琴效果 这种用css写的手风琴比较简单,主要是应用到css中的,transition属性. 代码如下: <!DOCTYPE HTML> <html> ...
- MySQL数据库学习笔记(二)----MySQL数据类型
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Unity Sample Bootcamp
M4枪 射击特效 Gun.js源码 function GenerateGraphicStuff(hit : RaycastHit) { var hitType : HitType; var body ...
- TestLink学习六:TestLink1.9.13工作使用小结
Testlink是一款强大的用例追踪和管理工具.测试管理注重的实际上就是一个流程. 1.默认当测试用例同名时,就会有提示.(以前版本需要修改配置) 2.测试用例序号:(缺点) 1)删除一个测试用例之后 ...
- java 12-4 StringBuffer类的替换、反转、截取功能
1.StringBuffer的替换功能: public StringBuffer replace(int start,int end,String str):从start开始到end用str替换 pu ...
- Android service ( 二) 远程服务
通常每个应用程序都在它自己的进程内运行,但有时需要在进程间传递对象,你可以通过应用程序UI的方式写个运行在一个不同的进程中的service.在android平台中,一个进程通常不能访问其他进程中的内存 ...
- python算法:rangeBitwiseAnd(连续整数的与)
def rangeBitwiseAnd(self, m, n): i = 0 while m != n: m >>= 1 n >>= 1 i += 1 return n < ...
- Linux Linux共享库
so文件在linux中为共享库,与windows下的dll类似. so文件中的函数可供多个进程调用,最大可能的提供二进制代码复用. 共享库可以使代码的维护工作大大简化,当修正了一些错误或者添加了新特性 ...
- listview的头布局把我的ACTION_DOWN事件给吃了.....
因为头布局的viewpager自己处理点击事件 public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) ...
- Spring系列: 使用aop报错:nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$Refle
写了个最简单的aop例子 配置文件如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...