zoj.3865.Superbot(bfs + 多维dp)

Superbot

Time Limit: 2 Seconds       Memory Limit: 65536 KB

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 NM (2 <= NM <= 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

Hint

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<stdio.h>
  2 #include<queue>
  3 #include<string.h>
  4 #include<math.h>
  5 #include<algorithm>
  6 struct node
  7 {
  8     int x , y , pan , ti ;
  9     node () {}
 10     node (int x , int y , int pan , int ti) : x (x) , y (y) , pan (pan) , ti (ti) { }
 11 };
 12 
 13 int move[4][2] ;
 14 int n , m , p ;
 15 char  map[12][12] ;
 16 bool dp[12][12][4][300] ;
 17 
 18 void rotate (int x)
 19 {
 20     int a0[4][2] = {{0 , -1} , {0 , 1} , {-1 , 0} , {1 , 0}  } ;
 21     int a1[4][2] = {{1 , 0} , {0 , -1} , {0 , 1} , {-1 , 0}  } ;
 22     int a2[4][2] = {{-1 , 0} , {1 , 0} , {0 , -1} , {0 , 1}  } ;
 23     int a3[4][2] = {{0 , 1} , {-1 , 0} , {1 , 0} , {0 , -1}  } ;
 24     if (x == 0)
 25     for (int i = 0 ; i < 4 ; i++) {
 26         for (int j = 0 ; j < 2 ; j++) {
 27             move[i][j] = a0[i][j] ;
 28         }
 29     }
 30     else if (x == 1)
 31         for (int i = 0 ; i < 4 ; i++) {
 32         for (int j = 0 ; j < 2 ; j++) {
 33             move[i][j] = a1[i][j] ;
 34         }
 35     }
 36     else if (x == 2)
 37         for (int i = 0 ; i < 4 ; i++) {
 38         for (int j = 0 ; j < 2 ; j++) {
 39             move[i][j] = a2[i][j] ;
 40         }
 41     }
 42     else
 43         for (int i = 0 ; i < 4 ; i++) {
 44         for (int j = 0 ; j < 2 ; j++) {
 45             move[i][j] = a3[i][j] ;
 46         }
 47     }
 48 }
 49 
 50 void bfs (int sx , int sy)
 51 {
 52     node ans , tmp ;
 53     ans = node (sx , sy , 0 , 0) ;
 54     std::queue <node> q ;
 55     while (!q.empty ()) q.pop () ;
 56     q.push (ans) ;
 57     dp[sx][sy][0][0] = 1 ;
 58     while (!q.empty ()) {
 59         ans = q.front () ; q.pop () ;
 60      //   printf ("s   (%d,%d)(%d)(%d)\n" , ans.x , ans.y , ans.pan , ans.ti) ;
 61         for (int i = 0 ; i < 4 ; i++) {
 62             tmp ;
 63             int time = ans.ti + (fabs (i - ans.pan) == 3 ? 1 : fabs(i - ans.pan)) ;
 64             rotate ((time/p)%4) ;
 65             tmp.x = ans.x + move[i][0] ; tmp.y = ans.y + move[i][1] ;
 66             tmp.pan = i ;
 67             if (tmp.x < 0 || tmp.y <  0 || tmp.x >= n || tmp.y >= m) continue ;
 68             if (map[tmp.x][tmp.y] == '*') continue ;
 69             tmp.ti = 1 + time ;
 70             if (tmp.ti > 200)  continue ;
 71             if (dp[tmp.x][tmp.y][tmp.pan][tmp.ti]) continue ;
 72           //  printf ("(%d,%d)(%d)(%d)\n" , tmp.x , tmp.y , tmp.pan , tmp.ti) ;
 73             dp[tmp.x][tmp.y][tmp.pan][tmp.ti] = 1 ;
 74             q.push (tmp) ;
 75         }
 76         if (ans.ti + 1 <= 200) {
 77             ans.ti ++ ;
 78             if (!dp[ans.x][ans.y][ans.pan][ans.ti])
 79                 q.push (ans) ;
 80         }
 81     }
 82 }
 83 
 84 int main ()
 85 {
 86    // freopen ("a.txt" , "r" , stdin ) ;
 87     int T ;
 88     scanf ("%d" , &T) ;
 89     while (T--) {
 90         int x , y , ex , ey ;
 91         scanf ("%d%d%d" , &n , &m , &p) ;
 92         getchar () ;
 93         for (int i = 0 ; i < n ; i++) {
 94             gets (map[i]) ;
 95         }
 96         for (int i = 0 ; i < n ; i++) {
 97             for (int j = 0 ; j <m ; j++) {
 98                 if (map[i][j] == '@') {
 99                     x = i , y = j ;
100                 }
101                 if (map[i][j] == '$') {
102                     ex = i , ey = j ;
103                 }
104             }
105         }
106         memset (dp , 0 , sizeof(dp)) ;
107         bfs (x , y) ;
108         int tim = 99999999 ;
109         for (int i = 0 ; i < 4 ; i++) {
110             for (int j = 0 ; j < 301 ; j++) {
111                 if (dp[ex][ey][i][j] ) {
112                    tim = std::min (tim , j) ;
113                 }
114             }
115         }
116         if (tim == 99999999) puts ("YouBadbad") ;
117         else printf ("%d\n" , tim) ;
118     }
119     return 0 ;
120 }
View Code

一直让我很伤脑筋的问题是如何令 “ 喝咖啡"这个行动加入到队列中,后来问学长:

最大情况为10 * 10 * 4 * 200 = 8 * 10^4 很显然暴力过吧,orz。

所以不能回头的状态也用4维标志。

转载于:https://www.cnblogs.com/get-an-AC-everyday/p/4426025.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值