BFS例题
来源:互联网 发布:php storm10使用教程 编辑:程序博客网 时间:2024/05/19 01:29
Knight Moves
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 32 Accepted Submission(s) : 26
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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.
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
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
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.
题意:只需要知道跟象棋中的马走日一样
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <queue>using namespace std;int dir[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};int map[8][8];char s1[10],s2[10];struct node{ int x,y,m;};int x,y;bool check(int x,int y){ if(x<0||y<0||x>=8||y>=8||map[x][y]) return false; else return true;}int bfs(){ node q,p,next; queue<node>Q; p.x=s1[0]-'a'; p.y=s1[1]-'1'; p.m=0; Q.push(p); memset(map,0,sizeof(map)); x=s2[0]-'a'; y=s2[1]-'1'; map[p.x][p.y]=0; while(!Q.empty()) { q=Q.front(); Q.pop(); if(q.x==x&&q.y==y) return q.m; for(int i=0;i<8;i++) { next.x=q.x+dir[i][0]; next.y=q.y+dir[i][1]; if(next.x==x&&next.y==y) return q.m+1; if(!check(next.x,next.y)) continue; next.m=q.m+1; map[next.x][next.y]=1; Q.push(next); } } return 0;}int main(){ while(~scanf("%s%s",s1,s2)) { printf("To get from %s to %s takes %d knight moves.\n",s1,s2,bfs()); } return 0;}
1 0
- BFS例题
- BFS和队列例题
- BFS例题:A计划
- BFS例题详解
- BFS的基本例题
- bfs例题:血色先锋军
- BFS经典例题Isenbaev number
- BFS经典例题:Maze迷宫
- BFS经典例题合集
- POJ 2251 Dungeon Master (BFS例题)
- BFS经典例题—迷宫问题
- 训练第二周之BFS例题
- 紫书 例题7-8 Uva 10603 BFS
- Poj-3669 Meteor Shower--Bfs经典例题
- 紫书上的例题,关于BFS
- BFS例题 ZOJ——1091 Knight Moves
- 例题6-20 UVa1599 Ideal Path(两次BFS)
- [C++]广度优先搜索(BFS)(附例题)
- 1492: 求和
- UART中的硬件流控RTS与CTS
- ZOJ 1003
- Mysql数据库优化系列(二)------AWK脚本统计数据库性能参数
- 水题
- BFS例题
- 彻底解决 jspatch app store 审核通不过的问题
- linux驱动开发之字符设备框架
- Ajax请求Servlet返回文本 json html和XML数据并解析xml及responseText和responseXML的区别
- Javascript的数据类型
- [BZOJ4545]DQS的trie(广义后缀自动机+lct)
- javaWeb测试
- 深入理解JS—闭包
- 数据访问层