BNU 26582Gregory the Grasshopper 搜索水题

Gregory the Grasshopper

Time Limit: 3000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name: Main
Prev Submit Status Statistics Discuss Next
Font Size:
Type:

 

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: R, C, GR, GC, LR, and LC. R and C 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,GC are the coordinates of the square that Gregory is standing on, and LR, LC are the coordinates of the square with the delicious clover leaf. (1 ≤ GR, LR R; 1 ≤ GC, LC C)

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

Source

CTU Open Contest 2012

http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=26582

/*
D题 Gregory the Grasshopper
http://acm.bnu.edu.cn/bnuoj/contest_show.php?cid=1405#problem/15734
题意  一只青蛙可以跳到8个方向 和马走日的方向一样 输入map大小以及起点 终点
问从起点到终点所需要的最小步骤
思路: 简化版的马走日 还省去了蹩脚条件
直接BFS+队列
*/
#include<stdio.h>  
#include<string.h> 
#include<stdlib.h> 
#include<queue> 
using namespace std; 
int step[10][2]={0,0,0,0,2,-1,2,1,1,2,-1,2,-2,-1,-2,1,1,-2,-1,-2},n,m; 
int p[5][2]={0,0,1,0,0,1,-1,0,0,-1}; 
int vis[200][200],ex,ey,flag,sx,sy,mm;  
struct haha  
{  
    int x;  
    int y;  
    int step; 
    //friend bool operator <(haha a,haha b) 
    //{ 
    //    return a.step>b.step; 
    //} 
     
}q,temp;  
void BFS()  
{  
    int i,x,y,x1,y1;  
    queue<haha>que; 
    q.x=sx;q.y=sy;q.step=0; 
    vis[sx][sy]=1;
    que.push(q); 
    while(!que.empty()) 
    { 
        temp=que.front(); 
        que.pop(); 
        if(temp.x==ex&&temp.y==ey) 
        { 
            mm=temp.step;flag=1; return; 
        } 
        for(i=2;i<10;i++) 
        { 
            x=temp.x+step[i][0];  
            y=temp.y+step[i][1];  
            x1=temp.x+p[i/2][0];  
            y1=temp.y+p[i/2][1];
            if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y])  
            {  
                     vis[x][y]=1; 
                q.x=x; q.y=y; q.step=temp.step+1; 
                que.push(q); 
            } 
             
        } 
         
    }  
} 

int main()
{
  
	 while(scanf("%d %d %d %d %d %d",&n,&m,&sx,&sy,&ex,&ey)!=EOF)
	 {
		 flag=0;
		 memset(vis,0,sizeof(vis));
                 BFS();
	     if(flag) printf("%d\n",mm);
		 else printf("impossible\n");
	 }
	 return 0;
}


你可能感兴趣的:(BNU 26582Gregory the Grasshopper 搜索水题)