ACM--steps--4.2.4--Knight Moves
来源:互联网 发布:任逍遥 贾樟柯 知乎 编辑:程序博客网 时间:2024/05/21 14:09
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 461 Accepted Submission(s): 331Problem 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.
Source
University of Ulm Local Contest 1996
Recommend
Eddy
#include<iostream>#include<cstring>#include<queue>using namespace std;//构建结构体struct dyx{ int x,y,step;};int vis[10][10];//是否访问过的状态char map1[5],map2[5];//用作输入int point_x[8]={-1,-2,-2,-1,1,2,2,1};int point_y[8]={2,1,-1,-2,-2,-1,1,2};int sx,sy,ex,ey;int wyx(int x,int y){ if(x<0||y<0||x>=8||y>=8||vis[x][y]) return 1; return 0;}int bfs(){ queue<dyx> que; int i; memset(vis,0,sizeof(vis)); dyx now,next; sx=now.x=map1[0]-'a'; sy=now.y=map1[1]-'1'; now.step=0; ex=map2[0]-'a'; ey=map2[1]-'1'; vis[sx][sy]=1; que.push(now); while(!que.empty()) { now=que.front(); que.pop(); if(now.x==ex&&now.y==ey) return now.step; for(i=0;i<8;i++) { next=now; next.x+=point_x[i]; next.y+=point_y[i]; //if(next.x==ex&&next.y==ey) //return next.step+1; if(wyx(next.x,next.y)) continue; next.step++; vis[next.x][next.y]=1; que.push(next); } } return 0;}int main(){ while(cin>>map1>>map2) { cout<<"To get from "<<map1<<" to "<<map2<<" takes "<<bfs()<<" knight moves."<<endl; } return 0;}
0 0
- ACM--steps--4.2.4--Knight Moves
- acm 2 1015 Knight Moves
- ACM 搜索 hdu 1372 Knight Moves
- ACM 广搜优先 Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- 分治法——最大子数组
- Android主线程向子线程发送消息
- shell和命令行参数有关的变量
- 阅读论文
- 《不要一个人吃饭( Never Eat Alone)》----交际规则
- ACM--steps--4.2.4--Knight Moves
- spring的配置文件引用properties配置的详解
- MySQL中emoji表情过滤
- html表单与输入,图像,head,meta
- spring学习(五)ClassPathXmlApplicationContext的getBean获取实例bean
- 关于权限的数据库设计
- OpenCV.2.Computer.Vision.Application.Programming.Cookbook--Efficient scanning of continuous images
- 阅读论文2
- 【java】容器