不仅仅是DFS,还需要考虑可以走到终点。同时,需要进行预处理。至多封闭点数为起点和终点的非墙壁点的最小值。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std; typedef struct node_st {
int x, y, t, flg;
node_st() {}
node_st(int xx, int yy, int tt, int fflg) {
x=xx; y=yy; t=tt; flg=fflg;
}
} node_st; char map[][];
char visit[][][];
int direct[][] = {{-,},{,},{,-},{,}};
int n, m, time, fmin;
int begx, begy, endx, endy; bool check(int x, int y) {
if (x< || x>=n || y< || y>= m)
return false;
return true;
} bool bfs() {
queue<node_st> que;
int x, y, t, flg;
int i;
node_st node; memset(visit, , sizeof(visit));
que.push(node_st(begx, begy, , ));
visit[begx][begy][] = ; while ( !que.empty() ) {
node = que.front();
que.pop();
t = node.t;
if (t == time)
break;
for (i=; i<; ++i) {
x = node.x + direct[i][];
y = node.y + direct[i][];
flg = node.flg;
if ( !check(x, y) )
continue;
if (map[x][y] != '#') {
if (map[x][y] == 'E' && flg==)
return true;
if (map[x][y] == 'J')
flg = ;
if (t<time && !visit[x][y][flg]) {
visit[x][y][flg] = ;
que.push(node_st(x, y, t+, flg));
}
}
}
} return false;
} void printmap() {
for (int i=; i<n; ++i)
printf("\t%s\n", map[i]);
} int dfs(int t, int cur) {
int flg = ;
char ch; if (t >= fmin)
return ; for (int i=; i<n; ++i) {
for (int j=; j<m; ++j) {
if (map[i][j]=='.' || map[i][j] == 'J') {
ch = map[i][j];
map[i][j] = '#';
if (cur == t) {
//printmap();
if ( !bfs() ) {
fmin = (t<fmin) ? t:fmin;
return ;
}
}
if (cur < t)
flg = dfs(t, cur+);
map[i][j] = ch;
}
if (flg)
return ;
}
} return ;
} int pre() {
int i, x, y, tmp1, tmp2; tmp1 = ;
for (i=; i<; ++i) {
x = begx + direct[i][];
y = begy + direct[i][];
if ( check(x, y) && (map[x][y]=='.'||map[x][y]=='J'))
tmp1++;
} tmp2 = ;
for (i=; i<; ++i) {
x = endx + direct[i][];
y = endy + direct[i][];
if ( check(x, y) && (map[x][y]=='.'||map[x][y]=='J'))
tmp2++;
} return tmp1<tmp2 ? tmp1 : tmp2;
} int main() {
int case_n;
int i, j; scanf("%d", &case_n); while (case_n--) {
scanf("%d %d %d%*c", &n, &m, &time);
for (i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j) {
if (map[i][j] == 'S') {
begx = i;
begy = j;
}
if (map[i][j] == 'E') {
endx = i;
endy = j;
}
}
}
if ( !bfs() ) {
printf("0\n");
continue;
}
fmin = pre();
//printf("pre:%d\n", fmin);
dfs(,);
//printf("1:%d\n", fmin);
dfs(,);
//printf("2:%d\n", fmin);
dfs(,);
printf("%d\n", fmin);
} return ;
}

【HDOJ】1983 Kaitou Kid - The Phantom Thief (2)的更多相关文章

  1. HDOJ/HDU 1982 Kaitou Kid - The Phantom Thief (1)(字符串处理)

    Problem Description Do you know Kaitou Kid? In the legend, Kaitou Kid is a master of disguise, and c ...

  2. HDU 1983 Kaitou Kid - The Phantom Thief (2)

    神题,搜索太差,来自网络的题解与程序 思路: 封锁出口或者入口周围的格子. 最多需要4个封锁点. 所以我们可以采取这样的策略: 1.寻找一条盗贼的可行路线,如果没有,返回0. 2.计算封锁出口和入口四 ...

  3. HDU——1982Kaitou Kid - The Phantom Thief (1)(坑爹string题)

    Kaitou Kid - The Phantom Thief (1) Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/327 ...

  4. 【HDOJ】4729 An Easy Problem for Elfness

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

  5. 【HDOJ】【3506】Monkey Party

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

  6. 【HDOJ】【3516】Tree Construction

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

  7. 【HDOJ】【3480】Division

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

  8. 【HDOJ】【2829】Lawrence

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

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

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

随机推荐

  1. Jquery Table添加行、删除行

    html页面代码 <table id="tblUserInfo"> </table> Js代码 function DealUserInfo(qty){ ) ...

  2. ASP.NET 预编译

    ASP.NET 预编译概述 https://msdn.microsoft.com/zh-cn/library/bb398860%28v=VS.90%29.aspx 如何:预编译 ASP.NET 网站以 ...

  3. 动态规划&矩阵连乘

    动态规划&矩阵连乘 动态规划的概念 •     与分治方法类似       分-治-合 • 与分治方法不同       子问题之间并非相互独立 •     基本思想        用一个表记录 ...

  4. cleartool mkview snapshot windows

    mkview 用法详解:mkview - Support - IBM 创建View的命令相对来讲十分直截了当. cleartool mkview -snapshot -tag ViewName -vw ...

  5. 使用sui实现的选择控件【性别、日期、省市级联】

    使用sui mobile做的选择控件,其中sm.js有修改,增加自定义api,详情请看index.html的注释,不多说了,上代码 <!DOCTYPE html> <html> ...

  6. jquery的插件机制

    jQuery的内核; (function( window, undefined ) { //这就是jQuery的原型 var jQuery = function( selector, context ...

  7. javaScript中with的用法

    1 JavaScript中的with语句的作用是为逐级的对象访问提供命名空间式的速写方式, 也就是在指定的代码区域, 直接通过节点名称调用对象 初次接触到with用法,是这样一段代码: 1 2 3 4 ...

  8. HTML5格式化

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. php异步请求(可以做伪线程)

    $fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30); stream_set_blocking($fp,0);     ...

  10. Improved Semantic Representations From Tree-Structured Long Short-Term Memory Networks(1)

    今天和陈驰,汪鑫讨论了一下,借此记录一下想法. 关于这篇论文,要弄清的地方有: 1.LSTMtree到底是从上往下还是从下往上学的,再确认一下 2.关于每个节点的标注问题 3.label的值到底该怎么 ...