大多数刚需封锁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. java这些东西发展(1)-------大约ORA00604和ORA12705

    ******************************有关myEclipse和oracle在连接发生的一个问题********************************* 用户界面显示的评 ...

  2. SPOJ 15. The Shortest Path 堆优化Dijsktra

    You are given a list of cities. Each direct connection between two cities has its transportation cos ...

  3. NSIS 自定义页面制作关闭功能

    因工作需要要自定义NSIS的安装页面,其中用到一个功能. 修改的是这个项目:https://github.com/nicecai/nsissource http://hamletsoft.com/ 在 ...

  4. Script:SQL调优健康检查脚本

    Script:SQL调优健康检查脚本 http://www.askmaclean.com/archives/sql-tuning-health-check-script.html 以下脚本可以用于收集 ...

  5. RH253读书笔记(7)-Lab 7 Electronic Mail

    Lab 7 Electronic Mail Goal: To build common skills with MTA configuration Estimated Duration: 90 min ...

  6. ViewPager实现页面切换

    先贴效果图(每个开关Tab债券.尾随页变化.效果图蓝条添加的用户体验) watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzIxMDYyMA==/fo ...

  7. Android数据加载和Json解析——蓝本

    1.下载数据来创建一个实体类 class MyData { String imagepath; String title; String desc; public MyData(String imag ...

  8. 微软Visual Studio &quot;14&quot; CTP 2 公布

     对于在微软阵营下进行工作的团队来说,拥有最新版本号的Visual Studio是提高效率最佳的选择,没有之中的一个. 在本文中,我们就上个月公布的Visual Studio "14&q ...

  9. JavaEE(15) - JPA实体继承

    1. 实体继承映射的三种策略 #1. 整个类层次对应一张表 #2. 连接子类 #3. 每个具体类对应一张表 2. 使用抽象实体 3. 使用非实体父类 4. 重定义子类实体的外键列 ---------- ...

  10. SQL 注意事项

    -------选择表名 配置Ctrl+3 能够select * 桌 USE [NB] GO /* 物: StoredProcedure [dbo].[SP_Select] 脚本日期: 05/28/20 ...