浙江大学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 ...
随机推荐
- hdu 3333 Turing Tree
题目链接 给n个数, m个询问, 每次询问输出区间内的数的和, 相同的数只计算一次. 数组里的数是>-1e9 <1e9, 可以把它离散以后用莫队搞... #include <iost ...
- java生成随机字符串
学习java comparable特性时候,定义如下Student类,需要需要随机添加学生姓名以及学号和成绩,这是java如何随机生成名字,根据我的查询,我找到目前java库支持两种方法. 1. or ...
- HDU 1090 A+B for Input-Output Practice (II)
#include <cstdio> int main() { int n,a,b; scanf("%d",&n); ; i<=n; i++) { scan ...
- c++ cout 保留小数点位
需要头文件 <iomanip> 输出时需要用 fixed 和 setprecision() fixed代表输出浮点数,setprecision()设置精度. #include <io ...
- 全栈JavaScript之路(七)学习 Comment 类型节点.
凝视 在DOM中,用 Comment 类型 节点表示, 构造器函数为: function Comment(){[native code]}. comment 节点的特征: nodeType:8 no ...
- 前端性能监控系统ShowSlow
作者:zhanhailiang 日期:2014-11-14 1. 简单介绍 ShowSlow是开源的前端性能监控系统,提供了下面功能: 前端性能指标数据收集功能:ShowSlow原生提供了数据收集工具 ...
- 【剑指offer】调整数组顺序
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25829395 剑指offer上的第14题,九度OJ为了确保输出的结果的唯一性,在输出上做了 ...
- windows下使用python googleprotobuf
首先下载:protobuf-2.5.0.tar.gz 和protoc-2.5.0-win32.zip.两者的版本要对应: 将下载的google protobuf解压,会看到一个python目录,Win ...
- iOS中的 SB和XIB的前世今生
今天给大家介绍一下Apple开发中三种几种常用的应用程序编写方式:纯代码创建.使用storyboard/XIB.我们都知道,纯代码编写模式适合大型项目大规模使用,利于版本管理.追踪改动以及代码合并,代 ...
- C# Coding & Naming Conventions
Reference document https://msdn.microsoft.com/en-us/library/ff926074.aspx https://msdn.microsoft.com ...