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 ...
随机推荐
- poj2993 翻转2996
Emag eht htiw Em Pleh Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2944 Accepted: ...
- MySql的like语句中的通配符:百分号、下划线和escape
MySql的like语句中的通配符:百分号.下划线和escape %:表示任意个或多个字符.可匹配任意类型和长度的字符. Sql代码 select * from user where user ...
- Quartz作业调度框架
Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提供了巨大的灵活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度.本 ...
- C++多态公有继承
面向对象的三个基本特征 面向对象的三个基本特征是:封装.继承.多态.其中,封装可以隐藏实现细节,使得代码模块化:继承可以扩展已存在的代码模块(类):它们的目的都是为了——代码重用.而多态则是为了实现另 ...
- maven web项目build失败
通过maven build发布web项目到tomcat时报如下异常: [INFO] ---------------------------------------------------------- ...
- OpenGL在 win8 64bits系统下的配置
1 program files(x86)与program files 在64位系统下,为了更好的兼容32位程序,在安装一些32位程序(注意某些程序他就是32位的),会默认扔到program files ...
- 解决Unable to locate Kerberos realm
在windows环境下 将服务器上的/etc/krb5.conf 复制到<jdk-home>/jre/lib/security
- 火车站(codevs 2287)
题目描述 Description 火车从始发站(称为第1站)开出,在始发站上车的人数为a,然后到达第2站,在第2站有人上.下车,但上.下车的人数相同,因此在第2站开出时(即在到达第3站之前)车上的人数 ...
- svn 文件夹 无法提交
[root@v01 www]# svn add localsvn/kkk/ svn: warning: 'localsvn/kkk' is already under version control ...
- error: insufficient permissions for device: verify udev rules
error: insufficient permissions for device: verify udev rules.See [http://developer.android.com/tool ...