【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 ...
随机推荐
- eclipse删除空行
1.打开源码编辑器 2.使用快捷键Ctrl+f 3.在Find输入框中输入:^\s*\n 4.Replace With输入框的值为空 5.在[Options]选中的"Regular expr ...
- PowerDesigner15的安装和破解
一.PowerDesigner15的安装 运行安装包,出现如下安装界面
- DataTbale取值
有一个DataTable数据 //创建DataTable对象 DataTable dt = new DataTable("Table_AX"); //为DataTable创建列 / ...
- HTML5 Canvas实现刮刮卡效果实例
HTML: <style> #canvas { border: 1px solid blue; position: absolute; left: 10px; top: 10px; bac ...
- 手势交互之GestureOverlayView
一种用于手势输入的透明覆盖层,可以覆盖在其他空间的上方,也可包含在其他控件 android.gesture.GestureOverlayView 获得手势文件 需要用GesturesBuilder,如 ...
- sql数值显示成千分位分隔符的形式
), )--带小数点 ), ),'.00','')--不带小数点
- NSNumber 转 NSString
之前number 转string时候调用stringValue,后来发现未完全转 NSNumber * a_num = [NSNumber numberWithInteger: ]; NSString ...
- java基础之抽象类与接口的区别
在学习java的过程中,或者是在找工作笔试或面试的时候,如果你是java岗位,那么抽象类与接口的区别无疑是一个大热点,是各大公司都想要考的一个小知识点,下面是我为了9月份秋招自己总结的,若有不对的地方 ...
- 《du命令》-linux命令五分钟系列之三
本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linux大棚博客的运行和发展.请见“关于捐款” == ...
- html5本地数据库(一)
本地数据库 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important ...