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 ...
随机推荐
- iphone数据存储之-- Core Data的使用(一)
http://www.cnblogs.com/xiaodao/archive/2012/10/08/2715477.html 一.概念 1.Core Data 是数据持久化存储的最佳方式 2.数据最终 ...
- window 安装 sass compass 记录
1.安装Ruby 安装sass 和compass 需要 Ruby 的环境,还区分xp 和win7-8 下版本区别 ruby 官网中文 ruby 官网英文 注意: xp: 下载 [xp不能下载包含64位 ...
- 【云计算】基于Ansible的自动部署平台化思路
目标: 1.自动化—支持命令行.webui.api调用 2.支持基础命令.脚本.复杂任务编排.满足生产环境各类模块自动化部署需求 3.满足生产环境(开发.测试.线上)性能.可靠性.安全性要求 4.流程 ...
- ubuntu 15 安装Qt
Linux 下安装 QT5.4.0 http://blog.163.com/xd8171@126/blog/static/620810432015027111314471/ Linux qt ...
- 【转】如何修改Chrome缓存目录的地址
本文转自:http://www.nowamagic.net/librarys/veda/detail/2573 C盘空间越来越小,在Win7里还标红了,心里看得不舒服,得想一些方法腾出一些空间.看了A ...
- Mobile first! Wijmo 5 + Ionic Framework之:Hello World!
本教程中,我们用Wijmo 5 和 Ionic Framework实现一个Mobile的工程:Hello World. Ionic是什么? Ionic是一个HTML5框架.免费.开源,用于帮助生成hy ...
- 将文件放到Android模拟器的SD卡
1.打开DDMS页面2.打开File Explorer页,如果没有,在Window –> Show View –>File Explorer3.一般就在mnt –> sdcard中4 ...
- Java for LeetCode 034 Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- DP:Multiplication Puzzle(POJ 1651)
卡片游戏 题目大意:给你一排卡片,你可以从从中抽一些卡片(但是不能抽最左和最右的卡片),每张卡片上有一个数字,当你从中抽出一张卡片后,你将得卡片的数字和卡片左右两张卡片的数字的乘积的分数,问当剩下最左 ...