Knight moves
| Time limit:1000 ms |
|
Memory limit:30000 K |
| Total submissions:21919 |
|
Accepted:10223 |
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
A very watery BFS search question has a very simple idea. It is similar to the catch that cow thought made yesterday. It is a very watery question. According to your own ideas, the code can be knocked out at once, and then tested and used. In the example, the second group of data runs at 17. After checking for a long time, I still did not find the error, then I re-typed the code again. I tested the data again. The sample passed. I submitted the code again as Wa. I checked the data several times and did not find the error, later, I read other people's code. I suspect that my array is too small, my queue array is only open to 1000, and the visit array uses 1000, changed the queue array to 100000 and finally ac... At the beginning, I thought it was a wrong idea, but the idea was very clear. I actually made a mistake in the array. No !!! You can't commit any more crimes in the future. It's worth noting !!
The main idea of this question is to search in eight directions. When it is searched, it will be output, and then join the team if it is not searched.
Simulating Queues with arrays is more efficient than using STL;
The following is the AC code:
# Include <cstdio> # include <cstring> const int maxn = 100000; // 1000 is set up at the beginning, resulting in a long history of WA Ah typedef struct queue {int X, Y, count ;} queue; queue que [maxn]; // use an array to simulate the bool visit [310] [310]; int dir [8] [2] = }, {-2,-1}, {-}, {-1,-2}, {}, {1,-2}, {}, {2, -1 }}; // direction array int L, c, d; void BFS (int x, int y) {memset (visit, false, sizeof (visit )); // initialize int front = 0, rear = 0; int FX, fy, I; que [rear]. X = x; // The que [rear]. y = y; que [rear ++]. count = 0; visit [x] [Y] = true; while (front <rear) // The team is not empty {queue q = que [Front ++]; If (Q. X = C & Q. y = d) // search result {printf ("% d \ n", Q. count); break;} for (I = 0; I <8; I ++) {FX = Q. X + dir [I] [0]; FY = Q. Y + dir [I] [1]; If (FX> = 0 & FY> = 0 & FX <L & FY <L &&! Visit [FX] [FY]) // restrictions. Note that the {visit [FX] [FY] = true of WA here; // mark the access que [rear]. X = FX; // The que [rear]. y = FY; que [rear ++]. count = Q. count + 1 ;}}} int main () {int t, a, B; scanf ("% d", & T); While (t --) {memset (que, 0, sizeof (que); scanf ("% d", & L); scanf ("% d", & A, & B ); scanf ("% d", & C, & D); BFS (a, B);} return 0 ;}
The idea is relatively simple. below is my wa code. The second group of data is not passed, and I cannot find the error...
#include <cstdio>#include <cstring>typedef struct Queue{ int x,y; int count;}Queue;const int maxn=1000;int dir[8][2]={{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};bool visit[maxn][maxn];Queue queue[maxn];int c,d,l;void bfs(int x,int y){ int front=0,rear=0; int i,fx,fy; queue[rear].x=x; queue[rear].y=y; queue[rear++].count=0; visit[x][y]=1; while(front<rear) { Queue q=queue[front++]; if(q.x==c && q.y==d) { printf("%d\n",q.count); break; } for(i=0;i<8;i++) { fx=q.x+dir[i][0]; fy=q.y+dir[i][1]; if( fx>= 0 && fy>= 0 && fx <l && fy <l&& !visit[fx][fy]) { visit[fx][fy]=1; queue[rear].x=fx; queue[rear].y=fy; queue[rear++].count=q.count+1; } } }}int main(){ int t; int a,b; scanf("%d",&t); while(t--) { memset(queue,0,sizeof(queue)); //memset(visit,0,sizeof(visit)); scanf("%d",&l); scanf("%d%d",&a,&b); scanf("%d%d",&c,&d); bfs(a,b); } return 0;}