SPOJ AMR11J

Description

 

The wizards and witches of Hogwarts School of Witchcraft found Prof. Binn's History of Magic lesson to be no less boring than you found your own history classes.  Recently Binns has been droning on about Goblin wars, and which goblin civilization fought which group of centaurs where etc etc.  The students of Hogwarts decided to use the new-fangled computer to figure out the outcome of all these wars instead of memorizing the results for their upcoming exams.  Can you help them?

 

civilization fought which group of centaurs where etc etc.  The students of Hogwarts decided to use the new-fangled computer to figure out the outcome of all these wars instead of memorizing the results for their upcoming exams.  Can you help them?
The magical world looks like a 2-D R*C grid. Initially there are many civilizations, each civilization occupying exactly one cell. A civilization is denoted by a lowercase letter in the grid. There are also certain cells that are uninhabitable (swamps, mountains, sinkholes etc.) - these cells are denoted by a '#' in the grid. All the other cells - to which the civilizations can move  - are represented by a '.' in the grid.
A cell is said to be adjacent to another cell if they share the same edge - in other words, for a cell (x,y), cells (x-1, y), (x, y-1), (x+1, y), (x, y+1) are adjacent, provided they are within the boundaries of the grid.   Every year each civilization will expand to all unoccupied adjacent cells. If it is already inhabited by some other civilization, it just leaves the cell alone. It is possible that two or more civilizations may move into an unoccupied cell at the same time - this will lead to a battle between the civilizations and the cell will be marked with a '*'. Note that the civilizations fighting in a particular cell do not try to expand from that cell, but will continue to expand from other cells, if possible.
Given the initial grid, output the final state of the grid after no further expansion by any civilization is possible.
Input (STDIN):
The first line contains T, the number of cases. This is followed by T test case blocks.
Each test case contains two integers, R, C.
This is followed by R lines containing a string of length C. The j-th letter in the i-th row describes the state of the cell in year 0.
Each cell is either a
1. '.' which represents an unoccupied cell
2. '#' which represents a cell that cannot be occupied
3. A civilization represented by a lowercase letter ('a' - 'z')
Output (STDOUT):
For each test case, print the final grid after no expansion is possible. Apart from the notations used in the input, use '*' to denote that a battle is being waged in that particular cell. 
Print a blank line at the end of each case.
Constraints:
1 <= R, C <= 500
1 <= T <= 5
Time Limit:  3 s
Memory Limit: 64 MB
Sample Input:
5
3 5
#####
a...b
#####
3 4
####
a..b
####
3 3
#c#
a.b
#d#
3 3
#c#
...
a.b
3 5
.....
.#.#.
a...b
Sample Output:
#####
aa*bb
#####
####
aabb
####
#c#
a*b
#d#
#c#
acb
a*b
aa*bb
a#.#
aa*bb

The magical world looks like a 2-D R*C grid. Initially there are many civilizations, each civilization occupying exactly one cell. A civilization is denoted by a lowercase letter in the grid. There are also certain cells that are uninhabitable (swamps, mountains, sinkholes etc.) - these cells are denoted by a '#' in the grid. All the other cells - to which the civilizations can move  - are represented by a '.' in the grid.

 

A cell is said to be adjacent to another cell if they share the same edge - in other words, for a cell (x,y), cells (x-1, y), (x, y-1), (x+1, y), (x, y+1) are adjacent, provided they are within the boundaries of the grid.   Every year each civilization will expand to all unoccupied adjacent cells. If it is already inhabited by some other civilization, it just leaves the cell alone. It is possible that two or more civilizations may move into an unoccupied cell at the same time - this will lead to a battle between the civilizations and the cell will be marked with a '*'. Note that the civilizations fighting in a particular cell do not try to expand from that cell, but will continue to expand from other cells, if possible.

Given the initial grid, output the final state of the grid after no further expansion by any civilization is possible.

 

Input (STDIN):

The first line contains T, the number of cases. This is followed by T test case blocks.

Each test case contains two integers, R, C.

This is followed by R lines containing a string of length C. The j-th letter in the i-th row describes the state of the cell in year 0.

Each cell is either a

1. '.' which represents an unoccupied cell

2. '#' which represents a cell that cannot be occupied

3. A civilization represented by a lowercase letter ('a' - 'z')

 

Output (STDOUT):

For each test case, print the final grid after no expansion is possible. Apart from the notations used in the input, use '*' to denote that a battle is being waged in that particular cell. 

