【Jason's_ACM_解题报告】The Morning after Halloween

The Morning after Halloween

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.




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 

36 
77

这道题如果有些基础那么一定要要求自己,捋清思路然后尽量自己做出来。

最简单粗暴的方法是记录16×16个网格每一步的状态,可是这样的话,状态数是(16×16)^3即2^24个,这对空间的要求太高了,并不合适,而且时间上也无法承受。

可不可以有更高效的方法呢?由于题目提到每2×2个区域内至少有一个是“#”,而且所有的空白是联通的,那么我们可以把每一个空格看作一个点,构出图来,然后进行bfs。

这样题目就可以解决了,这就是隐式图的建立。

写本题的时候我学习了fgets函数的使用,首先它是从缓冲区读取字符,并且遇到回车结束,所以在读入w、h、n之后,必须强行scanf("\n");否则,回车会被送入缓冲区并被读入maze数组中。


附代码如下:
#include<cctype>
#include<cstdio> 
#include<cstring>

#include<vector>
#include<queue>

using namespace std;

#define MAXG (20)
#define MAXN (200)

struct GRAPH{
	int x,y;
	vector<int> next;
};

int w,h,n,total;
char maze[MAXG][MAXG];
int id[MAXG][MAXG],dis[MAXN][MAXN][MAXN],s[3],t[3];
GRAPH g[MAXN];

bool input(){
	scanf("%d%d%d\n",&w,&h,&n);
	if(!w&&!h&&!n)return false;
	for(int i=0;i<h;i++)
		fgets(maze[i],20,stdin);
	return true;
}

const int dr[]={-1,0,1,0,0};
const int dc[]={0,1,0,-1,0};

void build_graph(){
	total=0;
	for(int i=0;i<MAXN;i++)g[i].next.clear();
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(maze[i][j]!='#'){
				g[total].x=i;g[total].y=j;id[i][j]=total;
				if(islower(maze[i][j]))s[maze[i][j]-'a']=total;
				if(isupper(maze[i][j]))t[maze[i][j]-'A']=total;
				total++;
			}
		}
	}
	for(int i=0;i<total;i++){
		for(int j=0;j<5;j++){
			int newx=g[i].x+dr[j];
			int newy=g[i].y+dc[j];
			if(maze[newx][newy]!='#'){
				g[i].next.push_back(id[newx][newy]);
			}
		}
	}/*
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(maze[i][j]!='#'){
				printf("%d",id[i][j]);
			}else printf("#");
		}
		printf("\n");
	}*/
	if(n<=2){g[total].next.push_back(total);s[2]=t[2]=total++;}
	if(n<=1){g[total].next.push_back(total);s[1]=t[1]=total++;}
}

int STATE(int a,int b,int c){
	return (a<<16)|(b<<8)|c;
}

bool conflict(int a,int b,int a2,int b2){
	if(a2==b2||(a==b2&&b==a2))return true;return false;
}

int bfs(){
	memset(dis,-1,sizeof(dis));
	queue<int> q;
	q.push(STATE(s[0],s[1],s[2]));
	dis[s[0]][s[1]][s[2]]=0;
	while(!q.empty()){
		int tmp=q.front();q.pop();
		int a=(tmp>>16)&0xff;
		int b=(tmp>>8)&0xff;
		int c=tmp&0xff;
		if(a==t[0]&&b==t[1]&&c==t[2])return dis[a][b][c];
		for(int i=0;i<g[a].next.size();i++){
			int a2=g[a].next[i];
			for(int j=0;j<g[b].next.size();j++){
				int b2=g[b].next[j];
				if(conflict(a,b,a2,b2))continue;
				for(int k=0;k<g[c].next.size();k++){
					int c2=g[c].next[k];
					if(conflict(a,c,a2,c2))continue;
					if(conflict(b,c,b2,c2))continue;
					if(dis[a2][b2][c2]!=-1)continue;
					dis[a2][b2][c2]=dis[a][b][c]+1;
					q.push(STATE(a2,b2,c2));
				}
			}
		}
	}
}

int main(){
//freopen("in.txt","r",stdin);
	while(input()){
		build_graph();
		printf("%d\n",bfs());
	}
//fclose(stdin);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值