POJ 3523 The Morning after Halloween 搜索

The Morning after Halloween
Time Limit: 8000MS Memory Limit: 65536K
Total Submissions: 2356 Accepted: 537

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.

whn 
c11c12c1w
c21c22c2w
ch1ch2chw

wh 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

Source


给你地图,问所有小写字母走到对应大写字母的最少时间。


暴力搜索,bfs一遍就可以。

真的好恶心啊。。。


#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <bitset>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=16777216,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);  
int q[maxn],step[maxn];
//int pr[maxn];
int b[7];
char s[25][25];
bool hash[maxn];
int x[3],y[3];
int p,h,t,n,m;
int ax,ay,bx,by,cx,cy,flag,f;

int hashv(int i,int j,int o) {
	return i*b[o*2]+j*b[o*2+1];
}

void solve(int state) {
	for (int i=0;i<p;i++) {
		x[i]=state%16;
		state/=16;
		y[i]=state%16;
		state/=16;
	}
}

bool check(int i,int j) {
	return i>=0&&i<n&&j>=0&&j<m&&s[i][j]!='#';
}

bool cover(int i,int j,int x,int y) {
	return !(i==x&&y==j);
}

void proceed(int val,int pre) {
	hash[val]=1;
	q[++t]=val;
	step[t]=step[pre]+1;
//	pr[t]=pre;
}

int main() {
	int k,i,j;
	scanf("%d%d%d",&m,&n,&p);
	getchar();
	int dir[5][2]={{0,1},{0,-1},{1,0},{-1,0},{0,0}};
	b[0]=1;
	for (i=1;i<7;i++) {
		b[i]=b[i-1]*16;
	}
	while (n!=0||m!=0||p!=0) {
		mem0(hash);
		int state=0,final=0;
		for (i=0;i<n;i++) {
			cin.getline(s[i],20);
			for (j=0;j<m;j++) {
				if (s[i][j]>='A'&&s[i][j]<='Z') {
					final+=hashv(i,j,s[i][j]-'A');
				}
				if (s[i][j]>='a'&&s[i][j]<='z') {
					state+=hashv(i,j,s[i][j]-'a');
				}
			}
		}
		h=-1,t=0;q[0]=state;hash[state]=1;step[0]=0;
		flag=0;
		int ans=-1;
		while (h<t) {
			h++;
			solve(q[h]);
	/*		for (i=0;i<p;i++) {
				cout << x[i] << " " << y[i] << "   ";
			}
			cout << h << ' ' << step[h] << ' ' << pr[h] << '\n';*/
			for (i=0;i<5;i++) {
				ax=x[0]+dir[i][0];ay=y[0]+dir[i][1];
				if (check(ax,ay))
				if (p==1) {
					if (!hash[f=(hashv(ax,ay,0))]) {
						if (f==final) {
							flag=1;
							ans=step[h]+1;
							break;
						}
						proceed(f,h);	
					}	
				} else
				for (j=0;j<5;j++) {
					bx=x[1]+dir[j][0];by=y[1]+dir[j][1];
					if (check(bx,by))
					if (cover(ax,ay,bx,by))
					if (cover(ax,ay,x[1],y[1])||cover(bx,by,x[0],y[0]))
					if (p==2) {
						if (!hash[f=(hashv(ax,ay,0)+hashv(bx,by,1))]) {
							if (f==final) {
								flag=1;
								ans=step[h]+1;
								break;
							}
							proceed(f,h);	
						}	
					} else
					for (k=0;k<5;k++) {
						cx=x[2]+dir[k][0];cy=y[2]+dir[k][1];
						if (check(cx,cy))
						if (cover(ax,ay,cx,cy))
						if (cover(bx,by,cx,cy))
						if (cover(ax,ay,x[2],y[2])||cover(cx,cy,x[0],y[0]))
						if (cover(bx,by,x[2],y[2])||cover(cx,cy,x[1],y[1]))
						if (!hash[f=(hashv(ax,ay,0)+hashv(bx,by,1)+hashv(cx,cy,2))]) {
							if (f==final) {
								flag=1;
								ans=step[h]+1;
								break;
							}
							proceed(f,h);	
						}	
					}
					if (flag) break;
				}
				if (flag) break;
			}
			if (flag) break;
		}
		int now=h;
		printf("%d\n",ans);
		scanf("%d%d%d",&m,&n,&p);
		getchar();
	}
	return 0;
}
/*
5 5 2
#####
#A#B#
#   #
#b#a#
#####
16 4 3
################
## ########## ##
#    ABCcba    #
################
16 16 3
################
### ##    #   ##
##  #  ##   # c#
#  ## ########b#
# ##  # #   #  #
#  # ##   # # ##
##  a#  # # #  #
### ## #### ## #
##   #   #  #  #
#  ##### # ## ##
####   #B# #   #
##  C#   #   ###
#  # # ####### #
# ######  A##  #
#        #    ##
################
0 0 0
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值