浙江大学2015年校赛F题 ZOJ 3865 Superbot BFS 搜索
不知道为什么比赛的时候一直想着用DFS 来写
一直想剪枝结果还是TLE = =
这题数据量不大,又是问最优解,那么一般来说是用 BFS 来写
int commandi[4] = {1, 2, 3, 4};
我定义了一个方向数组,其实题目意思中的,指针移动还有操作版的变化本质上都是指针的移动
在此只需要 额外定义一个变量 cur 在数组 commandi 中循环移动即可
这道题目还是因为数据量不大吧,直接用 STL 中的 Queue 即可,优先队列肯定会更快。
总体来说,还是一道容易题。
Source Code :
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; typedef long long ll ;
typedef unsigned long long ull ;
typedef unsigned int uint ;
typedef unsigned char uchar ; template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;} const double eps = 1e- ;
const int N = ;
const int M = * ;
const ll P = 10000000097ll ;
const int MAXN = ;
const int INF = 0x3f3f3f3f ; const int dir_x[] = {, , , -, };
const int dir_y[] = {, -, , , }; struct sc {
int x, y;
int time;
int cur;
} st, ed; int n, m, p;
char a[][];
bool vis[][][];
int commandi[] = {, , , };
queue <sc> q; bool check (int x, int y) {
return x >= && x <= n && y >= && y <= m;
} int left (int &cur) {
cur = (cur - + ) % ;
} int right (int & cur) {
cur = (cur + + ) % ;
} void solve (struct sc v) {
++v.time;
if (v.time % p == ) {
left (v.cur);
}
if (vis[v.x][v.y][commandi [v.cur]] == false) {
vis[v.x][v.y][commandi [v.cur]] = true;
q.push (v);
}
} void bfs () {
int ans = -INF; sc e;
e = st;
e.time = ;
e.cur = ; memset (vis, , sizeof (vis));
vis [e.x][e.y][commandi [e.cur]] = true;
q.push (e); while (!q.empty ()) {
sc u = q.front ();
q.pop ();
if ((u.x == ed.x && u.y == ed.y) || '$' == a[u.x][u.y]) {
ans = u.time;
break;
} sc v = u; //to left
left (v.cur);
solve (v); v = u; //to right
right (v.cur);
solve (v); v = u; //wait
solve (v); v = u; //press
int dr = commandi [v.cur];
v.x += dir_x[dr];
v.y += dir_y[dr];
if (!check (v.x, v.y)) continue;
if ('*' == a[v.x][v.y]) continue;
solve (v);
} if (-INF == ans) {
cout << "YouBadbad" << endl;
} else {
cout << ans << endl;
}
} int main () {
int i, j, t; cin >> t;
while (t--) {
while (!q.empty ()) q.pop ();
cin >> n >> m >> p;
for (i = ; i <= n; ++i) {
for (j = ; j <= m; ++j) {
cin >> a[i][j];
if ('@' == a[i][j]) {
st.x = i;
st.y = j;
} else if ('$' == a[i][j]) {
ed.x = i;
ed.y = j;
}
}
}
bfs ();
} return ;
} /*
10 10 3
@.........
..........
..........
..........
..........
..........
..........
..........
..........
.........$ 10 5 3
@....
.....
.....
.....
.....
.....
.....
.....
.....
....$
*/
浙江大学2015年校赛F题 ZOJ 3865 Superbot BFS 搜索的更多相关文章
- 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock
这道题目是队友写的,貌似是用暴力枚举出来. 题意:给出一组数,要求这组数在解锁的界面可能的滑动序列. 思路:按照是否能够直接到达建图,如1可以直接到2,但是1不能直接到3,因为中间必须经过一个2. 要 ...
- zoj.3865.Superbot(bfs + 多维dp)
Superbot Time Limit: 2 Seconds Memory Limit: 65536 KB Superbot is an interesting game which you ...
- 2013年山东省赛F题 Mountain Subsequences
2013年山东省赛F题 Mountain Subsequences先说n^2做法,从第1个,(假设当前是第i个)到第i-1个位置上哪些比第i位的小,那也就意味着a[i]可以接在它后面,f1[i]表示从 ...
- ACM-ICPC 2019南昌网络赛F题 Megumi With String
ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...
- BFS+模拟 ZOJ 3865 Superbot
题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...
- ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds Me ...
- ZOJ 3814 Sawtooth Puzzle (2014年牡丹江赛区网络赛F题)
1.题目描写叙述:点击打开链接 2.解题思路:本题是一道隐式图的搜索题目.一般来说,这类题目首先要定义状态,接下来是弄清楚状态怎样转移,以及状态怎样判重,怎样推断当前状态是否和目标状态同样.至于求解最 ...
- (中等) Hiho 1232 Couple Trees(15年北京网络赛F题),主席树+树链剖分。
"Couple Trees" are two trees, a husband tree and a wife tree. They are named because they ...
- 上海高校金马五校赛 F题:1 + 2 = 3?
链接:https://www.nowcoder.com/acm/contest/91/F来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言26214 ...
随机推荐
- (asp.net MVC学习)System.Web.Mvc.UrlHelper的学习与使用
上一次学习了HtmlHelper帮助类,这次我们学习一下UrlHelper帮 助类,看类名也都知道这个类是用来帮我们生成URL在ASP.NET MVC应用程序中.让我们来看看该类给我们带来了哪些方便的 ...
- Sublime Text2
Ctrl+L选择整行(按住-继续选择下行) Ctrl+KK 从光标处删除至行尾 Ctrl+Shift+K 删除整行 Ctrl+Shift+D 复制光标所在整行,插入在该行之前 Ctrl+J 合并行(已 ...
- QT小技巧—更好管理项目(增加预编译头文件,并且指定moc文件的生成位置)good
预编译加速编译 QT也可以像VS那样使用预编译头文件来加速编译器的编译速度.首先在.pro文件中加入: CONFIG += precompiled_header 然后定义需要预编译的头文件: PREC ...
- Delphi中取整函数Round的Bug解决
Delphi中 Round函数有个Bug一旦参数是形如 XXX.5这样的数时如果 XXX 是奇数 那么就会 Round up如果 XXX 是偶数 那么就会 Round down例如 Round(17. ...
- vi 替换命令“找不到模式”解决
在linux vi编辑工具中使用替换命令操作时,会出现明明有匹配查找模式的数据.却报"找不到模式"问题. 原因是vi s///替换操作缺省针对行,若要生效,则须要将光标移动到指定行 ...
- 《Java虚拟机原理图解》 1.2.2、Class文件里的常量池具体解释(上)
[last updated:2014/11/27] NO1.常量池在class文件的什么位置? 我的上一篇文章<Java虚拟机原理图解> 1.class文件基本组织结构中已经提到了clas ...
- 各浏览器对 window.open() 的窗口特征 sFeatures 参数支持程度存在差异
标准参考 无. 问题描述 使用 window.open 方法可以弹出一个新窗口,其中 open 方法的 sFeatures 参数选项在各浏览器中支持程度不一,这有可能导致同样的代码使各浏览器中弹出窗口 ...
- iOS开发常识
一.NSString 创建字符串. NSString *astring = @"This is a String!"; 创建空字符串,给予赋值. NSString *astri ...
- SharePoint Secure Store Service(SSSS)的使用(一)
SSS在案例中的应用: SSS介绍 SSS部署 SSS应用 http://www.cnblogs.com/renzh/archive/2013/03/31/2990280.html 创建.部署.应用S ...
- 记一次排查log4net 不输出日志的解决过程
最近发现log4net 不输出日志了,重点排查几个地方,发现都没有问题. 1.[assembly: log4net.Config.XmlConfigurator(ConfigFile = " ...