标签:
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477
主要思路:1.从一个点(cur)到它相邻的点(next),所需要的时间数(t)其实是固定的,而且这个移动过程后,到达next时,相应的方向也是固定的,找到求t的办法就好了。
2.到达一个未到达的点可能有多条路,优先队列取时间最短的路,则答案最优
题目:
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:
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; 3rdmoves 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)
Author: DAI, Longao
Source: The 15th Zhejiang University Programming Contest
代码:
1 #include "cstdio" 2 #include "cstring" 3 #include "queue" 4 using namespace std; 5 6 #define N 15 7 #define INF 0x3333333 8 char map[N][N]; 9 int n,m,p; 10 int mark[N][N][4]; //到达每个点4个方向上的最短时间 11 int dir[4][2] = {{0,-1},/*0:left*/{0,1},/*1:right*/{-1,0}/*2:up*/,{1,0}/*3:down*/}; 12 13 typedef struct node 14 { 15 int x,y; //coordinate 16 int t; //time 17 int di; //direction 0:left 1:right 2:up 3:down 18 bool operator < (const node &b) const{ 19 return t > b.t; 20 } 21 /* 22 friend bool operator<(node a,node b) 23 { 24 return a.di > b.di; 25 } 26 */ 27 } Point; 28 29 Point st,en; 30 31 void Init() //标记数组初始化 32 { 33 for(int i=1; i<=n; i++) 34 { 35 for(int j=1; j<=m; j++) 36 { 37 for(int k=0; k<4; k++) 38 mark[i][j][k] = INF; 39 } 40 } 41 } 42 43 void Init_st_en() 44 { 45 for(int i=1; i<=n; i++) 46 { 47 for(int j=1; j<=m; j++) 48 { 49 if(map[i][j]==‘@‘) 50 st.x=i,st.y=j,st.t=0,st.di=0; 51 if(map[i][j]==‘$‘) 52 en.x=i,en.y=j; 53 } 54 } 55 } 56 57 int DFS(); 58 59 int main() 60 { 61 int i,T; 62 int ans; 63 scanf("%d",&T); 64 while(T--) 65 { 66 scanf("%d %d %d",&n,&m,&p); 67 for(i=1; i<=n; i++) 68 scanf("%s",map[i]+1); 69 Init_st_en(); 70 Init(); 71 ans = DFS(); 72 if(ans==-1) 73 printf("YouBadbad\n"); 74 else 75 printf("%d\n",ans); 76 } 77 return 0; 78 } 79 80 int DFS() 81 { 82 int i; 83 int x,y,di,t; 84 priority_queue<Point> q; 85 Point cur,next; 86 q.push(st); 87 map[st.x][st.y] = ‘*‘; 88 while(!q.empty()) 89 { 90 cur = q.top(); q.pop(); 91 if(cur.x==en.x && cur.y==en.y) 92 return cur.t; 93 for(i=0; i<4; i++) 94 { 95 next.x = x = cur.x+dir[i][0]; 96 next.y = y = cur.y+dir[i][1]; 97 next.di= i; 98 if(x<1 || x>n || y<1 || y>m || map[x][y]==‘*‘) continue; 99 di = cur.di; //若从点cur到点next,到next的时候,方向一定是i 100 t = cur.t; 101 if(t!=0 && t%p==0) 102 di = (di+4-1)%4; 103 if(di==i) //可以1s从点cur到点next的条件 104 t++; 105 else if( ((t+1)%p!=0 && ((di+1)%4==i || (di+4-1)%4==i)) //可以2s从点cur到点next的条件 106 || ((t+1)%p==0 && ((di+4-1)%4==i || (di+4-2)%4==i))) 107 t+=2; 108 else //最多3s可以从点cur到点next 109 t+=3; 110 next.t = t; 111 if(t<mark[x][y][i]) 112 { 113 q.push(next); 114 mark[x][y][i] = t; 115 } 116 } 117 } 118 return -1; 119 }
标签:
原文地址:http://www.cnblogs.com/ruo-yu/p/4430140.html