不仅仅是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. sharedPreference的奇怪bug

    一定要清楚sp的结构,而且要知道是什么类型的.类型不对,会引起很多不知道的bug,比如本来是int类型的值,如果用String的类型去匹配,会让Activity开Activity Thread,不断地 ...

  2. MVC Filter自定义验证(拦截)

    namespace QS.Web.Extensions { /// <summary> /// 验证session.权限 状态 /// </summary> [Attribut ...

  3. jsp 页面实现增减行

    1.页面加入 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> < ...

  4. C#中运用事件实现异步调用

    问题引出: winform程序中的耗时操作,一般不能在UI线程中执行,需要另开线程.往往我们需要在耗时操作结束后将结果显示在UI上. 以下是Mainform.cs中调用耗时操作的一段代码: Job j ...

  5. JAVA中实现百分比

    import java.text.NumberFormat; public class TeachYou { public static void main(String[] args) { //这里 ...

  6. Flexbox盒子弹性布局

    Can I Use? 2. 概念: 当你给一个元素使用了flexbox模块,那么它的子元素就会指定的方向在水平或者纵向方向排列.这些子元素会按照一定的比例进行扩展或收缩来填补容器的可用空间. < ...

  7. VIM 及正则表达式

    VIM及正则表达式 一.查找/Search + 统计 1.统计某个关键字 方法是:%s:keyword:&:gn. 其中,keyword是要搜索的关键字,&表示前面匹配的字符串,n表示 ...

  8. 【CODECHEF】【phollard rho + miller_rabin】The First Cube

    All submissions for this problem are available. Read problems statements in Mandarin Chinese and Rus ...

  9. rpm命令详解

    http://www.rpm.org/max-rpm/s1-rpm-install-additional-options.html#S2-RPM-INSTALL-REPLACEFILES-OPTION ...

  10. 【原创】Linux opensource-src-4.3.2.tar.gz的安装。

    下载好opensource-src-4.3.2.tar.gz 安装G++等必备库: sudo apt-get install make gcc g++ sudo apt-get install bui ...