地图很小,根据题意BFS
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:
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 2ndposition; 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".
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.
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.
4 3 4 50 @... ***. $... 5 5 2 ..... ..@.. .*... $.*.. ..... 2 3 1 *.@ $.* 5 5 2 ***** ..@.. ***** $.... .....
12
4
4
YouBadbad
For the first example:
0s: start
1s: cursor move right (cursor is at "right")
2s: press button (robot move right)
3s: press button (robot move right)
4s: press button (robot move right)
5s: cursor move right (cursor is at "up")
6s: cursor move right (cursor is at "down")
7s: press button (robot move down)
8s: press button (robot move down)
9s: cursor move right (cursor is at "left")
10s: press button (robot move left)
11s: press button (robot move left)
12s: press button (robot move left)
For the second example:
0s: start
1s: press button (robot move left)
2s: press button (robot move left)
--- panel rotated ---
3s: press button (robot move down, without changing cursor)
4s: press button (robot move down)
For the third example:
0s: start
1s: press button (robot move left)
--- panel rotated ---
2s: press button (robot move down)
--- panel rotated ---
3s: cursor move left (cursor is at "right")
--- panel rotated ---
4s: press button (robot move left)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int dir_x[5]={0,0,0,-1,1}; const int dir_y[5]={0,-1,1,0,0}; int n,m,p; char mp[20][20]; bool inside(int x,int y) { if((x<0||x>=n)||(y<0||y>=m)) return false; return true; } struct ZT { int x,y; ///位置 int cmd; ///命令顺序 int time;/// 时间 }; int getcmd(int x){return x/1000;} int shiftL(int x){return (x%1000)*10+x/1000;} int shiftR(int x){int t=x%10;return x/10+t*1000;} ZT st,ed; bool vis[20][20][4]; int bfs() { queue<ZT> q; int ans=-1; ZT e; e.x=st.x; e.y=st.y; e.cmd=1234; e.time=0; memset(vis,false,sizeof(vis)); vis[st.x][st.y][getcmd(e.cmd)]=true; q.push(e); while(!q.empty()) { ZT u=q.front(); q.pop(); //cout<<"x: "<<u.x<<" , y: "<<u.y<<endl; //cout<<"cmd: "<<u.cmd<<" time "<<u.time<<endl; //getchar(); if((u.x==ed.x&&u.y==ed.y)||(mp[u.x][u.y]=='$')) { ans=u.time; break; } ZT v; /// L shift v=u; v.cmd=shiftL(v.cmd); v.time++; if(v.time%p==0) v.cmd=shiftR(v.cmd); if(vis[v.x][v.y][getcmd(v.cmd)]==false) { vis[v.x][v.y][getcmd(v.cmd)]=true; q.push(v); } /// R shift v=u; v.cmd=shiftR(v.cmd); v.time++; if(v.time%p==0) v.cmd=shiftR(v.cmd); if(vis[v.x][v.y][getcmd(v.cmd)]==false) { vis[v.x][v.y][getcmd(v.cmd)]=true; q.push(v); } /// do nothing v=u; v.time++; if(v.time%p==0) v.cmd=shiftR(v.cmd); if(vis[v.x][v.y][getcmd(v.cmd)]==false) { vis[v.x][v.y][getcmd(v.cmd)]=true; q.push(v); } /// do it v=u; int dr=getcmd(v.cmd); v.x=v.x+dir_x[dr];v.y=v.y+dir_y[dr]; if(inside(v.x,v.y)==false) continue; if(mp[v.x][v.y]=='*') continue; v.time++; if(v.time%p==0) v.cmd=shiftR(v.cmd); if(vis[v.x][v.y][getcmd(v.cmd)]==false) { vis[v.x][v.y][getcmd(v.cmd)]=true; q.push(v); } } if(ans==-1) puts("YouBadbad"); else printf("%d\n",ans); } int main() { int T_T; scanf("%d",&T_T); while(T_T--) { memset(mp,0,sizeof(mp)); scanf("%d%d%d",&n,&m,&p); for(int i=0;i<n;i++) { scanf("%s",mp[i]); for(int j=0;j<m;j++) { if(mp[i][j]=='@') st.x=i,st.y=j; if(mp[i][j]=='$') ed.x=i,ed.y=j; } } bfs(); } return 0; }