POJ 1915 Knight Moves
来源:互联网 发布:程序员可以干到多少岁 编辑:程序博客网 时间:2024/05/19 04:56
Knight Moves
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 18909 Accepted: 8665
Description
Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.
Input
The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.
Output
For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.
Sample Input
380 07 01000 030 50101 11 1
Sample Output
5280
Source
TUD Programming Contest 2001, Darmstadt, Germany
解题思路:经典的骑士游历问题,很多人双广搜过的,不过我的单广搜跑了300+ms,也过了。。。
解题思路:经典的骑士游历问题,很多人双广搜过的,不过我的单广搜跑了300+ms,也过了。。。
#include<iostream>#include<queue>using namespace std;int n,xa,xb,ya,yb;long long depth[1000000],Count,s;bool visit[305][305];int bfs()//s用来记录第i步有多少种走法,并记录到depth数组中,Count记录在可走的s步中已经走了多少步//全部走完的话,s和Count清空为0重新计数,步数i+1{int a,b,a1,a2,a3,a4,a5,a6,a7,a8,b1,b2,b3,b4,b5,b6,b7,b8,i=1;queue<int> x;queue<int> y;x.push(xa);y.push(ya);while(!x.empty()&&!y.empty()){if(Count==depth[i-1]){i++;Count=s=0;}a=x.front();b=y.front();x.pop();y.pop();if(a==xb&&b==yb)break;a1=a-2;b1=b-1;if(a1>=0&&a1<n&&b1>=0&&b1<n&&!visit[a1][b1]){visit[a1][b1]=true;x.push(a1);y.push(b1);s++;}a2=a-1;b2=b-2;if(a2>=0&&a2<n&&b2>=0&&b2<n&&!visit[a2][b2]){visit[a2][b2]=true;x.push(a2);y.push(b2);s++;}a3=a+1;b3=b-2;if(a3>=0&&a3<n&&b3>=0&&b3<n&&!visit[a3][b3]){visit[a3][b3]=true;x.push(a3);y.push(b3);s++;}a4=a+2;b4=b-1;if(a4>=0&&a4<n&&b4>=0&&b4<n&&!visit[a4][b4]){visit[a4][b4]=true;x.push(a4);y.push(b4);s++;}a5=a+2;b5=b+1;if(a5>=0&&a5<n&&b5>=0&&b5<n&&!visit[a5][b5]){visit[a5][b5]=true;x.push(a5);y.push(b5);s++;}a6=a+1;b6=b+2;if(a6>=0&&a6<n&&b6>=0&&b6<n&&!visit[a6][b6]){visit[a6][b6]=true;x.push(a6);y.push(b6);s++;}a7=a-1;b7=b+2;if(a7>=0&&a7<n&&b7>=0&&b7<n&&!visit[a7][b7]){visit[a7][b7]=true;x.push(a7);y.push(b7);s++;}a8=a-2;b8=b+1;if(a1>=0&&a1<n&&b1>=0&&b1<n&&!visit[a8][b8]){visit[a8][b8]=true;x.push(a8);y.push(b8);s++;}depth[i]=s;Count++;}return i-1;}int main(){int t,ans;cin>>t;while(t--){cin>>n>>xa>>ya>>xb>>yb;memset(visit,false,sizeof(visit));memset(depth,0,sizeof(depth));visit[xa][ya]=true;depth[0]=1;Count=s=0;ans=bfs();cout<<ans<<endl;}return 0;}
- poj 1915 Knight Moves
- poj 1915 Knight Moves
- poj 1915 Knight Moves
- POJ 1915 Knight Moves
- poj 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- poj 1915 Knight Moves
- poj 1915 Knight Moves
- poj 1915Knight moves
- POJ 1915 Knight Moves
- POJ 1915Knight Moves
- POJ 1915: Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- POJ 1915 Knight Moves
- android webview js交互 第一节 (java和js交互)
- TCO round 2A level3 EllysReversals
- linux文件目录简介
- C++中#define的用法
- java设计模式之简单工厂模式
- POJ 1915 Knight Moves
- java synchronized详解
- latex中\input和\include的区别
- Global.asax 的用途原理
- JAVAC 命令详解 -d表示目录
- 图片旋转播放效果
- const 对象默认为文件的局部变量
- vmware虚拟机安装---心得之四虚拟机的工作模式
- OGRE自定义渲染循环实现