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. Android学习笔记(第二篇)View中的五大布局

    PS:人不要低估自己的实力,但是也不能高估自己的能力.凡事谦为本... 学习内容: 1.用户界面View中的五大布局... i.首先介绍一下view的概念   view是什么呢?我们已经知道一个Act ...

  2. Team Foundation Server简介

    对于任何一个软件开发团队而言,成功的一个重要因素在于成员之间.成员与首先使用软件的用户之间有很好的沟通. Team Foundation Server是一个独立的服务器产品,逻辑上,由下列两层组成,这 ...

  3. [ORM] Entity Framework(1) CodeFirst快速入门

    Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案 对象关系映射(英语:Object Relational Mapping ...

  4. 快捷获取浏览器(navigator对象)的全部属性

    理论:     navigator对象包含关于web浏览器的信息,浏览器的类型,版本信息都可以从该对象获取. 属性 说明 appCodeName 浏览器代码说明 appName 浏览器名称 appVe ...

  5. 一个Linq表达式的扩展函数帮助类

    /// <summary> /// Linq表达式的扩展函数 /// </summary> public static class ExpressionExtensions { ...

  6. 【循序渐进学Python】5.Python常用流程控制及其他语句

    1. 赋值语句常用技巧 通过序列解包(sequence unpacking)可以将多个值的序列解开,让后一一放置到变量的序列中.解包的序列中的元素必须和等号左边变量数量一致.如下: values = ...

  7. windbg学习进阶之——dump分析常用命令收集

    #重要说明 (1) windbg命令分为标准命令,元命令和扩展命令. 标准命令提供最基本的调试功能,不区分大小写.如:bp  g  dt  dv  k等 元命令提供标准命令没有提供的功能,也内建在调试 ...

  8. PHP正则表达式提取超链接及其标题

    有这么一段HTML,比较不规则的,如果要提取其中的链接地址和链接名称,怎么弄? //HTML$str = ''<a id="top8" href="http://l ...

  9. JPHP试用笔记

    JPHP试用指南 编译 环境准备 有JDK 1.6 的环境 Gradle 1.4 以上 具体配置略过,git签出https://github.com/dim-s/jphp/代码后,看readme.md ...

  10. Protocol Buffers动态消息解析

    http://www.searchtb.com/2012/09/protocol-buffers.html http://www.cnblogs.com/jacksu-tencent/p/344731 ...