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.

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.

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,也过了。。。
#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;}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红米note砖头怎么办 mi4c不支持4G怎么办 小米手机抢不到怎么办 洗衣机外壳凹了怎么办 塑料手机套变黄怎么办 淘宝上没货了怎么办啊 华为手机音量低怎么办 手机电池冲鼓了怎么办 手机电池起鼓了怎么办 手机信息总提示怎么办 一体机电池不耐用怎么办 血糖偏高怎么办雅培益力佳SR 投诉电话打不通怎么办 宝宝换奶粉拉肚子怎么办 微店没有客源怎么办 itunes文件被保护怎么办 魅族e1黑屏了怎么办 窗帘打孔的扣环怎么办 棉质裤子掉色怎么办 孕期喝咖啡了怎么办 实体店买到假手机怎么办 派派提现没有那么多好友怎么办 派派提现没有支付宝怎么办 电脑电源不够用怎么办 电脑电源不够会怎么办 中行信用卡异常了怎么办 美借还款后秒拒怎么办 海尔空调关不了怎么办 华为p9无服务怎么办 贝贝买到假货怎么办 京东不予退货怎么办? 咸鱼收到货不对怎么办 闲鱼没收到货怎么办 淘宝购物不到货怎么办 百世快递不派送怎么办 圆通快递不派送怎么办 无卡存款没到账怎么办 中国银行分行冻结信用卡怎么办 银行信用卡没还怎么办 免税店提货单丢了怎么办 店长故意整我怎么办