大多数刚需封锁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. 如何成为游戏的生产者——第二章:如何开始你的编程(开发环境的搭建、C++语言适应)

    如何成为游戏的生产者--文章二章:怎样開始你的编程 小故事:上节说到我六年级打开了那本C语言的书,然后其实我还是没看懂.好像看懂了一些printf语句.之后遇到了史无前例的困难--怎么让代码执行起来. ...

  2. [ACM] POJ 2342 Anniversary party (树DP获得冠军)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4410   Accepted: 2496 ...

  3. 如何成功实施SDL提供的官方Android平台Demo

    如何成功实施SDL提供的官方Android平台Demo 作者:雨水  日期:2014-4-30 编写说明:SDL的官方提供了一个Anroid的demo模板SDLActivity,无法直接执行,依照官方 ...

  4. SQL Server 数据库没有有效全部者的三种解决的方法

    问题:     开发的过程中,操作系统出了问题.决定重装系统.可是没有将SQL Server中的数据库文件分离出来,直接将系统格了.在新系统数据库中附加了数据库文件,一切还算正常.但当打开数据库关系图 ...

  5. ash

    查看当前用户使用的Shell,echo $SHELL BusyBox ('01-current) The BusyBox distribution is aiming for small implem ...

  6. (大数据工程师学习路径)第四步 SQL基础课程----SQL介绍及mysql的安装

    一.数据库和SQL介绍 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它的产生距今已有六十多年.随着信息技术和市场的发展,数据库变得无处不在:它在电子商务.银行系统等众多领域都 ...

  7. EXCEL Pivot table manipulate

    Add filter For the Demo time,I would like to filter out the products which not in Red and Black colo ...

  8. 页面加速Chromium 预压 Prerendering

    前者已经推出了一个博客prefetch预加载的网页资源,以提高网页加载速度,下面我们一起来看一下,以加速网页chromium prerendering.介绍prerendering之前.先介绍两个概念 ...

  9. 十天学Linux内核之第五天---有关Linux文件系统实现的问题

    原文:十天学Linux内核之第五天---有关Linux文件系统实现的问题 有时间睡懒觉了,却还是五点多醒了,不过一直躺倒九点多才算起来,昨晚一直在弄飞凌的嵌入式开发板,有些问题没解决,自己电脑系统的问 ...

  10. Swift——(三)Swift神奇的下划线

    在Swift在.有许多神奇的下划线,在这里,我们将看到神奇的汇总,我希望能够帮助其他很多学习Swift朋友. @Author: twlkyao转载或者引用请保留此行. 1.格式化数字字面量     通 ...