大多数刚需封锁4区域可以,DFS地区封锁。BFS无论是通过

#include "stdio.h"
#include "string.h"
#include "queue"
using namespace std; int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int s_x,s_y,n,m,t;
char str[11][11]; struct node
{
int x,y,step,key;
};
int bfs()
{
queue<node>q;
node cur,next;
int i;
int hash[11][11][2]; cur.x=s_x;
cur.y=s_y;
cur.step=0;
cur.key=0;
q.push(cur);
memset(hash,0,sizeof(hash));
hash[s_x][s_y][0]=1; while (!q.empty())
{
cur=q.front();
q.pop();
if (cur.step>=t) return -1; for (i=0;i<4;i++)
{
next.x=cur.x+dir[i][0];
next.y=cur.y+dir[i][1];
if (next.x<0 || next.y<0 || next.x>=n || next.y>=m) continue;
if (str[next.x][next.y]=='#') continue;
next.step=cur.step+1;
next.key=cur.key;
if (str[next.x][next.y]=='J')
next.key=1;
if (hash[next.x][next.y][next.key]==1) continue;
hash[next.x][next.y][next.key]=1;
q.push(next);
if (str[next.x][next.y]=='E' && next.key==1) return 1;
}
}
return -1;
} int dfs(int k)
{
int i,j;
char ch;
if (k==0)
return bfs(); for (i=0;i<n;i++)
for (j=0;j<m;j++)
if (str[i][j]=='J' || str[i][j]=='.')
{
ch=str[i][j];
str[i][j]='#';
if (dfs(k-1)==-1)
return -1;
str[i][j]=ch;
}
return 1;
}
int main()
{
int Case,i,j;
scanf("%d",&Case);
while (Case--)
{
scanf("%d%d%d",&n,&m,&t);
for (i=0; i<n; i++)
{
scanf("%s",str[i]);
for (j=0; j<m; j++)
if(str[i][j]=='S')
{
s_x=i;
s_y=j;
break;
}
} if (bfs()==-1)
{
printf("0\n");
continue;
} if (dfs(1)==-1)
printf("1\n");
else
if (dfs(2)==-1)
printf("2\n");
else
if (dfs(3)==-1)
printf("3\n");
else
printf("4\n");
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 1983 BFS&amp;&amp;DFS的更多相关文章

  1. hdu 1983(BFS+DFS) 怪盗Kid

    http://acm.hdu.edu.cn/showproblem.php?pid=1983 首先,题目要求出口和入口不能封闭,那么,只要把出口或入口的周围全给封闭了那盗贼肯定无法成功偷盗,出口或入口 ...

  2. hdu 1044 BFS(压缩图)+DFS

    题意:              给你起点,终点,图上有墙有路还有宝物,问你在规定时间内能否能到终点,如果能问最多能捡到多少宝物. 思路:           看完这个题目果断 BFS+三维的mark ...

  3. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  4. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  5. UVA10410-Tree Reconstruction(BFS序和DFS序的性质)

    Problem UVA10410-Tree Reconstruction Accept:708  Submit:4330 Time Limit: 3000 mSec Problem Descripti ...

  6. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  7. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  8. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  9. HDU 3313 Key Vertex(dfs + bfs)

    HDU 3313 Key Vertex 题目链接 题意:一个有向无环图.求s,t之间的割点 思路:先spfa找一条最短路出来,假设不存在.就n个都是割点. 然后每次从s进行dfs,找到能经过最短路上的 ...

随机推荐

  1. 【转】window.scroll 浏览器滚动条的参数总结

    如内容超出单元格,则隐藏style="TABLE-LAYOUT: fixed" 让弹出窗口总是在最上面: <body onblur="this.focus();&q ...

  2. [LeetCode] Longest Palindrome Substring 具体分析

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. MVC中下拉框显示枚举项

    原文:MVC中下拉框显示枚举项 本篇将通过3种方式,把枚举项上的自定义属性填充到下拉框: 1.通过控制器返回List<SelectListItem>类型给前台视图 2.通过为枚举类型属性打 ...

  4. linux 终端下敲ctrl-c时,到底发生了什么?(转)

    通过telnet登录到单板,然后按ctrl-c会发生什么情况,流程是怎么样的? 在分析之前,先介绍tty的相关知识.我们可以认为,所有跟输入输出相关的操作,最终都由tty来接管.举例来说,当我们敲 l ...

  5. 补间动画实现(tween)

    1.补间动画的概念: 补间动画:仅仅须要开发人员设置好动画的開始与结束的关键帧 中间帧有喜用计算机补齐. 2.种类:分为4种: ①alpha 透明度 ②alpha 透明度 ③translate 位置移 ...

  6. Writing your first Django app, part 1(转)

    Let’s learn by example. Throughout this tutorial, we’ll walk you through the creation of a basic pol ...

  7. polay定理总结

    参考资料:url=YveSCnlx_dlXo4oGjE8w3_2QRObRXvXMbix81ppCxBGqBswxffwDN9frzY5xxaKf7GgEMY8K93JTPEhkBzY66XOaN0G ...

  8. Android 布局学习之——Layout(布局)具体解释二(常见布局和布局參数)

     [Android布局学习系列]   1.Android 布局学习之--Layout(布局)具体解释一   2.Android 布局学习之--Layout(布局)具体解释二(常见布局和布局參数)   ...

  9. HDU 5037 FROG (贪婪)

    Problem Description Once upon a time, there is a little frog called Matt. One day, he came to a rive ...

  10. linux_后台启动多个java -jar 程序,及关闭

    启动脚本 startup.sh #!/bin/bash x=$(($)) )) do java -jar /home/chenpenghui/crawler/crawler-hb/StartUpIp. ...