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 ...
随机推荐
- Android学习笔记之HttpClient实现Http请求....
PS:最近光忙着考试了....破组成原理都看吐了....搞的什么也不想干...写篇博客爽爽吧....貌似明天就考试了...sad... 学习笔记: 1.如何实现Http请求来实现通信.... 2.解决 ...
- UWP开发入门(十八)——使用ContentControl减少页面元素数量
我们今天学习一下ContentControl,主要介绍如何使用ContentControl搭配DataTemplate来进行界面的复用,以及通过ContentTemplateSelector进一步减少 ...
- 可拖拽的ListBox
之前在写播放器的时候,遇到了一个问题,现在播放器无论是千千,KuGoo还是比较原始的MediaPlayer,它们的播放表都是可以拖拽的,直接把文件拖到播放表实现歌曲的添加那个先暂且不说,光是播放表里面 ...
- VisualStudio代码调试输出跟踪
class EFIntercepterLogging : DbCommandInterceptor { private readonly Stopwatch _stopwatch = new Stop ...
- 判断s2是否能够被通过s1做循环移位(rotate)得到的字符串是否包含
问题:给定两个字符串s1和s2,要求判断s2是否能够被通过s1做循环移位(rotate)得到的字符串包含.例如,S1=AABCD和s2=CDAA,返回true:给定s1=ABCD和s2=ACBD,返回 ...
- ActiveReports 报表应用教程 (2)---清单类报表
在大多报表系统中都有清单类报表的身影,比如:客户清单.商品信息清单.设备清单.物品采购清单.记账凭证.货品发货清单.员工清单等等.清单类报表看视乎比较简单,但是,由清单类报表演变而来的报表类型却十分丰 ...
- 外表cms,内在wiki的系统anwiki
比较完整面向对象的语法格式, 外表cms,内在wiki的系统 http://enanocms.org/features 比较老,php4的语法
- ArrayList集合
//在使用ArrayList时别忘了引用命名空间 using System.Collections;//首先得导入命名空间 //01.添加方法 add方法 //告诉内存,我要存储内容 ArrayLis ...
- DShow实现一个avi视频的播放(含有个人解释和注释)
此项目为win32下的控制台C++代码(别忘记配置DShow库) // movie_test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" ...
- SQL数据库基础(三)
认识数据库备份和事务日志备份 数据库备份与日志备份是数据库维护的日常工作,备份的目的是在于当数据库出现故障或者遭到破坏时可以根据备份的数据库及事务日志文件还原到最近的时间点将损失降到最低点. 数据库备 ...