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维标志。

zoj.3865.Superbot(bfs + 多维dp)的更多相关文章

  1. 浙江大学2015年校赛F题 ZOJ 3865 Superbot BFS 搜索

    不知道为什么比赛的时候一直想着用DFS 来写 一直想剪枝结果还是TLE = = 这题数据量不大,又是问最优解,那么一般来说是用 BFS 来写 int commandi[4] = {1, 2, 3, 4 ...

  2. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...

  3. ZOJ - 3865 Superbot 【BFS】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3865 思路 一个迷宫题 但是每次的操作数和普通的迷宫题不一样 0 ...

  4. ZOJ Problem Set - 3865 Superbot (bfs)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 大牛博客:http://www.cnblogs.com/kylehz/p ...

  5. ZOJ 3865 Superbot(优先队列--模板)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 主要思路:1.从一个点(cur)到它相邻的点(next),所需 ...

  6. Zoj 3865 Superbot

    按规则移动机器人 , 问是否能拾得宝藏 . 加了一个控制板 , 还增加了一个控制板移动周期 p 将移动周期变换一下 , 移动一次  就相当于光标向左不耗费时间的移动了一格 搜索思路 : 搜索当前格子到 ...

  7. 悦动达人 (多维dp)

    悦动达人 Description 一个游戏,在屏幕上有5个格子形成一行,每一秒都会有一个格子闪烁,格子闪烁时你需要保证至少有一只手指在格子上面, 现在我们已经知道第i秒时,第xi个格子会闪烁,我们假设 ...

  8. POJ - 1170 Shopping Offers (五维DP)

    题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折.有s种打折方式.问怎么才干使买的价格达到最低 解题思路:最多仅仅有五种商品.且每件商品最多仅仅有5个,所以能够用5维dp来 ...

  9. luogu 4401 矿工配餐 多维dp

    五维dp,记忆化搜索会MLE超内存,所以用滚动数组,十分经典 五维dp #include <bits/stdc++.h> using namespace std; ; ][][][],la ...

随机推荐

  1. 20145222黄亚奇《Java程序设计》第5周学习总结

    教材学习内容总结 Java中所有错误都会被打包为对象,运用try.catch,可以在错误发生时显示友好的错误信息. 运用try.catch,还可以在捕捉处理错误之后,尝试恢复程序正常执行流程.如: i ...

  2. mysql慢查询分析工作pt-query-digest的使用

    一.简单安装 wget percona.com/get/pt-query-digest chmoe u+x pt-query-digest 二.简单使用 ./pt-query-digest /var/ ...

  3. java中的自增问题

    运行下面这段代码,其结果是什么呢? package com.test; public class Inc { public static void main(String[] args) { Inc ...

  4. js的深度拷贝和浅拷贝

    从extend看浅拷贝和深拷贝 请先查看: http://blog.sina.com.cn/s/blog_912389e5010120n2.html

  5. jquery基本方法

    jquery的delay sleep 与js的setTime的区别. delay 和sleep推荐不要用,太难用了. click on live delegate bind http://www.jb ...

  6. linq查询语句转mongodb

    && (与操作) 子表达式可以通过&&合并来查询满足所有子表达式的文档 var query = from c in collection.AsQueryable< ...

  7. 求根号m(巴比伦算法)

    巴比伦算法是针对求根号m的近似值情况的,它的思想是这样的: 设根号m=X0,则如果枚举有答案X(X<X0),则m/X>X0,当精度要求不高的时候,我们可以看成X=m/X=X0,而如果精度要 ...

  8. 14.C#属性访问器、命名空间、pragma指令(七章7.3-7.5)

    看到一些零星的知识片,今天就用自己的理解说明下,也是因为太简单了,一下就过的,也是我们日常开发中常用.留下一个脚印,当书不在手上的,也能翻出来看看.说下属性访问器.命名空间和pragma指令. 属性访 ...

  9. 关于js的string的3个函数slice,substring,substr对比

    slice,substring,substr三个函数都是截取字符串,但是对参数的处理有区别 参数处理相似的两个函数式slice和substring slice(start,end)和substring ...

  10. Java死锁的例子

    死锁 死锁是这样一种情形:多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放.由于线程被无限期地阻塞,因此程序不可能正常终止. 导致死锁的根源在于不适当地运用“synchronized”关 ...