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. Lucene.net常用功能说明

    Lucene.net是一个.net下的全文检索类库.配置简单,功能丰富,比较成熟.我在项目中用Lucene.net有一段时间了,这里我把常用一些功能写出来,与大家一起分享. Lucene.net用的是 ...

  2. ASP.net程序在本地操作正确,新电脑不正确的处理经验

    一.可能是计算机操作系统位数不兼容的问题,如下处理后即可.

  3. 【JAVA错误笔记】 - Unable add facets project AnnotationWebService CXF 2-x Web Services

    错误描述: 创建webservice接口服务时候提示: Unable add facets project AnnotationWebService CXF 2-x Web Services Unab ...

  4. LINQ2EF-LINQ2SQL-LINQ笔记

    例1:In SQL: AND dep_all_code LIKE '" + depAll + "' AND dep_code in (SELECT DISTINCT DEP3 FR ...

  5. iOS-开发日志-UITextView介绍

    UITextView 属性 1.     text: 设置textView中文本 _textView.text = @"Now is the time for all good develo ...

  6. Javascript三元条件运算符

    今天谈一个小知识点,三元运算符.三元运算,顾名思义会有三个要素,表达式的大致组成为condition ? expr1 : expr2:一个语句加两个表达式.问号之前为判断语句.如果为真,则执行第一个表 ...

  7. 10.15_SVG可以解决问题吗

    (1)淘宝开放平台. (2)Teiid是一个数据虚拟化系统.Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架.Apache Jackrabbit. (3)SVG:百度百科.SVG.js .Sn ...

  8. ActiveMQ基本配置与示例演示

    一.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 二.运行ActiveMQ 将apache-activemq-5.11.1-bin.zip解压,由于本系 ...

  9. Android学习7--日志信息的使用

    在代码的调试中,日志信息是必不可少的,关于日志信息的使用,接下来,小编简要的说明一下: 日志分为几种:Verbose:从字面上解释,verbose为冗长的,啰嗦的,所以verbose日志信息输出的内容 ...

  10. js判断是否全是相同的字符串

    isSameStr("aa2a") //不都是相同的字符 function isSameStr(str){ var tem=0; for(var i=0;i<str.leng ...