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 ...
随机推荐
- Ultra-QuickSort
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 44489 Accepted: 16176 ...
- js中document.documentElement 和document.body 以及其属性 clientWidth等
在设计页面时可能经常会用到固定层的位置,这就需要获取一些html对象的坐标以更灵活的设置目标层的坐标,这里可能就会用到document .body.scrollTop等属性,但是此属性在xhtml标准 ...
- js中的闭包之我理解
闭包是一个比较抽象的概念,尤其是对js新手来说.书上的解释实在是比较晦涩,对我来说也是一样. 但是他也是js能力提升中无法绕过的一环,几乎每次面试必问的问题,因为在回答的时候.你的答案的深度,对术语的 ...
- TCP/IP详解学习笔记(5)-- ICMP:internet 控制报文协议
1.概述 ICMP是(Internet Control Message Protocol)Internet控制报文协议.它是TCP/IP协议族的一个子协议,用于在IP主机.路由器之间传递控制 ...
- .Net Attribute特性
1.什么是Atrribute 首先,我们肯定Attribute是一个类,下面是msdn文档对它的描述: 公共语言运行时允许你添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标 ...
- 部署Office Web Apps Server并配置其与SharePoint 2013的集成
部署Office Web Apps Server并配置其与SharePoint 2013的集成 Office Web Apps Server 是新的 Office 服务器产品,它提供 Word.P ...
- putty mtputty 设置utf8编码
2013年10月30日 10:02:36 先load你指定的ip 然后选择左侧目录中的windows->translation 再在右侧选择utf-8编码 选中后,点击左侧目录中的session ...
- Java集合框架中Map接口的使用
在我们常用的Java集合框架接口中,除了前面说过的Collection接口以及他的根接口List接口和Set接口的使用,Map接口也是一个经常使用的接口,和Collection接口不同,Map接口并不 ...
- Linux系统排查3——I/O篇
当磁盘无法写入的时候,一般有以下可能: 文件系统只读 磁盘已满 I节点使用完 一. 遇到只读的文件系统 文件系统自动设置成只读可能是系统自我保护的一种机制,因此需要实现弄清究竟是什么原因造成了文件系统 ...
- HDU1850 Being a Good Boy in Spring Festival(博弈)
Being a Good Boy in Spring Festival Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I ...