BNU 26582 Gregory the Grasshopper【简单BFS】

链接:

http://www.bnuoj.com/bnuoj/problem_show.php?pid=26582

http://www.bnuoj.com/bnuoj/contest_show.php?cid=2318#problem/25690


D. Gregory the Grasshopper

Time Limit: 3000ms
Case Time Limit: 3000ms
Memory Limit: 65536KB
64-bit integer IO format:  %lld      Java class name:  Main
Submit  Status  PID: 26582
Font Size:  +   -

 

Gregory is a grasshopper. His favourite food are clover leafs — he can simply never have enough of them. Whenever he spots such a leaf, he wants to eat it as quickly as possible. Gregory is also lazy, so he wants to move to the leaf with minimal effort. Your task is to help him to find the shortest way to a clover leaf.

For simplicity, we will assume that Gregory lives on a rectangular grid consisting of unit squares. As a grasshopper, he prefers to move by jumping (or, more exactly, hopping) from one square to the other. Each hop takes him to a square that is in the adjacent row or column in one direction, and two columns or rows away in the other direction. So, his hops resemble the moves of a knight on a chessboard.

Input

 

 

 

The input consists of several test cases, each of them specified by six integer numbers on one line: RCGRGCLR, and LCand specify the size of the grid in unit squares, 1 ≤ R,C ≤ 100. Gregory cannot hop outside a rectangle of this size, because it would be too dangerous. The values of GR,Gare the coordinates of the square that Gregory is standing on, and LR, Lare the coordinates of the square with the delicious clover leaf. (1 ≤ GR, L≤ R; 1 ≤ GC, LC)

Output

 

For each test case, print one integer number — the minimal number of hops that Gregory needs to reach the square with his beloved delicacy. If it is not possible to reach that square at all, print the word “impossible” instead.

Sample Input

10 10 10 10 1 1
2 2 1 1 1 2
8 8 1 1 1 2

Sample Output

6
impossible
3
Submit  Status  PID: 26582



code:

/**
题意:类似于骑士周游列国
      给你棋盘大小和起点终点,
	  求到达终点的最小步数,如果不能到达则输出impossible 
算法:BFS
注意:输入时不要忘了 != EOF 。。。。贡献两次 TLE ToT 
*/ 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;

const int maxn = 110;
const int dir[8][2] = { 1,2, 2,1, 2,-1, 1,-2, -1,-2, -2,-1, -2,1, -1,2 };

struct Node{
	int step;
	int x,y;
};
int flag;
int step;

int r,c,gr,gc,lr,lc;
int vis[maxn][maxn];

void bfs()
{
	memset(vis, 0, sizeof(vis));
	vis[gr][gc] = 1;
	queue<Node> q;
	while(!q.empty()) q.pop();
	
	Node now, next;
	now.x = gr; now.y = gc; now.step = 0;
	q.push(now);
	
	while(!q.empty())
	{
		now = q.front(); q.pop();
		
		for(int i = 0; i < 8; i++)
		{
			next.x = now.x+dir[i][0];
			next.y = now.y+dir[i][1];
			
			if(next.x >= 1 && next.x <= r && next.y >= 1 && next.y <= c && !vis[next.x][next.y])
		    {
    			vis[next.x][next.y] = 1;
    			next.step = now.step+1;
    			q.push(next);
    			
    			if(next.x == lr && next.y == lc)
    			{
			    	flag = 1;
			    	step = next.step;
			    	return;
			    }
    		}
		}
	}
	return;	
}

int main()
{
	while(scanf("%d%d%d%d%d%d", &r,&c,&gr,&gc,&lr,&lc) != EOF)
	{
		flag = 0;
		step = 0;
		if(gr == lr && gc == lc) 
		{
			printf("0\n"); continue;
		}
		bfs();
		if(flag) printf("%d\n", step);
		else printf("impossible\n");
	}
	return 0;	
}


你可能感兴趣的:(BNU 26582 Gregory the Grasshopper【简单BFS】)