bfs,使用ttl进行剪枝。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define MAXNUM 10 int map[MAXNUM][MAXNUM], ttls[MAXNUM][MAXNUM], n, m;
int direct[][] = {{-,},{,},{,-},{,}}; typedef struct node_st {
int ttl;
int t;
int x, y;
node_st() {}
node_st(int ttll, int tt, int xx, int yy) {
ttl = ttll;
t = tt;
x = xx;
y = yy;
}
} node_st; int bfs(int begx, int begy) {
int val = -;
int x=begx, y=begy, ttl=, t=;
int newx, newy, newt, newttl;
queue<node_st> nodes;
node_st node; memset(ttls, , sizeof(ttls));
nodes.push(node_st(ttl,t,x,y));
map[x][y] = ; while ( !nodes.empty() ) {
node = nodes.front();
nodes.pop();
x = node.x;
y = node.y;
ttl = node.ttl;
t = node.t;
if (map[x][y]== && ttl) {
val = node.t;
break;
}
--ttl; ++t;
if (ttl == )
continue;
for (int i=; i<; ++i) {
newx = x + direct[i][];
newy = y + direct[i][];
newttl = ttl;
newt = t;
if (newx< || newx>=n || newy< || newy>=m)
continue;
if (map[newx][newy] == )
continue;
if (map[newx][newy] == ) {
newttl = ;
map[newx][newy] = ;
}
if (newttl > ttls[newx][newy]) {
ttls[newx][newy] = newttl;
nodes.push(node_st(newttl, newt, newx, newy));
}
}
} return val;
} int main() {
int t;
int i, j, begx, begy; scanf("%d", &t); while (t--) {
scanf("%d %d", &n, &m);
for (i=; i<n; ++i) {
for (j=; j<m; ++j) {
scanf("%d", &map[i][j]);
if (map[i][j] == ) {
begx = i;
begy = j;
}
}
}
i = bfs(begx, begy);
printf("%d\n", i);
} return ;
}

【HDOJ】1072 Nightmare的更多相关文章

  1. 【HDOJ】3085 Nightmare Ⅱ

    双向BFS.注意,任何一个点出队后,首先需要考虑ghost. /* 3085 */ #include <iostream> #include <queue> #include ...

  2. 【BZOJ】1072: [SCOI2007]排列perm(状压dp+特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1072 首先无限膜拜题解orz表示只会暴力orz 数据那么小我竟然想不到状压! orz 这种题可以取模 ...

  3. 【HDOJ】4729 An Easy Problem for Elfness

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

  4. 【HDOJ】【3506】Monkey Party

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

  5. 【HDOJ】【3516】Tree Construction

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

  6. 【HDOJ】【3480】Division

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

  7. 【HDOJ】【2829】Lawrence

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

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

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

  9. 【HDOJ】【3530】Subsequence

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

随机推荐

  1. android 中在CMD中查看sqlite

    今天第一次学习Sqlite,在代码中执行了sql,但是不知道在上面地方才能直观的查看sqlite中的数据,下面是查看资料后的找到的查看方法: 通过上述可以从cmd进入数据库查看原文地址:http:// ...

  2. php模板引擎

    http://baike.baidu.com/link?url=HmXfdJBv3zpCdnZPeaSmZmqDBHlyTBnz9Rmb5it-jf1_NLHfaku6_i8ssUYbnaTQEBD4 ...

  3. cognos 10.2.2 report studio数字---字符型查询注意事项

    做了一个简单的报表,就是按照员工编号查询员工,其中员工编号是全数字,我们保存在数据库中的是字符型varchar2(10),所以在report studio中做查询就一直报告服务器错误. 其中使用cas ...

  4. java swing窗口放置屏幕中央问题思考

    java swing窗口放置屏幕中央问题思考 以前总是尝试各种方法都没有能把组件放到屏幕中央,只能用死办法,设置绝对坐标,但这样就失去了可移植性,而且繁琐.今天仔细思考了一番,终于被我找出问题所在. ...

  5. OC - 2.OC基础知识介绍

    一.基础语法 1> OC语言和C语言 C语言是面向过程的语言,OC语言是面向对象的语言 OC语言继承了C语言,并增加了面向对象的思想 以下内容只介绍OC语言与C语言的不同之处 2> 关键字 ...

  6. java之泛型潜在错误

    如果使用带泛型声明的类时,没有传入类型参数,那么这个类型参数默认是声明该参数时指定的第一个上限类型,这个类型参数被称为raw type(原始类型 ). eg:     public class Lis ...

  7. [002] The Perks of Being a Wallflower - 读后记

    The Perks of Being a Wallflower 今天(2015年10月30日 18:26:17)读完"The Perks of Being a Wallflower" ...

  8. 『奇葩问题集锦』Zepto 页面唤醒拨号功能点透

    不废话直接上代码: HTML: <a class="js-tel tel" data-tel="1312414"></a> JS: // ...

  9. jquery禁用a标签,jquery禁用按钮click点击

    jquery禁用a标签方法1 $(document).ready(function () { $("a").each(function () { var textValue = $ ...

  10. MySQL中EXPLAIN解释命令详解

    MySQL中的explain命令显示了mysql如何使用索引来处理select语句以及连接表.explain显示的信息可以帮助选择更好的索引和写出更优化的查询语句. 1.EXPLAIN的使用方法:在s ...