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

 #include<stdio.h>
#include<queue>
#include<string.h>
#include<math.h>
#include<algorithm>
struct node
{
int x , y , pan , ti ;
node () {}
node (int x , int y , int pan , int ti) : x (x) , y (y) , pan (pan) , ti (ti) { }
}; int move[][] ;
int n , m , p ;
char map[][] ;
bool dp[][][][] ; void rotate (int x)
{
int a0[][] = {{ , -} , { , } , {- , } , { , } } ;
int a1[][] = {{ , } , { , -} , { , } , {- , } } ;
int a2[][] = {{- , } , { , } , { , -} , { , } } ;
int a3[][] = {{ , } , {- , } , { , } , { , -} } ;
if (x == )
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
move[i][j] = a0[i][j] ;
}
}
else if (x == )
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
move[i][j] = a1[i][j] ;
}
}
else if (x == )
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
move[i][j] = a2[i][j] ;
}
}
else
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
move[i][j] = a3[i][j] ;
}
}
} void bfs (int sx , int sy)
{
node ans , tmp ;
ans = node (sx , sy , , ) ;
std::queue <node> q ;
while (!q.empty ()) q.pop () ;
q.push (ans) ;
dp[sx][sy][][] = ;
while (!q.empty ()) {
ans = q.front () ; q.pop () ;
// printf ("s (%d,%d)(%d)(%d)\n" , ans.x , ans.y , ans.pan , ans.ti) ;
for (int i = ; i < ; i++) {
tmp ;
int time = ans.ti + (fabs (i - ans.pan) == ? : fabs(i - ans.pan)) ;
rotate ((time/p)%) ;
tmp.x = ans.x + move[i][] ; tmp.y = ans.y + move[i][] ;
tmp.pan = i ;
if (tmp.x < || tmp.y < || tmp.x >= n || tmp.y >= m) continue ;
if (map[tmp.x][tmp.y] == '*') continue ;
tmp.ti = + time ;
if (tmp.ti > ) continue ;
if (dp[tmp.x][tmp.y][tmp.pan][tmp.ti]) continue ;
// printf ("(%d,%d)(%d)(%d)\n" , tmp.x , tmp.y , tmp.pan , tmp.ti) ;
dp[tmp.x][tmp.y][tmp.pan][tmp.ti] = ;
q.push (tmp) ;
}
if (ans.ti + <= ) {
ans.ti ++ ;
if (!dp[ans.x][ans.y][ans.pan][ans.ti])
q.push (ans) ;
}
}
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
int T ;
scanf ("%d" , &T) ;
while (T--) {
int x , y , ex , ey ;
scanf ("%d%d%d" , &n , &m , &p) ;
getchar () ;
for (int i = ; i < n ; i++) {
gets (map[i]) ;
}
for (int i = ; i < n ; i++) {
for (int j = ; j <m ; j++) {
if (map[i][j] == '@') {
x = i , y = j ;
}
if (map[i][j] == '$') {
ex = i , ey = j ;
}
}
}
memset (dp , , sizeof(dp)) ;
bfs (x , y) ;
int tim = ;
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++) {
if (dp[ex][ey][i][j] ) {
tim = std::min (tim , j) ;
}
}
}
if (tim == ) puts ("YouBadbad") ;
else printf ("%d\n" , tim) ;
}
return ;
}

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

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

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

最新文章

  1. 【BO】WEBI文件打开时提示Illegal access错误
  2. 读取excel数据,并统计输出Frame版本
  3. Java 8 Optional类深度解析
  4. windows 编程中的常见bug
  5. Install Oracle Java JDK/JRE 7u55 on Fedora 20/19, CentOS/RHEL 6.5/5.10
  6. SQL操作(增删改查)
  7. Jmeter对基于websocket协议的压力测试
  8. Python爬虫入门教程 41-100 Fiddler+夜神模拟器+雷电模拟器配置手机APP爬虫部分
  9. Java程序的第一次作业
  10. JavaScript是如何工作的:与WebAssembly比较及其使用场景
  11. RestTemplate之GET和POST调用和异步回调
  12. ★Pandas 零碎知识
  13. printf以%d形式输出浮点数的问题
  14. 求1~n整数中1出现的次数(《剑指offer》面试题43)
  15. git如何忽略已经加入版本控制的文件
  16. 一、JSP九大内置对象 二、JAVAEE三层架构和MVC设计模式 三、Ajax
  17. C# 取两个集合的交集\并集\差集
  18. 【LG3248】[HNOI2016]树
  19. 浅谈欧几里得算法求最大公约数(GCD)的原理及简单应用
  20. Transform开发cube模型权限处理之不同用户数据的过滤

热门文章

  1. 20145215《Java程序设计》第9周学习总结
  2. wpf键盘记录器
  3. java并发:中断一个正在运行的线程
  4. FileItem类 用法详解
  5. 成都普华永道税务开发的offer
  6. Ruby On Rails 常用的精品Gem汇总
  7. groovyConsole — the Groovy Swing console
  8. c#中的protected和internal
  9. java日期处理总结
  10. UML 几种关系的理解