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. IL指令大全

    IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的程序代码并不是CPU能直接执 ...

  2. android用欢迎界面加载运行环境

    以前一直以为splash页只是图好玩.. 今天才知道是应该把环境加载放在这个页面... 论坛和github上太多仿XXX的项目果然只能学习下ui的思路... 以前把环境加载放在application里 ...

  3. 第六章 prototype和constructor

    首先我们看下面一段代码(第六章 01.htm) function myfun() //定义一个函数myfun { }; console.log(typeof (myfun.prototype)); c ...

  4. 打造自己的MyLifeOrganized 2(MLO2)云同步

    0x01 官方云同步,付费也很卡 MyLifeOrganized(MLO)是Windows平台下强大的GTD软件,PC版本和Android版本需要分别购买授权,云同步还要再买包月或包年服务真不便宜,关 ...

  5. JDO持久 (jdbc ejb)

    转自:http://blog.csdn.net/liuzhigang1237/article/details/6305113 JDO快速入门 Java数据对象(Java Data Objects,JD ...

  6. .net架构设计读书笔记--第三章 第8节 域模型简介(Introducing Domain Model)

    一.数据--行为转变     很长的时间,典型的分析方法或多或少是以下两种,第一,收集需求并做一些分析,找出有关实体 (例如,客户. 订单. 产品) 和进程来实现. 第二,手持这种理解你尝试推断一个物 ...

  7. JS所谓的享元模式-->

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  8. spring - ioc和aop

    1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对象的时候,一般都是直接使用关键字类new ...

  9. opencv笔记4:模板运算和常见滤波操作

    time:2015年10月04日 星期日 00时00分27秒 # opencv笔记4:模板运算和常见滤波操作 这一篇主要是学习模板运算,了解各种模板运算的运算过程和分类,理论方面主要参考<图像工 ...

  10. 洛谷P2507 [SCOI2008]配对

    题目背景 四川NOI2008省选 题目描述 你有 n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一个Bp[i].要求所有配对的整数差的绝对值之和尽量小,但不允许两个相同的数配对. ...