[POJ3523]The Morning after Halloween

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[] = {0, 1, -1, 0, 0}, dy[] = {0, 0, 0, 1, -1};
int n, m, Gh;
char mp[17][17];
int S[4], T[4];
int id[17][17], cnt, X[17*17], Y[17*17];
bool vis[17*17][17*17][17*17];
vector <int> ve[17*17];

struct date {
    int a, b, c;
    int stp;
};

bool ok(int x1, int y1, int x2, int y2) {
    if (x2 == y2) return 0;
    if (x1 == y2 and y1 == x2) return 0;
    return 1;
}

int main()
{
    while(1) 
    {
        for (reg int i = 1 ; i <= cnt ; i ++) ve[i].clear();
        cnt = 0;
        scanf("%d%d%d", &m, &n, &Gh);
        if (!n and !m and !Gh) break;
        char c;
        while((c = getchar()) || 23333) if(c == '
') break;
        for (reg int i = 1 ; i <= n ; i ++)
        {
            gets(mp[i]);
            for (reg int j = 0 ; j < m ; j ++)
            {
                id[i][j + 1] = ++cnt;
                X[cnt] = i, Y[cnt] = j + 1;
                if (mp[i][j] >= 'a' and mp[i][j] <= 'c') S[mp[i][j] - 'a' + 1] = id[i][j + 1];
                if (mp[i][j] >= 'A' and mp[i][j] <= 'C') T[mp[i][j] - 'A' + 1] = id[i][j + 1];
            }
        }
        for (reg int i = 1 ; i <= n ; i ++)
        {
            for (reg int j = 1 ; j <= m ; j ++)
            {
                for (reg int k = 0 ; k <= 4 ; k ++)
                {
                    int ti = i + dx[k], tj = j + dy[k];
                    if (ti <= 0 or ti > n or tj <= 0 or tj > m or mp[ti][tj - 1] == '#') continue;
                    ve[id[i][j]].push_back(id[ti][tj]);
                }
            }
        }
        if (Gh == 1) {
            S[2] = cnt + 1, T[2] = cnt + 1;
            S[3] = cnt + 2, T[3] = cnt + 2;
        }
        if (Gh == 2) {
            S[3] = cnt + 1, T[3] = cnt + 1;
        }
        ve[cnt + 1].push_back(cnt + 1);
        ve[cnt + 2].push_back(cnt + 2);
        queue <date> q;
        q.push((date){S[1], S[2], S[3], 0});
        memset(vis, 0, sizeof vis);
        vis[S[1]][S[2]][S[3]] = 1;
        
        while(!q.empty())
        {
            date ft = q.front();q.pop();
            if (ft.a == T[1] and ft.b == T[2] and ft.c == T[3]) {printf("%d
", ft.stp);goto End;}
            for (reg int i = 0 ; i < (signed)ve[ft.a].size() ; i ++)
            {
                for (reg int j = 0 ; 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 = 0 ; 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]] = 1;
                        q.push(((date){ve[ft.a][i], ve[ft.b][j], ve[ft.c][k], ft.stp + 1}));
                    }
                }
            }
        }
        End:;
    }
}
暴力搜索
双向搜索直接在以上代码上改改就行了。开两个队列,分别从初始状态和末尾状态同时往里搜索,每次各扩展一层,如果相遇则得到答案。


 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cstdlib>
#include <string>
using namespace std;
#define reg register
const int dx[] = {0, 1, -1, 0, 0}, dy[] = {0, 0, 0, 1, -1};
int n, m, Gh;
string mp[17];
int S[4], T[4];
int id[180][180], cnt, X[180], Y[180];
int vis1[180][180][180], vis2[180][180][180];
vector <int> ve[180];

struct date {
    int a, b, c;
    int stp;
};

bool ok(int x1, int y1, int x2, int y2) {
    if (x2 == y2) return 0;
    if (x1 == y2 and y1 == x2) return 0;
    return 1;
}

int main()
{
    while(1) 
    {
        for (reg int i = 1 ; i <= cnt ; i ++) ve[i].clear();
        cnt = 0;
        scanf("%d%d%d", &m, &n, &Gh);
        if (!n and !m and !Gh) break;
        char c;
        while((c = getchar()) || 23333) if(c == '
') break;
        for (reg int i = 1 ; i <= n ; i ++)
        {
            getline(cin, mp[i]);
            for (reg int j = 0 ; j < m ; j ++)
            {
                if (mp[i][j] != '#') 
                    id[i][j + 1] = ++cnt, X[cnt] = i, Y[cnt] = j + 1;
                if (mp[i][j] >= 'a' and mp[i][j] <= 'c') S[mp[i][j] - 'a' + 1] = id[i][j + 1];
                if (mp[i][j] >= 'A' and mp[i][j] <= 'C') T[mp[i][j] - 'A' + 1] = id[i][j + 1];
            }
        }
        for (reg int i = 1 ; i <= n ; i ++)
        {
            for (reg int j = 1 ; j <= m ; j ++)
            {
                if (mp[i][j - 1] == '#') continue;
                for (reg int k = 0 ; k <= 4 ; k ++)
                {
                    int ti = i + dx[k], tj = j + dy[k];
                    if (ti <= 0 or ti > n or tj <= 0 or tj > m or mp[ti][tj - 1] == '#') continue;
                    ve[id[i][j]].push_back(id[ti][tj]);
                }
            }
        }
        if (Gh == 1) {
            S[2] = cnt + 1, T[2] = cnt + 1;
            S[3] = cnt + 2, T[3] = cnt + 2;
        }
        if (Gh == 2) {
            S[3] = cnt + 1, T[3] = cnt + 1;
        }
        ve[cnt + 1].push_back(cnt + 1);
        ve[cnt + 2].push_back(cnt + 2);
        
        queue <date> q1, q2;
        q1.push((date){S[1], S[2], S[3], 0});
        q2.push((date){T[1], T[2], T[3], 0});
        memset(vis1, 0, sizeof vis1);
        memset(vis2, 0, sizeof vis2);
        vis1[S[1]][S[2]][S[3]] = 1;
        vis2[T[1]][T[2]][T[3]] = 1;
        while(!q1.empty() and !q2.empty())
        {
            date ft = q1.front();q1.pop();
            if (vis2[ft.a][ft.b][ft.c]) {printf("%d
", ft.stp + vis2[ft.a][ft.b][ft.c]);goto End;};
            for (reg int i = 0 ; i < (signed)ve[ft.a].size() ; i ++)
            {
                for (reg int j = 0 ; 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 = 0 ; 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 + 1;
                        q1.push(((date){ve[ft.a][i], ve[ft.b][j], ve[ft.c][k], ft.stp + 1}));
                    }
                }
            }
            
            ft = q2.front();q2.pop();
            if (vis1[ft.a][ft.b][ft.c]) {printf("%d
", ft.stp + vis1[ft.a][ft.b][ft.c]);goto End;};
            for (reg int i = 0 ; i < (signed)ve[ft.a].size() ; i ++)
            {
                for (reg int j = 0 ; 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 = 0 ; 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 + 1;
                        q2.push(((date){ve[ft.a][i], ve[ft.b][j], ve[ft.c][k], ft.stp + 1}));
                    }
                }
            }            
            
        }
        End:;
    }
    return 0;
}


 
原文地址:https://www.cnblogs.com/BriMon/p/9770795.html