Description

You are working for an amusement park as an operator of an obakeyashiki, or a haunted house, in which guests walk through narrow and dark corridors. The house is proud of their lively ghosts, which are actually robots remotely controlled by the operator, hiding here and there in the corridors. One morning, you found that the ghosts are not in the positions where they are supposed to be. Ah, yesterday was Halloween. Believe or not, paranormal spirits have moved them around the corridors in the night. You have to move them into their right positions before guests come. Your manager is eager to know how long it takes to restore the ghosts.

In this problem, you are asked to write a program that, given a floor map of a house, finds the smallest number of steps to move all ghosts to the positions where they are supposed to be.

A floor consists of a matrix of square cells. A cell is either a wall cell where ghosts cannot move into or a corridor cell where they can.

At each step, you can move any number of ghosts simultaneously. Every ghost can either stay in the current cell, or move to one of the corridor cells in its 4-neighborhood (i.e. immediately left, right, up or down), if the ghosts satisfy the following conditions:

  1. No more than one ghost occupies one position at the end of the step.

  2. No pair of ghosts exchange their positions one another in the step.

For example, suppose ghosts are located as shown in the following (partial) map, where a sharp sign (‘#’) represents a wall cell and ‘a’, ‘b’, and ‘c’ ghosts.

####
ab#
#c##
####

The following four maps show the only possible positions of the ghosts after one step.

####
ab#
#c##
####
 
####
a b#
#c##
####
 
####
acb#
# ##
####
 
####
ab #
#c##
####

Input

The input consists of at most 10 datasets, each of which represents a floor map of a house. The format of a dataset is as follows.

w h n  
c11 c12 c1w
c21 c22 c2w
ch1 ch2 chw

w, h and n in the first line are integers, separated by a space. w and h are the floor width and height of the house, respectively. n is the number of ghosts. They satisfy the following constraints.

4 ≤ w ≤ 16, 4 ≤ h ≤ 16, 1 ≤ n ≤ 3

Subsequent h lines of w characters are the floor map. Each of cij is either:

  • a ‘#’ representing a wall cell,

  • a lowercase letter representing a corridor cell which is the initial position of a ghost,

  • an uppercase letter representing a corridor cell which is the position where the ghost corresponding to its lowercase letter is supposed to be, or

  • a space representing a corridor cell that is none of the above.

In each map, each of the first n letters from a and the first n letters from A appears once and only once. Outermost cells of a map are walls; i.e. all characters of the first and last lines are sharps; and the first and last characters on each line are also sharps. All corridor cells in a map are connected; i.e. given a corridor cell, you can reach any other corridor cell by following corridor cells in the 4-neighborhoods. Similarly, all wall cells are connected. Any 2 × 2 area on any map has at least one sharp. You can assume that every map has a sequence of moves of ghosts that restores all ghosts to the positions where they are supposed to be.

The last dataset is followed by a line containing three zeros separated by a space.

Output

For each dataset in the input, one line containing the smallest number of steps to restore ghosts into the positions where they are supposed to be should be output. An output line should not contain extra characters such as spaces.

Sample Input

5 5 2
#####
#A#B#
# #
#b#a#
#####
16 4 3
################
## ########## ##
# ABCcba #
################
16 16 3
################
### ## # ##
## # ## # c#
# ## ########b#
# ## # # # #
# # ## # # ##
## a# # # # #
### ## #### ## #
## # # # #
# ##### # ## ##
#### #B# # #
## C# # ###
# # # ####### #
# ###### A## #
# # ##
################
0 0 0

Sample Output

7
36
77


这题直接搜索就可过,不用双向搜索。
但双向搜索比直接搜索快了一倍...
因为题目中说明障碍很多,如果我们对一个状态向四面扩展判断是否可行的话,就会多做非常多的运算和判断,常数巨大。
我们可以处理出每个点向四周有哪些可以走,这样会大大优化常数。
bfs要判重需要开很大的数组,在poj上开17*17的3次方的数组会炸掉。
所以一个障碍物不给他标号,因为他们不可能被用到,这样就少了很多无用的空间。
为了让代码更加简便,处理3只鬼以下的情况,我们只用在图外面新开一个点,当做缺少的鬼的起点和终点,避免的过多的分类讨论。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cstdlib>
using namespace std;
#define reg register
const int dx[] = {, , -, , }, dy[] = {, , , , -};
int n, m, Gh;
char mp[][];
int S[], T[];
int id[][], cnt, X[*], Y[*];
bool vis[*][*][*];
vector <int> ve[*]; struct date {
int a, b, c;
int stp;
}; bool ok(int x1, int y1, int x2, int y2) {
if (x2 == y2) return ;
if (x1 == y2 and y1 == x2) return ;
return ;
} int main()
{
while()
{
for (reg int i = ; i <= cnt ; i ++) ve[i].clear();
cnt = ;
scanf("%d%d%d", &m, &n, &Gh);
if (!n and !m and !Gh) break;
char c;
while((c = getchar()) || ) if(c == '\n') break;
for (reg int i = ; i <= n ; i ++)
{
gets(mp[i]);
for (reg int j = ; j < m ; j ++)
{
id[i][j + ] = ++cnt;
X[cnt] = i, Y[cnt] = j + ;
if (mp[i][j] >= 'a' and mp[i][j] <= 'c') S[mp[i][j] - 'a' + ] = id[i][j + ];
if (mp[i][j] >= 'A' and mp[i][j] <= 'C') T[mp[i][j] - 'A' + ] = id[i][j + ];
}
}
for (reg int i = ; i <= n ; i ++)
{
for (reg int j = ; j <= m ; j ++)
{
for (reg int k = ; k <= ; k ++)
{
int ti = i + dx[k], tj = j + dy[k];
if (ti <= or ti > n or tj <= or tj > m or mp[ti][tj - ] == '#') continue;
ve[id[i][j]].push_back(id[ti][tj]);
}
}
}
if (Gh == ) {
S[] = cnt + , T[] = cnt + ;
S[] = cnt + , T[] = cnt + ;
}
if (Gh == ) {
S[] = cnt + , T[] = cnt + ;
}
ve[cnt + ].push_back(cnt + );
ve[cnt + ].push_back(cnt + );
queue <date> q;
q.push((date){S[], S[], S[], });
memset(vis, , sizeof vis);
vis[S[]][S[]][S[]] = ; while(!q.empty())
{
date ft = q.front();q.pop();
if (ft.a == T[] and ft.b == T[] and ft.c == T[]) {printf("%d\n", ft.stp);goto End;}
for (reg int i = ; i < (signed)ve[ft.a].size() ; i ++)
{
for (reg int j = ; j < (signed)ve[ft.b].size() ; j ++)
{
if (!ok(ft.a, ft.b, ve[ft.a][i], ve[ft.b][j])) continue;
for (reg int k = ; k < (signed)ve[ft.c].size() ; k ++)
{
if (vis[ve[ft.a][i]][ve[ft.b][j]][ve[ft.c][k]]) continue;
if (!ok(ft.a, ft.c, ve[ft.a][i], ve[ft.c][k]) or !ok(ft.b, ft.c, ve[ft.b][j], ve[ft.c][k])) continue;
vis[ve[ft.a][i]][ve[ft.b][j]][ve[ft.c][k]] = ;
q.push(((date){ve[ft.a][i], ve[ft.b][j], ve[ft.c][k], ft.stp + }));
}
}
}
}
End:;
}
}

暴力搜索

双向搜索直接在以上代码上改改就行了。开两个队列,分别从初始状态和末尾状态同时往里搜索,每次各扩展一层,如果相遇则得到答案。

 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cstdlib>
#include <string>
using namespace std;
#define reg register
const int dx[] = {, , -, , }, dy[] = {, , , , -};
int n, m, Gh;
string mp[];
int S[], T[];
int id[][], cnt, X[], Y[];
int vis1[][][], vis2[][][];
vector <int> ve[]; struct date {
int a, b, c;
int stp;
}; bool ok(int x1, int y1, int x2, int y2) {
if (x2 == y2) return ;
if (x1 == y2 and y1 == x2) return ;
return ;
} int main()
{
while()
{
for (reg int i = ; i <= cnt ; i ++) ve[i].clear();
cnt = ;
scanf("%d%d%d", &m, &n, &Gh);
if (!n and !m and !Gh) break;
char c;
while((c = getchar()) || ) if(c == '\n') break;
for (reg int i = ; i <= n ; i ++)
{
getline(cin, mp[i]);
for (reg int j = ; j < m ; j ++)
{
if (mp[i][j] != '#')
id[i][j + ] = ++cnt, X[cnt] = i, Y[cnt] = j + ;
if (mp[i][j] >= 'a' and mp[i][j] <= 'c') S[mp[i][j] - 'a' + ] = id[i][j + ];
if (mp[i][j] >= 'A' and mp[i][j] <= 'C') T[mp[i][j] - 'A' + ] = id[i][j + ];
}
}
for (reg int i = ; i <= n ; i ++)
{
for (reg int j = ; j <= m ; j ++)
{
if (mp[i][j - ] == '#') continue;
for (reg int k = ; k <= ; k ++)
{
int ti = i + dx[k], tj = j + dy[k];
if (ti <= or ti > n or tj <= or tj > m or mp[ti][tj - ] == '#') continue;
ve[id[i][j]].push_back(id[ti][tj]);
}
}
}
if (Gh == ) {
S[] = cnt + , T[] = cnt + ;
S[] = cnt + , T[] = cnt + ;
}
if (Gh == ) {
S[] = cnt + , T[] = cnt + ;
}
ve[cnt + ].push_back(cnt + );
ve[cnt + ].push_back(cnt + ); queue <date> q1, q2;
q1.push((date){S[], S[], S[], });
q2.push((date){T[], T[], T[], });
memset(vis1, , sizeof vis1);
memset(vis2, , sizeof vis2);
vis1[S[]][S[]][S[]] = ;
vis2[T[]][T[]][T[]] = ;
while(!q1.empty() and !q2.empty())
{
date ft = q1.front();q1.pop();
if (vis2[ft.a][ft.b][ft.c]) {printf("%d\n", ft.stp + vis2[ft.a][ft.b][ft.c]);goto End;};
for (reg int i = ; i < (signed)ve[ft.a].size() ; i ++)
{
for (reg int j = ; j < (signed)ve[ft.b].size() ; j ++)
{
if (!ok(ft.a, ft.b, ve[ft.a][i], ve[ft.b][j])) continue;
for (reg int k = ; k < (signed)ve[ft.c].size() ; k ++)
{
if (vis1[ve[ft.a][i]][ve[ft.b][j]][ve[ft.c][k]]) continue;
if (!ok(ft.a, ft.c, ve[ft.a][i], ve[ft.c][k]) or !ok(ft.b, ft.c, ve[ft.b][j], ve[ft.c][k])) continue;
vis1[ve[ft.a][i]][ve[ft.b][j]][ve[ft.c][k]] = ft.stp + ;
q1.push(((date){ve[ft.a][i], ve[ft.b][j], ve[ft.c][k], ft.stp + }));
}
}
} ft = q2.front();q2.pop();
if (vis1[ft.a][ft.b][ft.c]) {printf("%d\n", ft.stp + vis1[ft.a][ft.b][ft.c]);goto End;};
for (reg int i = ; i < (signed)ve[ft.a].size() ; i ++)
{
for (reg int j = ; j < (signed)ve[ft.b].size() ; j ++)
{
if (!ok(ft.a, ft.b, ve[ft.a][i], ve[ft.b][j])) continue;
for (reg int k = ; k < (signed)ve[ft.c].size() ; k ++)
{
if (vis2[ve[ft.a][i]][ve[ft.b][j]][ve[ft.c][k]]) continue;
if (!ok(ft.a, ft.c, ve[ft.a][i], ve[ft.c][k]) or !ok(ft.b, ft.c, ve[ft.b][j], ve[ft.c][k])) continue;
vis2[ve[ft.a][i]][ve[ft.b][j]][ve[ft.c][k]] = ft.stp + ;
q2.push(((date){ve[ft.a][i], ve[ft.b][j], ve[ft.c][k], ft.stp + }));
}
}
} }
End:;
}
return ;
}

 

[POJ3523]The Morning after Halloween的更多相关文章

  1. 【BFS】The Morning after Halloween

    [POJ3523]The Morning after Halloween Time Limit: 8000MS   Memory Limit: 65536K Total Submissions: 23 ...

  2. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  3. Lightoj 题目1422 - Halloween Costumes(区间DP)

    1422 - Halloween Costumes   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  4. CSUFT 1004 This is Halloween: Saving Money

    1004: This is Halloween: Saving Money Time Limit: 1 Sec      Memory Limit: 128 MB Submit: 11      So ...

  5. [POJ 3370] Halloween treats

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7143   Accepted: 2641 ...

  6. poj 3370 Halloween treats(鸽巢原理)

    Description Every year there is the same problem at Halloween: Each neighbour is only willing to giv ...

  7. LightOJ - 1422 Halloween Costumes (区间dp)

    Description Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he i ...

  8. UVA 11237 - Halloween treats(鸽笼原理)

    11237 - Halloween treats option=com_onlinejudge&Itemid=8&page=show_problem&category=516& ...

  9. 鸽巢原理应用-分糖果 POJ 3370 Halloween treats

    基本原理:n+1只鸽子飞回n个鸽笼至少有一个鸽笼含有不少于2只的鸽子. 很简单,应用却也很多,很巧妙,看例题: Description Every year there is the same pro ...

随机推荐

  1. 第五场周赛(字符串卡常个人Rank赛)——题解

    本次题目因为比较简单,除了个别题目,其余题目我只写一个思路不再贴代码. 先是Div.2的题解 A题奇怪的优化,把递归函数改成2*fun(...)即可,其实看懂程序也不难,就是求a*2b: B题你会st ...

  2. 白话系列之IOC,三个类实现简单的Ioc

    前言:博客园上已经有很多IOC的博客.而且很多写的很好,达到开源的水平,但是对于很多新人来说,只了解ioc的概念,以及怎么去使用ioc.然后想更进一步去看源码,但是大部分源码都比较困难,当不知道一个框 ...

  3. 一个基于vue的仪表盘demo

    最近写了一个基于vue的仪表盘,其中 主要是和 transform 相关的 css 用的比较多.给大家分享一下,喜欢的话点个赞呗?嘿嘿 截图如下: 实际效果查看地址:https://jhcan333. ...

  4. C++程序设计学习

    第一章 预备知识 1.C++历史起源 由于C语言具有许多优点,比如语言简洁灵活:运算符和数据类型丰富:具有结构化控制语句:程序执行效率高:同时具有高级语言和汇编语言的优点等.与其他高级语言相比,C语言 ...

  5. 37 (OC)* 类别的作用

    问题: OC中类别(Category)是什么?Category类别是Objective-C语言中提供的一个灵活的类扩展机制.类别用于在不获悉.不改变原来代码的情况下往一个已经存在的类中添加新的方法,只 ...

  6. log4j日志不输出的问题

    今天服务器上报错,想先去看一下日志进行排查,结果发现日志很久都没有输出过了.从上午排查到下午,刚刚解决,因此记录一下,但现在也只是知其然,并不知其所以然,所以如果大家有什么想法请在下方评论. 先说一下 ...

  7. JavaScript之深入对象(一)

    在之前的<JavaScript对象基础>中,我们大概了解了对象的创建和使用,知道对象可以使用构造函数和字面量方式创建.那么今天,我们就一起来深入了解一下JavaScript中的构造函数以及 ...

  8. 关于WinForm TreeView的分享~

    最近在写个测试demo的时候使用到WinForm TreeView,已经好久没接触了,有些生疏,所以还是记录一下遇到的一些问题. 1.如果动态绑定TreeView,这个功能一般会在数据量不确定,需要去 ...

  9. 在Debian上用FVWM做自己的桌面

    用FVWM做自己的桌面 Table of Contents 1. 前言 2. 学习步骤 3. 准备 3.1. 软件包 3.2. 字体 3.3. 图片 3.4. 参考资料 4. 环境 5. 布局 6. ...

  10. 【系统设计】分布式唯一ID生成方案总结

    目录 分布式系统中唯一ID生成方案 1. 唯一ID简介 2. 全局ID常见生成方案 2.1 UUID生成 2.2 数据库生成 2.3 Redis生成 2.4 利用zookeeper生成 2.5 雪花算 ...