BNU 26582Gregory the Grasshopper 搜索水题

本文介绍了一个寻找最短路径的问题,通过BFS算法帮助一只名为格雷戈里的跳蚤找到它最爱的苜蓿叶。跳蚤只能以特定的方式跳跃,类似于国际象棋中的马走日。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gregory the Grasshopper

3000ms
65536KB
64-bit integer IO format: %lld      Java class name: Main
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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值