Time limit(ms): 1000      Memory limit(kb): 65535
 

江鸟突然想到了一个迷宫逃离的游戏,话说有三个人被困于一个n*m的迷宫里,他们三人都可以向上、向下、向左、向右四个方向进行走动,当然他们所在的初始位置没有障碍物,同时只能走到没有障碍物的格子上,现在江鸟要问你最少需要去掉多少个格子的障碍物,可以使他们三人之间两两互相可达。

Description

输入包括多组测试数据,每组测试数据第一行为两个整数n和m(2<=n,m<=100),接下来n行,每行m个字符,其中:‘w’、‘W’、‘f’分别代表那三个人;‘.’代表没有障碍物的格子,‘#’代表有障碍物的格子。

Input

每组数据输出一行。

Output
1
2
3
4
5
6
7
8
9
10
11
4 4
w...
####
.##f
W##.
4 4
w...
....
.##f
.W..
 
Sample Input
1
2
3
2
0
 
Sample Output
Hint
信息学院院赛
 
集体思路:直接打表3个人每个人到每一个点需要去掉障碍的个数,然后枚举可到达点计算最小值即可~
     具体的可以看代码内注释
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; #define inf 0x3f3f3f3f
#define maxn 110
struct node{
int x, y;
};
int n, m;
node p[];
char mpt[maxn][maxn];
int vis[maxn][maxn][];//vis分别代表每个人到每一个点需要消掉的障碍物
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++;//遇到一个障碍物,那么此人到达这个点需要消掉的障碍物加1
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)){
memset(vis, inf, sizeof(vis));
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 -= ;//如果在一个'#'集聚,那么就会多计算2个
ans = ans < tmp ? ans : tmp;
}
}
printf("%d\n", ans);
}
return ;
}

[Swust OJ 191]--迷宫逃离(打表搜索)的更多相关文章

  1. [swustoj 191] 迷宫逃离

    迷宫逃离(0191) 描述 江鸟突然想到了一个迷宫逃离的游戏,话说有三个人被困于一个n*m的迷宫里,他们三人都可以向上.向下.向左.向右四个方向进行走动,当然他们所在的初始位置没有障碍物,同时只能走到 ...

  2. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  3. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  4. Yii 1开发日记 -- 后台搜索功能下拉及关联表搜索

    Yii 1 实现后台搜索,效果如下: 一. 下拉搜索: 1.模型中和常规的一样 if (isset($_GET['agency']['status']) && $_GET['agenc ...

  5. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  6. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  7. [Swust OJ 409]--小鼠迷宫问题(BFS+记忆化搜索)

    题目链接:http://acm.swust.edu.cn/problem/409/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  8. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  9. [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)

    题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...

随机推荐

  1. yii教程

    http://www.yiichina.com/doc 官网是很好的参考文档

  2. Python学习之路——初识Python

    一.第一个程序Hello World: 1.打印输出Hello World: Python2打印方法: >>> print "hello world"hello ...

  3. Anatomy of a Program in Memory

    Memory management is the heart of operating systems; it is crucial for both programming and system a ...

  4. 调不尽的内存泄漏,用不完的Valgrind

    调不尽的内存泄漏,用不完的Valgrind Valgrind 安装 1. 到www.valgrind.org下载最新版valgrind-X.X.X.tar.bz2 2. 解压安装包:tar –jxvf ...

  5. JS 引用

    var arr1=[1,2,3,4]; var arr2=arr1; arr2.push(5); console.log(arr1);//和arr2一样 console.log(arr1==arr2) ...

  6. MacOS下使用VMware5 破解 安装win7 ISO 激活

    VMware5 下载 破解 以及win7 ISO版本的安装 激活VMware5 下载与破解参考方法http://www.macx.cn/thread-2060440-1-1.htmlVMware5 是 ...

  7. 项目管理软件伙伴https://www.huobanyun.cn/

    现在项目管理软件市面上很多,但能够完全适合每家公司需求的比较难找,因为众口难调,每家公司都有自己的特殊情况,所以,建议考虑下有比较齐全的基础功能的标准化软件产品,同时又在项目管理开发能力上比较突出. ...

  8. JavaEE Tutorials (8) - Java持久化API介绍

    8.1实体96 8.1.1实体类的需求97 8.1.2实体类中的持久化字段和属性97 8.1.3实体的主键101 8.1.4实体关系中的多重性103 8.1.5实体关系中的方向103 8.1.6实体中 ...

  9. Pascal向C++的跨越

    最近从pas转向了C++,觉得需要在语言上总结对比一下,以及记录一些注意点,关于STL,还需要之后好好地学习.同时,希望这篇文章对从pas转C++的同学有所帮助. 基本类型 首先是基本类型的比较: P ...

  10. Libev学习笔记2

    这一节根据官方文档给出的简单示例,深入代码内部,了解其实现机制.示例代码如下: int main (void) { struct ev_loop *loop = EV_DEFAULT; ev_io_i ...