poj1915 Knight Move
来源:互联网 发布:淘宝卖的玛咖是真是假 编辑:程序博客网 时间:2024/05/20 18:51
Knight Moves
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 22762 Accepted: 10636
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
题解:
给定起始和终止位置,求最小步数,显然BFS。为了节省时间要选择双向BFS。每次选择节点少的开始扩展。
程序代码:
<span style="font-size:18px;">#include<iostream>#include<cstdio>#include<cstring>#define MAXN 100000using namespace std;struct node{int x,y;}q[2][MAXN];int num,ans,n,l[2],r[2],dis[2][305][305];bool v[2][305][305];int epdx[8]={1,2,2,1,-1,-2,-2,-1},epdy[8]={2,1,-1,-2,-2,-1,1,2};int expand(int k){int i,j,x,y,d,tmpx,tmpy;x=q[k][l[k]].x;y=q[k][l[k]].y;d=dis[k][x][y];for (i=0; i<8; i++){tmpx=x+epdx[i];tmpy=y+epdy[i];if (tmpx>=0&&tmpx<n&&tmpy>=0&&tmpy<n&&!v[k][tmpx][tmpy]){v[k][tmpx][tmpy]=true;r[k]++;q[k][r[k]].x=tmpx;q[k][r[k]].y=tmpy;dis[k][tmpx][tmpy]=d+1;if (v[1-k][tmpx][tmpy]){ans=dis[k][tmpx][tmpy]+dis[1-k][tmpx][tmpy];return 1;}}}return 0;}void bfs(){if (q[0][1].x==q[1][1].x&&q[0][1].y==q[1][1].y) return;v[0][q[0][1].x][q[0][1].y]=true;v[1][q[1][1].x][q[1][1].y]=true;l[0]=r[0]=l[1]=r[1]=1;while (l[0]<=r[0]&&l[1]<=r[1]){if (r[0]-l[0]<r[1]-l[1]) {if(expand(0)) return; l[0]++;}else {if(expand(1)) return; l[1]++;}}}int main(){cin>>num;for (int i=1; i<=num; i++){memset(v,false,sizeof(v));memset(q,0,sizeof(q));memset(dis,0,sizeof(dis));ans=0;cin>>n;cin>>q[0][1].x>>q[0][1].y;cin>>q[1][1].x>>q[1][1].y;bfs();cout<<ans<<endl;}return 0;}//Written by aap</span>
0 0
- poj1915 Knight Move
- poj1915 & poj2243 Knight Moves
- poj1915 Knight Moves
- poj1915 - Knight Moves
- POJ1915:Knight Moves(BFS)
- poj1915 Knight Moves
- Knight Moves(poj1915)
- POJ1915---Knight Moves
- POJ1915 (Knight Moves) BFS
- POJ1915 Knight Moves
- POJ1915 Knight Moves
- POJ1915--Knight's Moves
- [POJ1915] Knight Moves
- Knight move
- poj1915——Knight Moves
- POJ1915 Knight Moves 经典BFS
- poj1915 Knight Moves(bfs)
- poj1915 Knight Moves(bfs)
- mount 命令挂载共享到/mnt
- 黑马程序员_JavaSE学习总结第01天_Java概述
- Linux汇编教程01: 基本知识
- Underscore.js
- 利用代码修改layout_marginBottom的属性
- poj1915 Knight Move
- Images.xcassets真是太好用了
- dll注入调用函数改变指针内容
- 和Keyle一起学ShaderForge - Overview
- linux文件链接
- 深入OpenVPN的配置
- 中层管理者在沟通中最忌讳什么?
- 使用js进行post请求
- (二十九)java条件控制语句培训笔记