Goblin Wars

Time Limit:432MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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?

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

题解:

  1. 题目的大意是在一块矩形地面上,一开始有很多个部落,也有很多无主地和不能居住的地,那么每个部落每年都会向无主地四周扩张各一块地,如果该地是不能居住的地,则不能扩张,如果同一年两个部落同时扩张到同一块地,那么就会爆发战争,该地无法居住,如果扩张发现那一块地已经有主,则不能扩张。
  2. 很显然是广度优先,使用tag[i][j]记录被占领的时间,无主地初始化为0,一开始的部落初始化为1,以后记录每块地被占领的时间。先将原始部落入队,然后再占领其他的地。

  3. 情况比较多,仔细考虑,应该没有什么问题。

​以下是代码:

#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cctype>
using namespace std; #define ss(x) scanf("%d",&x)
#define ff(i,s,e) for(int i=s;i<e;i++)
#define fe(i,s,e) for(int i=s;i<=e;i++)
#define print(x) printf("%d\n",x);
#define write() freopen("1.in","r",stdin);
struct Node{
int x, y,k;
char a;
void set(int i,int j){
x=i;y=j;
}
}t,tp;
const int N = 600;
const int xx[4]={0,0,-1,1};//代表四个方向
const int yy[4]={1,-1,0,0};
char str[N][N];
int r,c;
int tag[N][N];
void solve(){
memset(tag,0,sizeof(tag));
queue<Node>q;
int x,y;
ff(i,0,r)
ff(j,0,c)//先将最先的部落入队
if(isalpha(str[i][j])){
t.set(i,j);
tag[i][j]=1;
q.push(t);
}
while(!q.empty()){
tp=q.front();
q.pop();
int _x=tp.x,_y=tp.y;
char pre = str[_x][_y];
if(str[_x][_y]=='*')continue;//已经爆发战争,继续
ff(i,0,4){//向四个方向前进
x=tp.x+xx[i];
y=tp.y+yy[i];
char ch = str[x][y];
if(ch == pre)continue;//如果是自己部落,继续
if(x<0 || x >=r || y<0 || y>=c)continue;//越界,继续
if(ch=='#'|| ch=='*')continue;//不能居住,继续
if(ch=='.'){ //可以占领,占领后入队
t.set(x,y);
str[x][y]=pre;
tag[x][y]=tag[_x][_y]+1;
q.push(t);
}
else if(tag[_x][_y]+1 != tag[x][y])continue;//不是同时占领的,继续
else str[x][y]='*';//同时占领的不同部落爆发战争
}
}
}
int main(){
//write();
int T;
ss(T);
while(T--){
ss(r);ss(c);
ff(i,0,r)
scanf("%s",str[i]);
solve();
ff(i,0,r)
puts(str[i]);
printf("\n");
}
}

  

Spring-2-J Goblin Wars(SPOJ AMR11J)解题报告及测试数据的更多相关文章

  1. Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据

    Array Diversity Time Limit:404MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descript ...

  2. Spring-2-B Save the Students(SPOJ AMR11B)解题报告及测试数据

    Save the Students Time Limit:134MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descri ...

  3. Spring-2-A Magic Grid(SPOJ AMR11A)解题报告及测试数据

    Magic Grid Time Limit:336MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Tha ...

  4. sgu 104 Little shop of flowers 解题报告及测试数据

    104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...

  5. Spring-1-I 233 Matrix(HDU 5015)解题报告及测试数据

    233 Matrix Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Descript ...

  6. Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据

    Number Sequence Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Pro ...

  7. Spring-1-F Dice(HDU 5012)解题报告及测试数据

    Dice Time Limit:1000MS     Memory Limit:65536KB Description There are 2 special dices on the table. ...

  8. Spring-1-A Post Robot(HDU 5007)解题报告及测试数据

    Post Robot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K Problem Description ...

  9. Winter-2-STL-D The Blocks Problem 解题报告及测试数据

    Time Limit:3000MS     Memory Limit:0KB Description Background Many areas of Computer Science use sim ...

随机推荐

  1. Direct2D开发:纹理混合

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 我们都知道Direct2D可以加载并显示图片,但是不知道你有没有想过,这个2D的图形引擎可以进行纹理混合吗?如果 ...

  2. KeyValue Config

    public class ConfigHelper { public static ScriptsHelper Scripts { get { return new ScriptsHelper(); ...

  3. 概率论 --- Uva 11181 Probability|Given

    Uva 11181 Probability|Given Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.acti ...

  4. 小白学Linux(三)--文件系统基本结构

    Linux文件系统是一个倒立的单根树状结构,文件名称严格区分大小写(windows系统则是对大小写不明感的).路径用“/”分隔,跟windows的“\”不同. 这里我画了一张一般Linux系统的正常目 ...

  5. php获取textarea的值并处理回车换行的方法

    //注:\n是用双引号包的的,双引号!!双引号!!! explode("\n",$row[0]['value']

  6. 回文串--- Girls' research

    HDU   3294 Problem Description One day, sailormoon girls are so delighted that they intend to resear ...

  7. Yii2学习笔记之场景

    场景 一个模型可能在多个场景中使用,在不同的场景中,模型可能使用不同的业务逻辑和规则.例如, User 模型可能在用户登录时使用,也可能在用户注册时使用,某些属性可能在用户注册时强制要求有,在用户登录 ...

  8. php正规则表达式的语法

    界定符的三种书写方式: regexpal工具(正规则表达调试工具): 可以实时显示效果出来. 原子: 可见原子,即uincode编码表中的某个字符 不可见原子: 为了避免编码问题导致匹配不正确,要把文 ...

  9. java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect

    java.sql.SQLException: null, message from server: “Host ‘xxx’ is not allowed to connect 2014年06月29日  ...

  10. javascript基础系列(入门前须知)

    -----------------------小历史---------------------------- javascript与java是两种语言,他们的创作公司不同,JavaScript当时是借 ...