BFS_1
来源:互联网 发布:天天生鲜项目源码 编辑:程序博客网 时间:2024/06/05 09:09
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
题目意思是国际象棋中的马以“日”字走法从起点走到终点,输出最短步数。这显然是bfs解决,只要在结构体里面添加一个计数变量就可以
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int Maxn = 600;struct point{ int x; int y; int dis;};int a[Maxn][Maxn];//定义标记数组/*8个前进方向*/int X[8]={-2,-2,-1,-1,2,2,1,1};int Y[8]={-1,1,-2,2,-1,1,-2,2};int n;queue<point>que;point start,end;// 定义起点和终点int bfs(){ start.dis=0; // 注意: start.dis 要提前赋值为 0 que.push(start); a[start.x][start.y]=1; // 标记此点已被访问 point temp,next; while(1) { temp=que.front();//访问队首元素,即最早被压入队列的元素。 que.pop(); //出队, 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。 int k; for(k=0;k<8;k++) { next.x=temp.x+X[k]; next.y=temp.y+Y[k]; if(next.x>=0&&next.y>=0&&next.x<n&&next.y<n&&a[next.x][next.y]==0) //判断能否入队 { next.dis=temp.dis+1; if(next.x == end.x && next.y == end.y) return next.dis; que.push(next); //入队,将next接到队列的末端。 a[next.x][next.y]=1; } } }}int main(){ int t; scanf("%d",&t); while (t--) { scanf("%d",&n); memset(a,0,sizeof(a)); scanf("%d%d",&start.x,&start.y); scanf("%d%d",&end.x,&end.y); if(start.x == end.x && start.y == end.y) { printf("0\n"); continue; } while(!que.empty())//判断队列空,如例:q.empty(),当队列空时,返回true。 que.pop(); printf("%d\n",bfs()); } return 0;}
0 0
- BFS_1
- 怎样利用控制台cmd生成后缀为csv的文件
- HDU 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(多重背包模版题)
- POST请求demo
- android通过Jni加载so库遇到UnsatisfiedLinkError问题!!!
- 人工智能动态整理
- BFS_1
- C++STL之set容器
- 文本复制后自动实现跳转的js代码
- strncpy函数学习
- ByteBuffer的Put和Get的用法和要注意的细节
- block(块级元素)和 inline(内联元素) 的区别
- TestFlight,app正式上线之前的测试环节。
- java中的try-catch-finally的处理流程介绍
- ACM模板——最长公共子序列 LCS