Spring-2-J Goblin Wars(SPOJ AMR11J)解题报告及测试数据
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
题解:
- 题目的大意是在一块矩形地面上,一开始有很多个部落,也有很多无主地和不能居住的地,那么每个部落每年都会向无主地四周扩张各一块地,如果该地是不能居住的地,则不能扩张,如果同一年两个部落同时扩张到同一块地,那么就会爆发战争,该地无法居住,如果扩张发现那一块地已经有主,则不能扩张。
很显然是广度优先,使用tag[i][j]记录被占领的时间,无主地初始化为0,一开始的部落初始化为1,以后记录每块地被占领的时间。先将原始部落入队,然后再占领其他的地。
情况比较多,仔细考虑,应该没有什么问题。
以下是代码:
#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)解题报告及测试数据的更多相关文章
- Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据
Array Diversity Time Limit:404MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
- Spring-2-B Save the Students(SPOJ AMR11B)解题报告及测试数据
Save the Students Time Limit:134MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descri ...
- Spring-2-A Magic Grid(SPOJ AMR11A)解题报告及测试数据
Magic Grid Time Limit:336MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Tha ...
- sgu 104 Little shop of flowers 解题报告及测试数据
104. Little shop of flowers time limit per test: 0.25 sec. memory limit per test: 4096 KB 问题: 你想要将你的 ...
- Spring-1-I 233 Matrix(HDU 5015)解题报告及测试数据
233 Matrix Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descript ...
- Spring-1-H Number Sequence(HDU 5014)解题报告及测试数据
Number Sequence Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Pro ...
- Spring-1-F Dice(HDU 5012)解题报告及测试数据
Dice Time Limit:1000MS Memory Limit:65536KB Description There are 2 special dices on the table. ...
- Spring-1-A Post Robot(HDU 5007)解题报告及测试数据
Post Robot Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K Problem Description ...
- Winter-2-STL-D The Blocks Problem 解题报告及测试数据
Time Limit:3000MS Memory Limit:0KB Description Background Many areas of Computer Science use sim ...
随机推荐
- Hya.io – 基于 Web 的数字音频工作站
Hya.io 是基于 Web 的音频应用程序,通过 Web MIDI ,音频合成器,音序以及大量的插件来支持硬件 MIDI .您可以添加插件到工作区,将其连接到路由音频,进行播放和实验. HYA 支持 ...
- SQL Server中的事务日志管理(1/9):事务日志概况
当一切正常时,没有必要特别留意什么是事务日志,它是如何工作的.你只要确保每个数据库都有正确的备份.当出现问题时,事务日志的理解对于采取修正操作是重要的,尤其在需要紧急恢复数据库到指定点时.这系列文章会 ...
- 最近一段时间开发客户端app的感悟
关于android和cocos2d 凭着对大学时候写html+css的一点点的记忆,我还是认为android的布局xml文件还是参考了html+css,只是他更加臃肿!就想 android平台本身那样 ...
- 转载:全球首个微信小程序(应用号)开发教程!通宵吐血赶稿,每日更新!
微信应用号(小程序,「应用号」的新称呼)终于来了! 目前还处于内测阶段,微信只邀请了部分企业参与封测.想必大家都关心应用号的最终形态到底是什么样子?怎样将一个「服务号」改造成为「小程序」? 我们暂时以 ...
- log4net日志记录
这里是接着上一篇来优化的,上篇:ASP.NET MVC中错误日志信息记录 log4Net是用来记录日志的,可以将程序运行过程中的信息输出到一些地方(文件,数据库,EventLog等),日志就是程序的黑 ...
- 计算html标签textarea字符长度
今天学习jQuery,做练习计算html标签textarea字符长度,先添加一个视图操作(Action): 创建一个视图,并按下面顺序标记1,2,3进行写html或javascript脚本: 其中标记 ...
- Python基础:函数
一.概述 二.声明.定义和调用 三.参数 1.参数传递 2.实参类型 3.形参绑定 四.返回值 五.名字空间与作用域 1.基本概念 2.名字空间 3.作用域 4.总原则 六.高级 1.装饰器 2.生成 ...
- Titanium开发环境搭建第一个坑
操作系统: Ubuntu 12.04 LTS AMD64 在Titanium Studio中,装Titanium CLI怎么都不能成功,到了一个进度,发现卡在那里,硬盘一直狂闪,发现在Studio的文 ...
- ArrayList、Vector、HashMap、HashTable、HashSet的默认初始容量、加载因子、扩容增量
这里要讨论这些常用的默认初始容量和扩容的原因是: 当底层实现涉及到扩容时,容器或重新分配一段更大的连续内存(如果是离散分配则不需要重新分配,离散分配都是插入新元素时动态分配内存),要将容器原来的数据全 ...
- log4j.xml 配置参数属性level使用心得
jdbc.sqlonly 只显示执行的sql语句.info级才可以显示,debug增加显示java源代码位置. jdbc.sqltiming 显示执行的sql语句以及语句执行时间, ...