Light OJ 1377 - Blade and Sword (BFS)

1377 - Blade and Sword


Description

You may have played the game 'Blade and Sword', it's an action game. However, in this problem we are actually solving one stage of the game.

For simplicity, assume that the game stage can be modeled as a 2D grid which hasm rows andn columns. The cells are categorized as follows:

1)      '.' represents an empty space. The player can move through it.

2)      '#' represents wall, and the player cannot move through it. You can assume that the boundaries of the grid will be walls.

3)      '*' means a teleporting cell, once the player moves into this cell, he must chooseany other teleporting cell where he will be taken to. But if he cannot find a desired teleporting cell, he will die. However, after moving to the desired teleporting cell, he can either move to an adjacent cell, or he can teleport again using the same procedure.

4)      'P' means the position of the player and there will be exactly one cell containing 'P'.

5)      'D' means the destination cell and there will be exactly one cell containing'D'.

Now you are given a stage and the player starts moving. It takes one unit of time for the player to move to any adjacent cell from his current position. Two cells are adjacent if they share a side. One unit of time is needed for the teleporting service; that means taking the player from one teleporting cell to any other teleporting cell. Your task is to find the minimum possible time unit required for the player to reach the destination cell.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing two integers: m and n (3 ≤ m, n ≤ 200). Each of the next m lines contains n characters denoting the stage. The given stage follows the restrictions stated above.

Output

For each case, print the case number and the minimum required time. If it's impossible for the player to reach the destination cell, print'impossible'. See the samples for details.

Sample Input

2

4 10

##########

#.P..#*..#

#*......D#

##########

3 9

#########

#P.#..D.#

#########

Sample Output

Case 1: 6

Case 2: impossible


题意:一个图,上面可能会有细胞,如果你走上这个细胞你必须传送到其他的细胞上,不然你就会死亡,求走到目标位置的最小步数

思路:这道题当时题意读错了,以为细胞之间可以无限传送,后来才知道不是的,走上细胞后有两种选择:

1.传送到下一个细胞上,然后进行移动且以后不再进行传送

2.传送待下一个细胞上,然后再传送回来,然后以后不再进行传送

过程其实也不难实现,在步数的结构体里定义一个k记录是从什么地方来的,如果是1则说明是传送过来的,当传送过后,清空细胞数组就行了。。(PS:因为题意没读清,WA了10几次,以后要注意了)


ac代码:

#include
#include
#include
#include
#include
#include
#include
#include
#define MAXN 44444
#define LL long long
#define INF 0xfffffff
#define fab(x) (x)>0?(x):(-x)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
using namespace std;
struct s
{
	int x;
	int y;
	int step;
	int k;
};
struct ss
{
	int x;
	int y;
}cell[MAXN];
char map[210][210];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int v[2][210][210];
int bx,by,ex,ey;
int cnt,bz,n,m;
int cellx,celly;
int check(s aa)
{
	if(aa.x<0||aa.x>=n||aa.y<0||aa.y>=m||v[0][aa.x][aa.y])
	return 1;
	if(map[aa.x][aa.y]=='#')
	return 1;
	return 0;
}
void bfs(int xx,int yy)
{
	s a,b;
	int i,j;
	queueq;
	a.x=xx;a.y=yy;a.step=0;a.k=0;
	mem(v);
	v[0][xx][yy]=1;
	q.push(a);
	while(!q.empty())
	{
		a=q.front();
		q.pop();
		if(a.x==ex&&a.y==ey)
		{
			bz=1;
			printf("%d\n",a.step);
			return;
		}
		b.step=a.step+1;
		if(map[a.x][a.y]=='*')
		{
			int flag=0;
			for(i=0;i


 
    

你可能感兴趣的:(搜索)