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的更多相关文章

  1. 【HDOJ】1813 Escape from Tetris

    bfs预处理一点到边界的最小距离,IDA*求出可行方案.注意按字典序初始化dir数组.并且存在中间点全为1,边界含0的可能性(wa了很多次).此时不输出任何命令. /* 1813 */ #includ ...

  2. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  3. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  4. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  5. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  6. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  7. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  8. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  9. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

随机推荐

  1. Execution Contexts (执行上下文)

    本章我们一起讨论一下ECMAScript的执行上下文及相关可执行代码的各种类型.so...什么是执行上下文?我们来看看定义: 每次当控制器转到ECMAScript可执行代码的时候, 即会进入到一个执行 ...

  2. 分享一下自己在用的CSS样式重置代码

    通过借鉴网上大牛们的经验和自己在工作中碰到的一些问题,总结出了这些比较常用的CSS样式重置的代码: @charset "utf-8"; /* 防止用户自定义背景颜色对网页的影响,添 ...

  3. java web(jsp)-The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    在静态项目上新建 jsp文件的时候,报错:The superclass "javax.servlet.http.HttpServlet" was not found on the ...

  4. PHP代码批量加密

    <?php error_reporting(E_ALL); ini_set('display_errors','1'); //批量加密码当前目录 $dirnow = getcwd(); $dir ...

  5. CentOS 7重装mysql编译过程报错解决方法

    错误记录: [ 82%] Building C object libmysql/CMakeFiles/clientlib.dir/__/sql-common/client.c.o/usr/local/ ...

  6. Linux下安装Nginx1.9.3-0303(本人亲手实践)

    Linux下安装Nginx1.9.3 Linux操作系统 Oel 5.8 64bit 最新版Nginx: 1.9.3 最近同事让我帮忙搞 ngix,两天时间 安装.配置搞定了.继续 Nginx 1.9 ...

  7. 【原创】ZeroClipboard的时代或许已经过去了

    曾经,一个网页上要用Javascript实现网页内容的复制,其实是很麻烦的一件事情.虽然在这个问题上IE有其高大上的 window.clipboardData 方法支持直接复制指定内容,Firefox ...

  8. Java Swing 使用总结(转载)

    随笔转载自:此去经年ぢ 地址:http://www.cnblogs.com/FLFL/p/5369756.html 1.     GUI编程引言 以前的学习当中,我们都使用的是命令交互方式: 例如:在 ...

  9. pthread_rwlock_t读写锁函数说明

    读写锁 索引: 初始化一个读写锁pthread_rwlock_init 读锁定读写锁      pthread_rwlock_rdlock 非阻塞读锁定 pthread_rwlock_tryrdloc ...

  10. Struts2中的链接标签 <s:url>和<s:a>---在action中获取jsp表单提交的参数(转)

    转自:http://sgl124764903.iteye.com/blog/444183 1.普通链接 Web程序中最普通的应用是链接到其他页面,下面看Welcome.jsp. <%@ page ...