【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 ...
随机推荐
- Enable Access Logs in JBoss 7 and tomcat--转
JBoss 7 is slightly different than earlier version JBoss 5 or 6. The procedure to enable access logs ...
- Java基础知识强化之集合框架笔记43:Set集合之TreeSet存储Integer类型的元素并遍历
1. TreeSet类概述: • 能够对元素按照某种规则进行排序. • 或者根据创建set时提供的Comparator进行排序 • 具体取决于使用的构造方法 2. 代码示例: package cn.i ...
- WPF DataGrid某列使用多绑定后该列排序失效,列上加入 SortMemberPath 设置即可.
WPF DataGrid某列使用多绑定后该列排序失效 2011-07-14 10:59hdongq | 浏览 1031 次 悬赏:20 在wpf的datagrid中某一列使用了多绑定,但是该列排序失 ...
- c# 为什么要用 get set 属性
1 可以对赋值 做验证 ,范伟限制,额外的限制 2 可以设置 只读 只写 3 可以做线程同步 4 可以将属性设置在interface接口中 5 可以使用虚属性 或 抽象属性 可以填补 没有 虚字段 抽 ...
- CentOS 7重装mysql编译过程报错解决方法
错误记录: [ 82%] Building C object libmysql/CMakeFiles/clientlib.dir/__/sql-common/client.c.o/usr/local/ ...
- NSDate和NSString的转换及判定是昨天,今天,明天
用于uidate,picker.. +(NSDate*) convertDateFromString:(NSString*)uiDate{ NSDateFormatter *formatter ...
- iOS_ruby环境的配置
AC机中安装RUBY环境 转自:http://www.cnblogs.com/foxting/p/4520829.html 在安装CocoaPods之前要先配置好RUBY环境,本文就怎么安装RUBY ...
- javascript基础学习(六)
javascript之对象 学习要点: 对象的属性和方法 对象的原型 一.对象 对象其实就是一种引用类型,而对象的值就是引用对象的实例. 二.创建对象 在javascript中有两种对象,一种是系统内 ...
- Servlet(一)
BS架构的优势 1.数据库之负责数据库的管理 2.Web服务器负责业务逻辑的处理 3.浏览器提供操作界面 4.不需要单独安装客户端 5.开发相对于CS简单,客户端和服务器的通信模块都是使用标准的HTT ...
- Spring中的创建与销毁
在bean中添加属性init-method="方法名" destroy-method="方法名" init-method 该方法是由spring容 ...