UVA 3523(预处理+状态压缩)

I - The Morning after Halloween
Time Limit:12000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

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#   a b#   acb#   ab #
                       #c##   #c##   # ##   #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 
c11c12...c1w
c21c22...c2w
$ \vdots$$ \vdots$$ \ddots$$ \vdots$
ch1ch2...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.

$\displaystyle \le$ w $\displaystyle \le$16,   4  $\displaystyle \le$ h $\displaystyle \le$16,   1  $\displaystyle \le$ n $\displaystyle \le$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

题意:令a,b,c回到A,B,C位置,且过程中不能相撞,求最小到达该状态时间;
分析:因为该图每二宫格必有一面墙(题中条件),可以将可以站的方格都看做节点,相邻方块建一条边,如此预处理,就能生成一个节点数不超过256的图,而且后继状态也大大减少。
再来说状态压缩,图中最多有256==2^8节点,将上面预处理生成的节点编号分别放在一个int的0-8,9-16,17-24位,该int就是图中的一个状态了,自然开1<<24int数组就OK。
AC代码:
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXM=1500;
char mmp[20][20];
int mpp[20][20],u[MAXM],v[MAXM],next[MAXM],first[256];bool done[(1<<25)];
int vcnt,ecnt,A,B,C,a,b,c;//#define don(a,b,c) done[(a<<16)+(b<<8)+c]
struct node
{
	int v[3],w;
	node(int x=0,int y=0,int z=0,int ww=0)
	{
		v[0]=x;v[1]=y;v[2]=z;w=ww;
	}
};
int bfs()
{
	int i,j,k;
	queue<node>q;
	memset(done,0,sizeof done);
	q.push(node(a,b,c,0));
	done[(a<<16)+(b<<8)+c]=1;
	while(!q.empty())
	{
		node e=q.front();q.pop();
		
		for(i=first[e.v[0]];i!=-1;i=next[i])
			for(j=first[e.v[1]];j!=-1;j=next[j])
				for(k=first[e.v[2]];k!=-1;k=next[k])
				{
					if(v[i]==v[j]||v[i]==v[k]||v[j]==v[k]||done[(v[i]<<16)+(v[j]<<8)+v[k]])continue;//去掉移动后位置冲突的
					if(v[i]==e.v[1]&&v[j]==e.v[0])continue;//前后交换位置肯定相撞了
					if(v[i]==e.v[2]&&v[k]==e.v[0])continue;
					if(v[j]==e.v[2]&&v[k]==e.v[1])continue;
					if(v[i]==A&&v[j]==B&&v[k]==C)return e.w+1;
					q.push(node(v[i],v[j],v[k],e.w+1));
					done[(v[i]<<16)+(v[j]<<8)+v[k]]=1;
					//printf("%d %d %d-->%d %d %d     %d\n",e.v[0],e.v[1],e.v[2],v[i],v[j],v[k],e.w);
				}
	}
	return -1;
}
void add_(int a,int b)
{
	u[ecnt]=a;
	v[ecnt]=b;
	next[ecnt]=first[a];
	first[a]=ecnt++;
}
int main()
{
	int n,m,i,j,human;char s[2];
	while(~scanf("%d%d%d",&m,&n,&human))
	{
		gets(s);
		if(!m&&!n&&!human)break;
		for(i=0;i<n;i++)
			gets(mmp[i]);
		vcnt=0;ecnt=0;
		memset(first,-1,sizeof first);
		memset(next,-1,sizeof next);
		memset(mpp,0,sizeof mpp);
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
				if(mmp[i][j]!='#')
				{
					vcnt++;
					if(mmp[i][j]=='A')A=vcnt;
					if(mmp[i][j]=='B')B=vcnt;
					if(mmp[i][j]=='C')C=vcnt;
					if(mmp[i][j]=='a')a=vcnt;
					if(mmp[i][j]=='b')b=vcnt;
					if(mmp[i][j]=='c')c=vcnt;
					mpp[i][j]=vcnt;
					if(i&&mpp[i-1][j])add_(mpp[i-1][j],vcnt),add_(vcnt,mpp[i-1][j]);
					if(j&&mpp[i][j-1])add_(mpp[i][j-1],vcnt),add_(vcnt,mpp[i][j-1]);
					add_(vcnt,vcnt);//a,b,c可以选择不动
				}
		if(human==2)//如果人数不为3,将多的人发在其他节点中,而且该结点只能连接自己
		{
			C=c=++vcnt;
			add_(vcnt,vcnt);
		}
		else if(human==1)
		{
			C=c=++vcnt;
			add_(vcnt,vcnt);
			B=b=++vcnt;
			add_(vcnt,vcnt);
		}
		printf("%d\n",bfs());
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值