hdu1372Knight Moves(基本bfs)
来源:互联网 发布:js 保留两位小数 编辑:程序博客网 时间:2024/05/21 10:25
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5341 Accepted Submission(s): 3262
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 <queue>#include <cstring>using namespace std;struct node{ int x,y;}s,e,head,next;int vis[8][8];int dir[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};int bfs(){ int cnt = 0; queue<node> Q; memset(vis,0,sizeof(vis)); Q.push(s); vis[s.x][s.y]=1; int Q_size; while(!Q.empty()){ Q_size = Q.size(); cnt++; while(Q_size--){ head = Q.front(); Q.pop(); if(head.x==e.x && head.y==e.y) return cnt-1; for(int i=0; i<8; i++){ next.x = head.x+dir[i][0]; next.y = head.y+dir[i][1]; if(next.x>=0 && next.x<8 && next.y>=0 && next.y<8 && !vis[next.x][next.y]){ vis[next.x][next.y]=cnt; Q.push(next); } } } }}int main(){ char a[30],b[30]; while(cin>>a>>b){ s.x = a[1]-'0'-1; s.y = a[0]-'a'; e.x = b[1]-'0'-1; e.y = b[0]-'a'; cout<<"To get from "<<a<<" to "<<b<<" takes "<<bfs()<<" knight moves."<<endl; } return 0;}
#include <iostream>#include <cstring>using namespace std;struct node{ int x,y,cnt;}Nnode[1000],head,next;int x1,x2,y1,y2;bool vis[8][8];int dir[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};int bfs(){ memset(vis,false,sizeof(vis)); Nnode[0].x=x1; Nnode[0].y=y1; Nnode[0].cnt=0; int start=0, end=1; vis[x1][y1]=true; while(start<end){ head = Nnode[start++]; if(head.x==x2 && head.y==y2) return head.cnt; for(int i=0; i<8; i++){ int tx = head.x + dir[i][0]; int ty = head.y + dir[i][1]; if(tx<0 || tx>7 || ty<0 || ty>7 || vis[tx][ty]) continue; vis[tx][ty]=true; next.x = tx; next.y = ty; next.cnt = head.cnt+1; Nnode[end++]=next; } }}int main(){ char a[5],b[5]; while(cin>>a>>b){ x1 = a[1]-'0'-1; y1 = a[0]-'a'; x2 = b[1]-'0'-1; y2 = b[0]-'a'; cout<<"To get from "<<a<<" to "<<b<<" takes "<<bfs()<<" knight moves."<<endl; }}
0 0
- hdu1372Knight Moves(基本bfs)
- hdu1372Knight Moves bfs
- hdu1372Knight Moves
- hdu1372Knight Moves
- hdu1372Knight Moves
- 搜索专题: HDU1372Knight Moves
- hdu 1372 Knight Moves(最基本的BFS)
- Knight Moves hdu1372 bfs
- poj2243 Knight Moves (BFS)
- 【BFS】Knight Moves
- 1372 knight moves BFS
- Knight Moves(bfs)
- 1030 Knight Moves:BFS
- 439 - Knight Moves//bfs
- Knight Moves BFS
- HDU1372:Knight Moves(BFS)
- hdu1372 Knight Moves (BFS)
- Knight Moves hdu BFS
- action中的属性值会保存在Value Stack Contents中
- hdu3345War Chess(bfs,条件很多)
- sae python学习(一)
- 计算机是如何启动的?
- KVC
- hdu1372Knight Moves(基本bfs)
- 想爬得更高,需要知道的数学体系
- 微信公众平台开发(九) 数据库操作
- 深入理解Java:注解(Annotation)--注解处理器
- Mahout 定制ClusterDumper:只输出中心点
- 回调总结
- spring 监听器 IntrospectorCleanupListener简介
- “有电才‘型’2013主流智能手机耐力挑战赛” 挑战者七:联想P780
- AUPE学习第三章------文件I/O1