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. silverlight ListBox 多列图片效果

    这个功能之前用wpf写过一次这次用Silverlight写一次 这两种写法上基本上没有太大的差别 这个Demo并不完美,只是给大家提供一个思路 源码:SilverLightListPricture.r ...

  2. 玩转数据库之 Group by Grouping

    有的时候我们要从数据库里把数据组织成树结构再展现到页面上 像下面这样 今天我们用Group 和Grouping实现它,并总结一下它俩. 先看一下概念,再用代码一点一点去理解它们,最后我会给出完整的代码 ...

  3. 简便的自动布局,对UIStackView的个人理解!

    序言: 更新了很久的Linux,我怕朋友们都视觉疲劳了,今天就更新在学ios开发时候,对一些知识点的理解.希望各位会喜欢! 正文: UIStackView 类提供了一个高效的接口用于平铺一行或一列的视 ...

  4. 【原创】基于Memcached 实现用户登录的Demo(附源码)

    一个简单的Memcached在Net中运用的一个demo.主要技术 Dapper+MVC+Memcached+sqlserver, 开发工具为vs2015+Sql 效果图如下: 登录后 解决方案 主要 ...

  5. JavaScript和html5 canvas生成圆形印章

    代码: function createSeal(id,company,name){ var canvas = document.getElementById(id); var context = ca ...

  6. 第一章:Javascript语言核心

    本节是javascript语言的一个快速预览,也是本书的第一部分快速预览. 读此书之前,感谢淘宝技术团队对此javascript核心的翻译,感谢弗拉纳根写出此书.感谢你们无私的分享,仅以此笔记献给你们 ...

  7. C语言和数据结构的书单-再次推荐

    一.推荐专业书单: 1)         C语言方面: n  明解C语言——适合初学者 豆瓣链接:https://book.douban.com/subject/23779374/ 推荐理由:< ...

  8. mysql-5.7.9-winx64 MySQL服务无法启动,服务没有报告任何错误的解决办法

    问题背景 最新解压版本的mysql 解压安装的时候报错 D:\mysql-5.7.9-winx64\bin>net start mysql MySQL 服务正在启动 . MySQL 服务无法启动 ...

  9. Oracle中新增表代码

    create table userinfo ( id varchar2(36) primary key, username varchar2(50) not null, password varcha ...

  10. maven_项目的依赖、聚合、继承

      一.假设目前有三个maven项目,分别是project.A.project.B.project.C 要求B依赖A.C依赖B但不依赖C 1.B添加对A的依赖 1 2 3 4 5 <depend ...