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.
OutputFor 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 Input4 3 4 50 @... ***. $... 5 5 2 ..... ..@.. .*... $.*.. ..... 2 3 1 *.@ $.* 5 5 2 ***** ..@.. ***** $.... .....Sample Output
12
4
4
YouBadbad
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=20;
const int inf=0x3f3f3f3f;
char mp[maxn][maxn];
int stx,sty,edx,edy;
struct node{
int x,y;
int s,p;
node(){}
node(int _x,int _y,int _s,int _p):x(_x),y(_y),s(_s),p(_p){}
bool operator<(const node& b)const{
return s>b.s;
}
};
int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
int min_[maxn][maxn][4];
int n,m,P;
/*
模拟题
当前状态有四种 选择,我们按照要求写就好了
只是 这个当前的位置不明确
我们不用关下一个点的方向,我们只需要管,当前点的就好
他的这种调节比较巧妙,因为都是一秒一秒的走,
所以他是直接压进去,然后再看看我这个点到底现在的朝向到底是哪
这样来看得话,确实简单了超级多的步骤
扔进去的时候不调节,只是我们拿出来的时候调节
*/
void bfs(){
priority_queue<node> q;
q.push(node(stx,sty,0,0));
while(!q.empty()){
// printf("****\n");
struct node t=q.top();
q.pop();
// printf("%d %d\n",t.x,t.y);
if(t.s!=0&&t.s%P==0) t.p=(t.p-1+4)%4;
if(t.s>=min_[t.x][t.y][t.p])continue;
min_[t.x][t.y][t.p]=t.s;
if(mp[t.x][t.y]=='$') return;
struct node tt=t;
tt.s++;
tt.p=(t.p+1)%4;
q.push(tt);
tt.p=(t.p-1+4)%4;
q.push(tt);
tt=t;
tt.s++;
tt.x+=dir[t.p][0],tt.y+=dir[t.p][1];
if(!(tt.x<1||tt.x>n||tt.y<1||tt.y>m||mp[tt.x][tt.y]=='*'))
q.push(tt);
tt=t;
tt.s++;
q.push(tt);
}
}
void init(){
memset(min_,inf,sizeof(min_));
}
int main(){
int T;
scanf("%d",&T);
while(T--){
init();
scanf("%d %d %d",&n,&m,&P);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf(" %c",&mp[i][j]);
if(mp[i][j]=='@'){
stx=i,sty=j;
}
if(mp[i][j]=='$'){
edx=i,edy=j;
}
}
}
bfs();
int ans=inf;
for(int i=0;i<4;i++){
ans=min(ans,min_[edx][edy][i]);
}
if(ans==inf)
printf("YouBadbad\n");
else
printf("%d\n",ans);
}
return 0;
}