不知道为什么比赛的时候一直想着用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 搜索的更多相关文章

  1. 浙江大学2015年校赛B题 ZOJ 3861 Valid Pattern Lock

    这道题目是队友写的,貌似是用暴力枚举出来. 题意:给出一组数,要求这组数在解锁的界面可能的滑动序列. 思路:按照是否能够直接到达建图,如1可以直接到2,但是1不能直接到3,因为中间必须经过一个2. 要 ...

  2. zoj.3865.Superbot(bfs + 多维dp)

    Superbot Time Limit: 2 Seconds      Memory Limit: 65536 KB Superbot is an interesting game which you ...

  3. 2013年山东省赛F题 Mountain Subsequences

    2013年山东省赛F题 Mountain Subsequences先说n^2做法,从第1个,(假设当前是第i个)到第i-1个位置上哪些比第i位的小,那也就意味着a[i]可以接在它后面,f1[i]表示从 ...

  4. ACM-ICPC 2019南昌网络赛F题 Megumi With String

    ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...

  5. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...

  6. 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 ...

  7. ZOJ 3814 Sawtooth Puzzle (2014年牡丹江赛区网络赛F题)

    1.题目描写叙述:点击打开链接 2.解题思路:本题是一道隐式图的搜索题目.一般来说,这类题目首先要定义状态,接下来是弄清楚状态怎样转移,以及状态怎样判重,怎样推断当前状态是否和目标状态同样.至于求解最 ...

  8. (中等) Hiho 1232 Couple Trees(15年北京网络赛F题),主席树+树链剖分。

    "Couple Trees" are two trees, a husband tree and a wife tree. They are named because they ...

  9. 上海高校金马五校赛 F题:1 + 2 = 3?

    链接:https://www.nowcoder.com/acm/contest/91/F来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言26214 ...

随机推荐

  1. centos7 install magento

    http://mirrors.yun-idc.com/epel/7/x86_64/e/epel-release-7-2.noarch.rpmrpm --import /etc/pki/rpm-gpg/ ...

  2. Understanding and Selecting a SIEM/LM: Correlation and Alerting

    Continuing our discussion of core SIEM and Log Management technology, we now move into event correla ...

  3. 如何给变量取个简短且无歧义的名字(转) good

    湾区日报上分享的一篇文章,文章的作者在Google设计Dart语言,就变量命名方面给了4点建议,文中也列出了好变量名.坏变量名的对比.不管作者的看法与你实际中的命名习惯是否一致,看完这篇文章,相信可以 ...

  4. JavaEE Tutorials (9) - 运行持久化示例

    9.1order应用118 9.1.1order应用中的实体关系119 9.1.2order应用中的主键121 9.1.3实体映射到多个数据库表125 9.1.4order应用中的层叠操作125 9. ...

  5. 【Java线程】Lock、Condition

    http://www.infoq.com/cn/articles/java-memory-model-5  深入理解Java内存模型(五)——锁 http://www.ibm.com/develope ...

  6. Gimp制作圆角透明图片

    用蒙版制作圆角透明图片,步骤如下: 1,用Gimp(2.8版本)打开图片 2,在图层窗口右键当前图层创建蒙版 3,选择蒙版类型黑色(全透明) 4,结果如下 5,用圆角矩形选择工具选择图片,设置圆角半径 ...

  7. PHP程序猿必须学习的第二课——站点安全问题预防

    作为PHP程序猿.第一课我们学习了基本的语法.那么在熟悉基本的语法之后我们应该学些什么呢?我觉得是安全问题.安全问题基于一个站点宛如基石,一着不慎,意味着灾难性的事故. 这里主要就提三点最简单,也是最 ...

  8. 4个常用的HTTP安全头部

    转自:http://blog.jobbole.com/60143/ 它曾是世界性图书馆梦的开始,现在它是全球知识的聚集地,它是目前最流行的,人们将应用都部署之上的万维网. 它是敏捷的代表,它不是单一的 ...

  9. js 解析XML 在Edge浏览器下面 无法准确读到节点属性值

    js 解析XML 在Edge浏览器下面 无法准确读到节点属性值 Dom.documentElement.childNodes[j].attributes[2]  这个是大众写法 在win10的edge ...

  10. js中的setTimeout和setInterval

    在html页面中要使用自动刷新功能时,可以是使用js中setTimeout和setInterval: 一.使用方法 setTimeout的使用setTimeout('要调用的Js方法', 调用的延迟时 ...