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. .net的 async 和 await

    async 和 await 出现在C# 5.0之后,关系是两兄弟,Task是父辈,Thread是爷爷辈,这就是.net 多线程处理的东西,具体包括 创建线程,线程结果返回,线程中止,线程中的异常处理 ...

  2. SQLServer 在Visual Studio的连接方法

    一.Sql Server 在Visual Studio的连接有两种方法: (1)本地计算机连接; [c#] view plaincopy     string s = "Data Sourc ...

  3. redhat5.8无法进入图形界面

    解决办法: 删除/etc/X11/xorg.conf文件

  4. transfrom属性

    transfrom可以实现一些形变.常见的有平移.缩放和旋转三种.使用起来很简单: //横纵放大1.3倍 self.imageButton.transform=CGAffineTransformSca ...

  5. java新手笔记12 单例

    1.单例 package com.yfs.javase; public class Singleton { //private static final Singleton single = new ...

  6. Codevs 1138 聪明的质监员 2011年NOIP全国联赛提高组

    1138 聪明的质监员 2011年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 小 T 是一名质量监督员, ...

  7. python 在调用时计算默认值

    大家都知道python的默认值是在函数定义时计算出来的, 也就是说默认值只会计算一次, 之后函数调用时, 如果参数没有给出,同一个值会赋值给变量, 这会导致, 如果我们想要一个list默认值, 新手通 ...

  8. 解决读写properties属性文件

    package com.kzkj.wx.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileIn ...

  9. html5 canvas绘制圆形印章,以及与页面交互

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. Boolean 布尔类型详解

    这是最简单的类型.boolean 表达了真值,可以为 TRUE 或 FALSE.两个都不区分大小写. 要明确地将一个值转换成 boolean,用 (bool)或者 (boolean) 来强制转换.但是 ...