[swustoj 191] 迷宫逃离
迷宫逃离(0191)
描述
江鸟突然想到了一个迷宫逃离的游戏,话说有三个人被困于一个n*m的迷宫里,他们三人都可以向上、向下、向左、向右四个方向进行走动,当然他们所在的初始位置没有障碍物,同时只能走到没有障碍物的格子上,现在江鸟要问你最少需要去掉多少个格子的障碍物,可以使他们三人之间两两互相可达。
输入
输入包括多组测试数据,每组测试数据第一行为两个整数n和m(2<=n,m<=100),接下来n行,每行m个字符,其中:‘w’、‘W’、‘f’分别代表那三个人;‘.’代表没有障碍物的格子,‘#’代表有障碍物的格子。
输出
每组数据输出一行。
样例输入
4 4
w...
####
.##f
W##.
4 4
w...
....
.##f
.W..
样例输出
2
0
BFS、枚举中间点
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std;
#define INF 0x7fffffff
#define N 110 struct node
{
int x,y;
}; int n,m;
node p[];
char mpt[N][N];
int vis[N][N][];
int dir[][]={,,-,,,,,-}; void bfs(int index)
{
queue<node> q;
node now,next;
now.x=p[index].x;
now.y=p[index].y;
q.push(now);
vis[now.x][now.y][index]=;
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=;i<;i++)
{
next=now;
next.x+=dir[i][];
next.y+=dir[i][];
if(next.x< || next.x>n || next.y< || next.y>m) continue;
int tmp=vis[now.x][now.y][index];
if(mpt[next.x][next.y]=='#') tmp++;
if(tmp<vis[next.x][next.y][index])
{
vis[next.x][next.y][index]=tmp;
q.push(next);
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int k=;k<=;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
vis[i][j][k]=INF;
}
}
}
for(int i=;i<=n;i++)
{
scanf("%s",mpt[i]+);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(mpt[i][j]=='w') p[].x=i,p[].y=j;
else if(mpt[i][j]=='W') p[].x=i,p[].y=j;
else if(mpt[i][j]=='f') p[].x=i,p[].y=j;
}
}
bfs();
bfs();
bfs();
int ans=INF;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
int tmp=;
for(int k=;k<=;k++)
{
tmp+=vis[i][j][k];
}
if(mpt[i][j]=='#') tmp-=;
if(tmp<ans) ans=tmp;
}
}
cout<<ans<<endl;
}
return ;
}
[swustoj 191] 迷宫逃离的更多相关文章
- [Swust OJ 191]--迷宫逃离(打表搜索)
题目链接:http://acm.swust.edu.cn/problem/191/ Time limit(ms): 1000 Memory limit(kb): 65535 江鸟突然想到了一个 ...
- [Swust OJ 1023]--Escape(带点其他状态的BFS)
解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535 Descript ...
- hdu 1728:逃离迷宫(DFS,剪枝)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 1728:逃离迷宫(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有 ...
- hdoj 1728 逃离迷宫
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 逃离迷宫(HDU 1728 BFS)
逃离迷宫 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 4524 郑厂长系列故事——逃离迷宫 小水题
郑厂长系列故事——逃离迷宫 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...
- HDU 1728 逃离迷宫(DFS||BFS)
逃离迷宫 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可 ...
- hdu 1728 逃离迷宫 (BFS)
逃离迷宫 Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...
随机推荐
- Pigcms中WeixinAction的简略版流程
if $this->ali = 0; 1.new wechat() //该类存于PigCms/lib/ORG/Wechat.class.php 2.list($content,$type) = ...
- Windows和Linux下查看Apache、MySQL、PHP版本
# Windows查看Apache版本: 使用命令:httpd -v # Linux查看Apache版本: 使用命令:apachectl -v # Windows查看MySQL版本: 使用命令:mys ...
- linksys wrt160nv3 刷dd-wrt固件
家中有个闲置的wrt160nv3路由器,无意中在网上发现可以刷dd-wrt固件来实现更多功能.目前家里电信光猫F460的自带无线使用起来不是很稳定,就想把wrt160nv3刷成dd-wrt来当做一个A ...
- Python3 网络编程
虽然大家现在对互联网很熟悉,但是计算机网络的出现比互联网要早很多. 计算机为了联网,就必须规定通信协议,早期的计算机网络,都是由各厂商自己规定一套协议,IBM.Apple和Microsoft都有各自的 ...
- CSS中Padding的用法
Padding的英文意思是填充,在CSS中则是设置内边距属性. padding不允许使用负值 1. 四个参数时: padding: 10px,20px,30px,40px; 上边距:10px 右边距: ...
- ubuntu10.04搭建嵌入式开发环境
改源 配置vim set number set autoindent set smartindent set tabstop=4 set incsearch 安装g++ 配置samba 1.先安装程序 ...
- Appdelegate 导航操作
隐藏返回按钮 self.navigationItem.hidesBackButton = YES; 设置导航的透明度 self.navigationController.navigationBar.t ...
- sencha touch
download http://www.sencha.com/products/touch/thank-you/ Developer Center http://developer.sencha.co ...
- .net entity framework 泛型 更新与增加记录
static public bool SaveOrUpdate<T>(T entity) where T: class { bool result = false; using (wech ...
- BT5之配置笔记
BT5本来就是用Ubuntu 10.04做得蓝本,所以,我在配置BT5的时候,基本上都是按照Ubuntu 10.04的配置方法,在配置BT5 1 系统基本设置 1.1 安装Ubuntu10.0 ...