博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ 1091 (HDU 1372) Knight Moves(BFS)
阅读量:7005 次
发布时间:2019-06-28

本文共 3076 字,大约阅读时间需要 10 分钟。

Knight Moves

Time Limit: 2 Seconds      
Memory Limit: 65536 KB

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that 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.

Input Specification

The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

Output Specification

For each test case, print one line saying "To get from xx to yy takes n knight moves.".

Sample Input

e2 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 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. 题目大意:国际象棋的骑士走日。在一个8*8的棋盘中,给定两个点,求骑士从一个点到达另一个点的最少步数。 BFS题目,代码如下:
1 # include
2 # include
3 # include
4 # include
5 using namespace std; 6 bool vis[8][8]; //可以大幅度缩短搜索时间,本题不加这个也可以AC 7 int dx[8] = {
1,1,-1,-1,2,-2,2,-2}; 8 int dy[8] = {
2,-2,2,-2,1,1,-1,-1}; 9 int sx,sy,ex,ey;10 bool ismap(int x,int y){11 if(x<0 || y<0 ||x>=8 || y>=8)12 return false;13 return true;14 }15 16 struct Node{17 int x,y,step;18 }qishi;19 20 queue
q;21 22 int bfs(){23 memset(vis,0,sizeof(vis));24 qishi.x = sx; qishi.y = sy; qishi.step = 0;25 while(!q.empty()) q.pop();26 vis[sx][sy] = 1;27 q.push(qishi);28 29 while(!q.empty()){30 Node tmp = q.front(); q.pop(); 31 if(tmp.x==ex && tmp.y == ey)32 return tmp.step;33 for(int i=0;i<8;i++){34 int x = tmp.x + dx[i];35 int y = tmp.y + dy[i];36 if(vis[x][y]) continue;37 if(!ismap(x,y)) continue;38 vis[x][y] = 1;39 qishi.x = x; 40 qishi.y = y;41 qishi.step = tmp.step + 1;42 q.push(qishi);43 }44 }45 }46 47 int main(){48 char a[5],b[5];49 while(scanf("%s%s",a,b)!=EOF){50 sx = a[0]-'a'; sy = a[1]-'1';51 ex = b[0]-'a'; ey = b[1]-'1';52 printf("To get from %s to %s takes %d knight moves.\n", a, b, bfs());53 }54 return 0;55 }

 

 

转载地址:http://jujtl.baihongyu.com/

你可能感兴趣的文章
Git 常用命令
查看>>
Windows消息机制详解
查看>>
用微软makecert.exe生成一个自签名的证书
查看>>
socket实现大型文件传输
查看>>
项目 项目集 项目组合
查看>>
XPATH 带命名空间数据的读取
查看>>
MySQL逻辑架构简介
查看>>
网上找的Gif图片解析类
查看>>
node.js初学遇到的问题
查看>>
hibernate08--OpenSessionInView
查看>>
转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法...
查看>>
配置ORACLE 客户端连接到数据库
查看>>
drop asm disk、撤销drop asm disk
查看>>
Standby Redo Log 的设定原则、创建、删除、查看、归档位置
查看>>
[十二省联考2019]异或粽子
查看>>
winform 皮肤
查看>>
判断给定字符串中的大括号是否闭合
查看>>
MVC5+EF6 简易版CMS(非接口) 第二章:建数据模型
查看>>
Python 练习
查看>>
Silverlight知识点
查看>>