BNU 26582Gregory the Grasshopper 搜索水题

来源:互联网 发布:消防防毒面具品牌知乎 编辑:程序博客网 时间:2024/06/05 15:39

Gregory the Grasshopper

3000ms
65536KB
64-bit integer IO format: %lld      Java class name: Main
PrevSubmit 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, andLC. 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 ofGR,GCare the coordinates of the square that Gregory is standing on, andLR, LCare the coordinates of the square with the delicious clover leaf. (1 ≤GR, LRR; 1 ≤ GC, LCC)

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 12 2 1 1 1 28 8 1 1 1 2

Sample Output

6impossible3

Source

CTU Open Contest 2012

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

/*D题 Gregory the Grasshopperhttp://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;}


原创粉丝点击