Print a blank line at the end of each case.

 

Constraints:

1 <= R, C <= 500

1 <= T <= 5

 

Sample Input:

5

3 5

#####

a...b

#####

3 4

####

a..b

####

3 3

#c#

a.b

#d#

3 3

#c#

...

a.b

3 5

.....

.#.#.

a...b

 

Sample Output:

#####

aa*bb

#####

 

####

aabb

####

 

#c#

a*b

#d#

 

#c#

acb

a*b

 

aa*bb

a#.#b

aa*bb


解析

这道题乍一看很容易想到模拟。但是交上去后不幸地超时了。再想一想SPFA的优化策略,这道题也是这样的。下一步变化必定是与上一步变化相关的。就是说每一步变化可能引起下一步变化。当一步变化不能引起下一步变化时,程序结束。
我们可以用类似SPFA的优化的方法处理,用一个队列记录上一步变化的点,然后不停弹出曾经变化过的点,进行下一步变化。直到队列为空,程序结束。
有一点要处理的是交战区域,因此,我用了一个ti[i][j]来表示i,j被占领的时间,如果多个文明同时占领一个格子,就计为战争'*'。


#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

queue<int> qi,qj;

char Grid[600][600];
int ti[600][600];
int R,C;//R行C列

void write()
{
	for(int i=1;i<=R;i++)
	{
		for(int j=1;j<=C;j++)
			printf("%c",Grid[i][j]);
		printf("\n");
	}
}

void readdata()
{
	scanf("%d%d",&R,&C);

	for(int i=0;i<=R+1;i++)
		for(int j=0;j<=C+1;j++)
		{
			Grid[i][j]='#';
			ti[i][j]=0;
		}
	getchar();
	for(int i=1;i<=R;i++)
	{
		for(int j=1;j<=C;j++)
		{
			scanf("%c",&Grid[i][j]);
			if( ('a'<=Grid[i][j])&&(Grid[i][j]<='z') )
			{
				ti[i][j]=1;
				qi.push(i);
				qj.push(j);
			}
		}
		getchar();
	}
}

void expand(int i,int j)
{
	char c=Grid[i][j];
	int now=ti[i][j];
	//下
	int ii=i+1,jj=j;
	if( (ti[ii][jj] == 0)&&(Grid[ii][jj] == '.') )
		{
			Grid[ii][jj]=c;
			ti[ii][jj]=now+1;
			qi.push(ii);
			qj.push(jj);
		}
	else if( (ti[ii][jj]==now+1)&&(Grid[ii][jj] != c) )
			Grid[ii][jj]='*';
	//上
	ii=i-1;jj=j;
	if( (ti[ii][jj] == 0)&&(Grid[ii][jj] == '.') )
		{
			Grid[ii][jj]=c;
			ti[ii][jj]=now+1;
			qi.push(ii);
			qj.push(jj);
		}
	else if( (ti[ii][jj]==now+1)&&(Grid[ii][jj] != c) )
			Grid[ii][jj]='*';
	//左
	ii=i;jj=j-1;
	if((ti[ii][jj] == 0)&&(Grid[ii][jj] == '.'))		
		{
			Grid[ii][jj]=c;
			ti[ii][jj]=now+1;
			qi.push(ii);
			qj.push(jj);
		}
	else if( (ti[ii][jj]==now+1)&&(Grid[ii][jj] != c) )
			Grid[ii][jj]='*';
	//右
	ii=i;jj=j+1;
	if( (ti[ii][jj] == 0)&&(Grid[ii][jj] == '.'))
		{
			Grid[ii][jj]=c;
			ti[ii][jj]=now+1;
			qi.push(ii);
			qj.push(jj);
		}
	else if( (ti[ii][jj]==now+1)&&(Grid[ii][jj] != c) )
			Grid[ii][jj]='*';
}

void work()
{
	while(!qi.empty())
	{
		if(('a'<=Grid[qi.front()][qj.front()])&&(Grid[qi.front()][qj.front()]<='z'))
			expand(qi.front(),qj.front());
		qi.pop();qj.pop();
	}
}

int main()
{
	freopen("J.in","r",stdin);
	freopen("J.out","w",stdout);

	int T;
	scanf("%d",&T);
	for(int i=1;i<=T;i++)
	{
		readdata();
		work();
		write();
		printf("\n");
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值