【HDOJ】2364 Escape
bfs.题目做的不细心,好多小错误。尤其注意起始点就是边界的情况。wa了八次。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define MAXN 85 typedef struct node_st {
int x, y;
int d, s;
node_st() {}
node_st(int xx, int yy, int dd, int ss) {
x = xx; y = yy; d = dd; s = ss;
}
} node_st; char map[MAXN][MAXN];
bool visit[][MAXN][MAXN];
int direct[][] = {-,,,,,-,,};
int n, m; int bfs(int sx, int sy) {
int i, j, nx, ny;
queue<node_st> que;
node_st node;
bool flag; if (sx== || sx==n- || sy== || sy==m-)
return ;
memset(visit, false, sizeof(visit));
visit[][sx][sy] = visit[][sx][sy] = visit[][sx][sy] = visit[][sx][sy] = true; for (i=; i<; ++i) {
nx = sx + direct[i][];
ny = sy + direct[i][];
if (nx< || nx>=n || ny< || ny>=m)
continue;
if (map[nx][ny] != '#') {
que.push(node_st(nx, ny, i, ));
visit[i][nx][ny] = true;
}
} while ( !que.empty() ) {
node = que.front();
que.pop();
flag = true;
// node.d = 0/1 -> south/north
// node.d = 2/3 -> east/west
j = (node.d&) ? :;
for (i=j; i<j+; ++i) {
nx = node.x + direct[i][];
ny = node.y + direct[i][];
if (nx< || nx>=n || ny< || ny>=m)
continue;
if (map[nx][ny] != '#') {
flag = false;
if (!visit[i][nx][ny]) {
if (nx== || nx==n- || ny== || ny==m-)
return node.s+;
que.push(node_st(nx, ny, i, node.s+));
visit[i][nx][ny] = true;
}
}
}
if (flag) {
i = node.d;
nx = node.x + direct[i][];
ny = node.y + direct[i][];
if (nx< || nx>=n || ny< || ny>=m)
continue;
if (map[nx][ny]=='#' || visit[i][nx][ny])
continue;
if(nx== || nx==n- || ny== || ny==m-)
return node.s+;
que.push(node_st(nx, ny, i, node.s+));
visit[i][nx][ny] = true;
}
}
return -;
} int main() {
int t, x, y;
int i, j; scanf("%d", &t); while (t--) {
scanf("%d %d", &n, &m);
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j)
if (map[i][j] == '@') {
x = i;
y = j;
}
}
printf("%d\n", bfs(x, y));
} return ;
}
【HDOJ】2364 Escape的更多相关文章
- 【HDOJ】1813 Escape from Tetris
bfs预处理一点到边界的最小距离,IDA*求出可行方案.注意按字典序初始化dir数组.并且存在中间点全为1,边界含0的可能性(wa了很多次).此时不输出任何命令. /* 1813 */ #includ ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
随机推荐
- [转] Android LocalService与RemoteService理解
前段时间被别人问到相关的问题,没有回答对,发现自己原来理解的有偏差,最近看了下,写了个小Demo实验了下,现在将其记录下来,以后千万别犯同样的错误就好了. 一.LocalService(本地服务) 不 ...
- 基于GPUImage的实时美颜滤镜
1.背景 前段时间由于项目需求,做了一个基于GPUImage的实时美颜滤镜.现在各种各样的直播.视频App层出不穷,美颜滤镜的需求也越来越多.为了回馈开源,现在我把它放到了GitHub https:/ ...
- iOS流布局UICollectionView使用FlowLayout进行更灵活布局
一.引言 前面的博客介绍了UICollectionView的相关方法和其协议中的方法,但对布局的管理类UICollectionViewFlowLayout没有着重探讨,这篇博客介绍关于布局的相关设置和 ...
- struts2 convention-plugin实现零配置
零配置并不是没有配置,而是通过约定大于配置的方式,大量通过约定来调度页面的跳转而使得配置大大减少.使得Action等配置不必写在Struts.xml中. convention-plugin的约定 1. ...
- 洛谷比赛 堕落的Joe
/*暴力50*/ #include<iostream> #include<cstdio> #include<cstring> #define maxn 100010 ...
- 给Sublime Text2安装轻量级代码提示插件:SublimeCodeIntel
步骤: 1.下载SublimeCodeIntel(地址https://github.com/SublimeCodeIntel/SublimeCodeIntel): 2.将下载的压缩包解压,并放置在Pa ...
- HTML5小游戏源码收藏
html5魅族创意的贪食蛇游戏源码下载 html5网页版打砖块小游戏源码下载 html5 3D立体魔方小游戏源码下载 html5网页版飞机躲避游戏源码下载 html5三国人物连连看游戏源码下载 js ...
- HTML5 canvas createRadialGradient()放射状/圆形渐变
定义和用法 createLinearGradient() 方法创建放射状/圆形渐变对象. 渐变可用于填充矩形.圆形.线条.文本等等. 提示:请使用该对象作为 strokeStyle 或 fillSty ...
- power desinger 学习笔记<六>
原帖地址:http://blog.csdn.net/spt110/article/details/8640849 PowerDesigner中Table视图同时显示Code和Name,像下图这样的效果 ...
- javascript基础学习(十)
javascript之数组 学习要点: 数组的介绍 定义数组 数组元素 数组的方法 一.数组的介绍 数组中的元素类型可以是数字型.字符串型.布尔型等,甚至也可以是一个数组. 二.定义数组 1.通过数组 ...