BFS-Knight Moves
2016-07-27 15:59
351 查看
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
Sample Output
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
3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1
Sample Output
5 28 0 code: #include <iostream> #include<algorithm> #include<string.h> #include<stdio.h> #include<queue> using namespace std; const int MAX=400; int visited[MAX][MAX]; int n; int dx[]={-1,-1,-2,-2,1,1,2,2}; int dy[]={-2,2,-1,1,-2,2,-1,1}; struct node{ int x,y,step; }; void bfs(int x0,int y0,int x1,int y1){//行列; queue<node> q; node a,next; a.x=x0; a.y=y0; a.step=0; q.push(a); visited[x0][y0]=1; while(!q.empty()){ a=q.front(); q.pop(); if(a.x==x1&&a.y==y1){ cout<<a.step<<endl; return; } for(int i=0;i<8;i++){ int xx,yy; xx=a.x+dx[i]; yy=a.y+dy[i]; if(xx<0||xx>=n||yy<0||yy>=n||visited[xx][yy]) continue; next.x=xx; next.y=yy; next.step=a.step+1; visited[xx][yy]=1; q.push(next); } } } int main(){ // freopen("input.txt","r",stdin); int t; int x0,y0,x1,y1;//起始位置(行列); cin>>t; while(t--){ cin>>n>>x0>>y0>>x1>>y1; memset(visited,0,sizeof(visited)); bfs(x0,y0,x1,y1); } return 0; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 2015年acm国内排名
- js算法: 图的两种表示方法以及广度优先算法
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833