BFS+模拟 ZOJ 3865 Superbot
/*
BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少;
BFS分4种情况入队,最后在终点4个方向寻找最小值:)
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
using namespace std; const int MAXN = 1e2 + ;
const int INF = 0x3f3f3f3f;
int dir[][][] = {
{{, -}, {, }, {-, }, {, }},
{{, }, {, -}, {, }, {-, }},
{{-, }, {, }, {, -}, {, }},
{{, }, {-, }, {, }, {, -}}
};
int dp[][][];
int dp2[][][];
int ans[][][];
char maze[][];
int sx, sy, ex, ey;
int n, m, P; bool ok(int nx, int ny, int np)
{
if (nx < || nx >= n || ny < || ny >= m) return false;
if (maze[nx][ny] == '*' || dp[nx][ny][np] != -) return false; return true;
} void BFS(void)
{
int x, y, nx, ny, p, np;
queue<int> q;
q.push (sx); q.push (sy); q.push (); while (!q.empty ())
{
x = q.front (); q.pop ();
y = q.front (); q.pop ();
p = q.front (); q.pop (); if (x == ex && y == ey && ans[x][y][p] == -)
ans[x][y][p] = dp[x][y][p]; nx = x + dir[dp[x][y][p]/P%][p][];
ny = y + dir[dp[x][y][p]/P%][p][];
if (ok (nx, ny, p)) //move robot, not move dir
{
dp[nx][ny][p] = dp[x][y][p] + ;
q.push (nx); q.push (ny); q.push (p);
} np = p + ;
if (np > ) np = ;
if (ok (x, y, np)) //not move robot, move dir to right
{
dp[x][y][np] = dp[x][y][p] + ;
q.push (x); q.push (y); q.push (np);
} np = p - ;
if (np < ) np = ;
if (ok (x, y, np)) //not move robot, move dir to left
{
dp[x][y][np] = dp[x][y][p] + ;
q.push (x); q.push (y); q.push (np);
} if (dp[x][y][p] <= ) //not move
{
dp[x][y][p]++;
q.push (x); q.push (y); q.push (p);
}
} return ;
} int main(void) //ZOJ 3865 Superbot
{
//freopen ("F.in", "r", stdin); int t;
scanf ("%d", &t);
while (t--)
{
memset (dp, -, sizeof (dp));
memset (ans, -, sizeof (ans)); scanf ("%d%d%d", &n, &m, &P); for (int i=; i<n; ++i) scanf ("%s", &maze[i]);
for (int i=; i<n; ++i)
{
for (int j=; j<m; ++j)
{
if (maze[i][j] == '@') sx = i, sy = j;
else if (maze[i][j] == '$') ex = i, ey = j;
}
} dp[sx][sy][] = ;
BFS (); int mn = INF;
for (int i=; i<; ++i)
{
if (ans[ex][ey][i] == -) continue;
mn = min (mn, ans[ex][ey][i]);
} if (mn == INF) printf ("YouBadbad\n");
else printf ("%d\n", mn);
} return ;
}
BFS+模拟 ZOJ 3865 Superbot的更多相关文章
- zoj.3865.Superbot(bfs + 多维dp)
Superbot Time Limit: 2 Seconds Memory Limit: 65536 KB Superbot is an interesting game which you ...
- ZOJ - 3865 Superbot 【BFS】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3865 思路 一个迷宫题 但是每次的操作数和普通的迷宫题不一样 0 ...
- 浙江大学2015年校赛F题 ZOJ 3865 Superbot BFS 搜索
不知道为什么比赛的时候一直想着用DFS 来写 一直想剪枝结果还是TLE = = 这题数据量不大,又是问最优解,那么一般来说是用 BFS 来写 int commandi[4] = {1, 2, 3, 4 ...
- ZOJ 3865 Superbot(优先队列--模板)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 主要思路:1.从一个点(cur)到它相邻的点(next),所需 ...
- Zoj 3865 Superbot
按规则移动机器人 , 问是否能拾得宝藏 . 加了一个控制板 , 还增加了一个控制板移动周期 p 将移动周期变换一下 , 移动一次 就相当于光标向左不耗费时间的移动了一格 搜索思路 : 搜索当前格子到 ...
- hdu_1495_非常可乐(bfs模拟)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意:不解释 题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长 ...
- Hdu 5336 XYZ and Drops (bfs 模拟)
题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...
- ZOJ Problem Set - 3865 Superbot (bfs)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 大牛博客:http://www.cnblogs.com/kylehz/p ...
- 模拟 ZOJ 3878 Convert QWERTY to Dvorak
题目传送门 /* 模拟:手敲map一一映射,累! 除了忘记读入字符串不能用gets用getline外还是很顺利的AC了:) */ #include <cstdio> #include &l ...
随机推荐
- HTML中的IE条件注释
IE条件注释是一种特殊的HTML注释,这种注释只有IE5.0及以上版本才能理解.比如普通的HTML注释是: <!--This is a comment--> 而只有IE可读的IE条件注释是 ...
- Android学习笔记之打钩显示输入的密码
利用EditText作为密码输入框是个不错的选择(只需设置输入类型为textPassword即可),保密且无需担心被盗取.但有时用户也不知道自己输入的是否正确,这时就应该提供一个“显示密码”的复选框, ...
- linux下vim命令详解 转自: zhanglong0426
高级一些的编辑器,都会包含宏功能,vim当然不能缺少了,在vim中使用宏是非常方便的: :qx 开始记录宏,并将结果存入寄存器xq 退出记录模式@x 播放记录在x寄存器中的 ...
- WPF 将PPT,Word转成图片
在Office下,PowerPoint可以直接把每张幻灯片转成图片,而Word不能直接保存图片.所以只能通过先转换成xps文件,然后再转成图片. 一.PPT 保存为图片 /// <summary ...
- Python——内置类型
Python定义了丰富的数据类型,包括: 数值型:int, float, complex 序列:(iterable) str, unicode, tuple, list, bytearray, buf ...
- Flash Player 19.0.0.124 Beta + IHTMLDocument3 IHTMLDocument2 ->get_innerHTML
安装 Flash Player 19 之后 有 flash 动画的网页中 IHTMLDocument3 IHTMLDocument2 ->get_innerHTML 获取的 html 内容都是空 ...
- canvas API ,通俗的canvas基础知识(五)
前几期讲的都是路径图形的绘图,这节我们要讲的是如何在画布上操作图片,因为图形画不了漂亮妹子(画图高手忽略不计),想画美女怎么办?跟我来: 想要在画布中插入一张图片,我们需要的方法是这位大侠: draw ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Light OJ 1253 Misere Nim (尼姆博弈(2))
LightOJ1253 :Misere Nim 时间限制:1000MS 内存限制:32768KByte 64位IO格式:%lld & %llu 描述 Alice and Bob ar ...