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 N, M (2 <= N, M <= 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)的更多相关文章
- 浙江大学2015年校赛F题 ZOJ 3865 Superbot BFS 搜索
不知道为什么比赛的时候一直想着用DFS 来写 一直想剪枝结果还是TLE = = 这题数据量不大,又是问最优解,那么一般来说是用 BFS 来写 int commandi[4] = {1, 2, 3, 4 ...
- BFS+模拟 ZOJ 3865 Superbot
题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...
- ZOJ - 3865 Superbot 【BFS】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3865 思路 一个迷宫题 但是每次的操作数和普通的迷宫题不一样 0 ...
- ZOJ Problem Set - 3865 Superbot (bfs)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 大牛博客:http://www.cnblogs.com/kylehz/p ...
- ZOJ 3865 Superbot(优先队列--模板)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 主要思路:1.从一个点(cur)到它相邻的点(next),所需 ...
- Zoj 3865 Superbot
按规则移动机器人 , 问是否能拾得宝藏 . 加了一个控制板 , 还增加了一个控制板移动周期 p 将移动周期变换一下 , 移动一次 就相当于光标向左不耗费时间的移动了一格 搜索思路 : 搜索当前格子到 ...
- 悦动达人 (多维dp)
悦动达人 Description 一个游戏,在屏幕上有5个格子形成一行,每一秒都会有一个格子闪烁,格子闪烁时你需要保证至少有一只手指在格子上面, 现在我们已经知道第i秒时,第xi个格子会闪烁,我们假设 ...
- POJ - 1170 Shopping Offers (五维DP)
题目大意:有一个人要买b件商品,给出每件商品的编号,价格和数量,恰逢商店打折.有s种打折方式.问怎么才干使买的价格达到最低 解题思路:最多仅仅有五种商品.且每件商品最多仅仅有5个,所以能够用5维dp来 ...
- luogu 4401 矿工配餐 多维dp
五维dp,记忆化搜索会MLE超内存,所以用滚动数组,十分经典 五维dp #include <bits/stdc++.h> using namespace std; ; ][][][],la ...
随机推荐
- 身份证号码自动生成程序(Python)
今天收到一个小需求:需要一个自动生成身份证号码的小程序.近期用python较多,因此打算用python实现. 需求细化: 1.身份证必须能够通过身份证校验程序. 2.通过查询,发现身份证号码是有国家标 ...
- 利用php实现文件迁移重命名
首先表明,这是一个悲伤的故事. 暑假来临,学校安排我们到某软件外包公司实习,想想不用面试也是蛮方便的,可以借此机会向大牛学习学习,虽然没有工资(据说学校还交了600块的保险),但想想还是蛮期待的,但真 ...
- 『片段』OracleHelper (支持 多条SQL语句)
C# 调用 Oracle 是如此尴尬 >System.Data.OracleClient.dll —— .Net 自带的 已经 过时作废. >要链接 Oracle 服务器,必须在 本机安装 ...
- Ado.net 通用访问类
public class DbHelperSQL { private static string connString = ConfigurationManager.ConnectionStrings ...
- [Aaronyang] 写给自己的WPF4.5 笔记[2依赖属性]
人生的意义不在于拿一手好牌,而在于打好一手坏牌 --Aaronyang的博客(www.ayjs.net)-www.8mi.me =============时隔两年后再看WPF========== 因为 ...
- WCF 入门 (17)
前言 看的是入门视频,就希望培养一个学习的习惯吧. 前段时间看了微软的SurfaceBook的视频,被惊艳到了,但是我没钱买啊... 第17集 WCF中未经处理的异常 Unhandled except ...
- 第三十一课:JSDeferred详解2
这一课,我们先接着上一课讲一下wait方法,以及wait方法是如何从静态方法变化实例方法的. 首先我们先看wait方法为啥可以从静态方法变成实例方法,请看register源码: Deferred.re ...
- Memcached——非关系型数据库分布式处理
Memcached登录校验应用: MMCacheWriter.cs类 using Memcached.ClientLibrary; using System; using System.Collect ...
- Java-ArrayList
package 集合类.list类; /** * System.Collections.ArrayList类是一个特殊的数组.通过添加和删除元素,就可以动态改变数组的长度. 一.优点 1.支持自动改变 ...
- Java基础-静态代理与动态代理比较
JAVA的静态代理与动态代理比较 静态代理类: 由程序员创建或由特定工具自动生成源代码,再对其编译.在程序运行前,代理类的.class文件就已经存在了.动态代理类: 在程序运行时,运用反射机制动态创建 ...