BNU 26582 Gregory the Grasshopper【简单BFS】

链接:




D. Gregory the Grasshopper

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



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;	
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值