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. 20160314 Request 和Response

    一.Response 1.Resonse的继承结构: ServletResponse--HttpServletResponse 2.Response代表响应,于是响应消息中的 状态码.响应头.实体内容 ...

  2. Java中到底有没有指针;同时注意引用和指针的区别

    Java中引用的作用类似于指针,但是有区别:()    (1) 指针必然指向一个内存地址,如果你定义的时候不指定,就会乱指(很可能造成安全隐患)但是引用定义出来后默认指向为空.     (2) 指针可 ...

  3. C#冒泡排序法程序代码

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace Console ...

  4. 销毁session

    session运行在服务器是单用户,每个session都有一个唯一的sessionid 用法:session.setAttribute("userName", "张三丰& ...

  5. http 请求头

    HTTP Request的Header信息 1.HTTP请求方式 如下表: GET 向Web服务器请求一个文件 POST 向Web服务器发送数据让Web服务器进行处理 PUT 向Web服务器发送数据并 ...

  6. 安装"MySQLdb"一波三折.

    在慕课网学习课程"Python操作MySQL数据库",安装"MySQLdb"时遇到问题. 先是找错地方: 百度搜索"Mysql for Python& ...

  7. PHP CURL参数详解

    curl用法:cookie及post 一.cookie用法 <?php $cookie_jar = tempnam('./tmp','cookie'); // login $c=curl_ini ...

  8. AJAX 简单上手

    AJAX简介 没有AJAX会怎么样?普通的ASP.Net每次执行服务端方法的时候都要刷新当前页面,如实现显示服务器的时间每次都要刷新页面的坏处:页面刷新打断用户操作.速度慢.增加服务器的流量压力.如果 ...

  9. redis 安装及配置

    一.安装Redis 1.到官网下载redis最新版本,我下载的是 http://redis.io/ 2.拷贝redis-3.0.3到/usr/local目录 3.解压缩sudo tar -zxf re ...

  10. rownum

    rownum是一个伪列,oracle数据库会对查找到的数据 从1 开始递增指定每行的rownum值, 当查询条件里有 rownum时(比如 where rownum>2),数据库会依次从数据集里 ...