zoj 3865 Superbot (广搜)

Superbot is an interesting game which you need to control the robot on an N*M grid map.


As you see, it's just a simple game: there is a control panel with four direction left (1st position), right (2nd), up (3rd) and down (4th). For each second, you can do exact one of the following operations:

  • Move the cursor to left or right for one position. If the cursor is on the 1st position and moves to left, it will move to 4th position; vice versa.
  • Press the button. It will make the robot move in the specific direction.
  • Drink a cup of hot coffee and relax. (Do nothing)

However, it's too easy to play. So there is a little trick: Every P seconds the panel will rotate its buttons right. More specifically, the 1st position moves to the 2nd position; the 2nd moves to 3rd; 3rd moves to 4th and 4th moves to 1st. The rotating starts at the beginning of the second.

Please calculate the minimum time that the robot can get the diamond on the map.

At the beginning, the buttons on the panel are "left", "right", "up", "down" respectively from left to right as the picture above, and the cursor is pointing to "left".

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains three integers N, M (2 <= N, M <= 10) and P (1 <= P <= 50), which represent the height of the map, the width of the map and the period that the panel changes, respectively.

The following lines of input contains N lines with M chars for each line. In the map, "." means the empty cell, "*" means the trap which the robot cannot get in, "@" means the initial position of the robot and "$" means the diamond. There is exact one robot and one diamond on the map.

Output

For each test case, output minimum time that the robot can get the diamond. Output "YouBadbad" (without quotes) if it's impossible to get the diamond.

Sample Input

4
3 4 50
@...
***.
$...
5 5 2
.....
..@..
.*...
$.*..
.....
2 3 1
*.@
$.*
5 5 2
*****
..@..
*****
$....
.....

Sample Output

12
4
4
YouBadbad

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477

今年浙大校赛的题目。很可惜没有做出来啊~~~

#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
using namespace std;
int t,n,m,p,a,b,c,d,u;
char x[21][21];
int vis[15][15][10][500];
int r[4];
int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0}; 
struct node{
    int x, y, id, step;
    node(int xx, int yy, int ii, int s){  //这个写法很好,学习 
        x = xx; y = yy; id = ii; step = s;  
    }
};
int ok(int g,int h){
	if(g>=0&&g<n&&h>=0&&h<m&&x[g][h]!='*')
		return 1;
	else
		return 0;
}
void bfs(){
	queue<node> q;
	q.push(node(a,b,0,0)); //id和步数
	vis[a][b][0][0] = 1;  
	int iid;
	while(!q.empty()){
		node no=q.front();
		q.pop();
		int x1 = no.x, y = no.y, id = no.id, s = no.step;  
		if(x1==c&&y==d){
			cout<<s<<endl;
			u=1;
			break;
		}
		s++;
		if(s%p==0){  //方向键盘转动 
			if(ok(x1+dx[r[id]],y+dy[r[id]])){   //按下
				if(vis[x1+dx[r[id]]][y+dy[r[id]]][r[(id-1+4)%4]][s]==0){
					vis[x1+dx[r[id]]][y+dy[r[id]]][r[(id-1+4)%4]][s]=1;
					q.push(node(x1+dx[r[id]],y+dy[r[id]],(id-1+4)%4,s));
				}
			}
			if(vis[x1][y][r[id]][s]==0){  //向右 
				vis[x1][y][r[id]][s]=1;
				q.push(node(x1,y,id,s));
			} 
			if(vis[x1][y][r[(id-2+4)%4]][s]==0){  //向左 
				vis[x1][y][r[(id-2+4)%4]][s]=1;
				q.push(node(x1,y,(id-2+4)%4,s));
			}
			if(vis[x1][y][r[(id-1+4)%4]][s]==0){  //不动 
				vis[x1][y][r[(id-1+4)%4]][s]=1;
				q.push(node(x1,y,(id-1+4)%4,s));
			}
		}
		else{
			if(ok(x1+dx[r[id]],y+dy[r[id]])){   //按下
				if(vis[x1+dx[r[id]]][y+dy[r[id]]][r[id]][s]==0){
					vis[x1+dx[r[id]]][y+dy[r[id]]][r[id]][s]=1;
					q.push(node(x1+dx[r[id]],y+dy[r[id]],id,s));
				}
			} 
			if(vis[x1][y][r[(id+1)%4]][s]==0){ //向右 
				vis[x1][y][r[(id+1)%4]][s]=1;
				q.push(node(x1,y,(id+1)%4,s));
			}
			if(vis[x1][y][r[(id-1+4)%4]][s]==0){ //向左 
				vis[x1][y][r[(id-1+4)%4]][s]=1;
				q.push(node(x1,y,(id-1+4)%4,s));
			}
			if(vis[x1][y][r[id]][s]==0){  //不动(这里其实没用) 
				vis[x1][y][r[id]][s]=1;
				q.push(node(x1,y,id,s));
			}
		} 
	}
}
int main(){ 
	cin>>t;
	for(int i=0;i<4;++i)
		r[i]=i;
	while(t--){
		memset(vis,0,sizeof(vis));
		u=0;
		cin>>n>>m>>p;
		for(int i=0;i<n;++i){
			for(int j=0;j<m;++j){
				cin>>x[i][j];
				if(x[i][j]=='@'){
					a=i;b=j;
				}
				if(x[i][j]=='$'){
					c=i;d=j;
				}
			}
		}
		bfs();
		if(u==0)	
			cout<<"YouBadbad"<<endl;	
	}
	return 0; 
}


你可能感兴趣的:(zoj 3865 Superbot (广搜))