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

题意:给出了你一个n*n的字符矩阵,由小写字母,‘#’,'.' 组成,小写字母表示国家,‘.’表示未占领地区,‘#’表示不能到达的地区,每一个单位时间内,国家会向它的上下左右四个方向扩张,如果到达的点是‘.’,那这个国家就可以占领这个点,并将它的名字改为自己,如果多个国家同时到达一个点,那这个点就会变成‘ * ’。输出矩阵最终的样子。

思路:这题可以用BFS来做,用队列来模拟每个单位时间所有国家扩张的情况。

将所有可以扩张的点依次压入队列。

在BFS中,同一时间的操作在队列中是靠在一起的,所以可以看成同时进行,当然,还需要用一个visit数组记录下这个元素是第几步操作的结果。

如果你要扩张到的点是‘.’,那代表你可以直接占领它,并将他的visit值在你的基础上加1。

如果你要占领的点已经被占领,但是他的visit值比你大1,表示他是其他点在和你同一步操作时的产物,那你也可以向他扩张,他将变成‘*’;

其余的情况都是不能扩张的。

并且在每个元素出队时,还要判断它是不是‘*’,因为有国家占领一个‘.’时,那这个点就会被直接入队,但它入队后,如果有其他的点也扩张到了它,那它就会在队列中变成‘*’,而‘*’是不会扩张的。

代码:

 #include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define pi 3.141592653589793238462643383279
using namespace std;
struct node{
int x,y;
char country;
}map[][]; int dir[][] = {{,},{,},{-,},{,-}}; //表示扩张的四个方向
int t,n,m,visit[][]; //visit表示这点是第几步操作的产物
queue<node> q; void BFS()
{
node p,t;
while(!q.empty())
{
t = q.front(); //取出队列首元素
q.pop();
if(map[t.x][t.y].country != '*') //如果元素不是多个国家同时占领则可以扩张
{
for(int i=; i<; ++i)
{
p.x = t.x + dir[i][];
p.y = t.y + dir[i][];
if(p.x>= && p.x<n && p.y>= && p.y<m && map[p.x][p.y].country != '#') //如果未超出范围且不为无法到达的区域
{
if(map[p.x][p.y].country == '.') //如果是‘.’表示可以直接占领
{
map[p.x][p.y].country = map[t.x][t.y].country; //将占领的点更新为自己的名字
visit[p.x][p.y] = visit[t.x][t.y] + ; //记录下这个点是第几步的操作
q.push(map[p.x][p.y]); //入队
}
else if(visit[p.x][p.y] == visit[t.x][t.y] + && map[p.x][p.y].country != map[t.x][t.y].country)
//如果这个点已经被其他国家占领但是是在与自己同一步操做中占领的,则变为‘*’
{
map[p.x][p.y].country = '*';
}
}
}
}
}
} int main()
{
cin>>t;
while(t--)
{
memset(visit,,sizeof(visit));
cin>>n>>m;
for(int i=; i<n; ++i)
{
getchar();
for(int j=; j<m; ++j)
{
scanf("%c",&map[i][j].country);
map[i][j].x = i;
map[i][j].y = j;
if(map[i][j].country >= 'a' && map[i][j].country <= 'z') //将国家压入队列
{
visit[i][j] = ;
q.push(map[i][j]);
}
}
}
BFS();
for(int i=; i<n; ++i)
{
for(int j=; j<m; ++j)
cout<<map[i][j].country;
cout<<endl;
}
}
return ;
}
/*
5
3 5
#####
a...b
#####
3 4
####
a..b
####
3 3
#c#
a.b
#d#
3 3
#c#
...
a.b
3 5
.....
.#.#.
a...b
*/

SPOJ - AMR11J ——(BFS)的更多相关文章

  1. SPOJ LAS(BFS)题解

    题目:VJ 思路: BFS+回溯,但是要剪枝,看了dalao的题解,超时+WA无数发,终于过了 #include<cstdio> #include<cstring> #incl ...

  2. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  3. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  4. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  5. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  6. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  7. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  8. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  9. 层层递进——宽度优先搜索(BFS)

    问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...

随机推荐

  1. Cobalt strike 第二节生成报告

    0x00前言: 上一节我们说了怎么连接到服务器 0x01生成报告: 首先打开Cobalt Strike 点击Cobalt Strike -> Preferences Preferences Pe ...

  2. 07_java之练习题

    01奇数求和练习 * A: 奇数求和练习 * a: 题目分析 * 为了记录累加和的值,我们需要定义一个存储累加和的变量 * 我们要获取到1-100范围内的数 * 判断当前数是否为奇数,是奇数,完成累加 ...

  3. NLTK与自然语言处理基础

    NLTK (Natural Language Toolkit) NTLK是著名的Python自然语言处理工具包,但是主要针对的是英文处理.NLTK配套有文档,有语料库,有书籍. NLP领域中最常用的一 ...

  4. 安装完Apache后,配置httpd.conf来使apache来加载php模块

    以apache模块的方式来安装php,在httpd.conf文件中首先使用LoadModule php5_module '.../php5apache2.dll'来动态装载Php模块,然后再用语句Ad ...

  5. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡

    转载:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架 ...

  6. vue之vue-router跳转

    vue之vue-router跳转 一.配置 在初始化使用vue-cli的时候会有vue-router的安装选择,选择安装即可. 二.嵌套路由 一般情况下,登录和项目页面属于同级,所以App.vue 只 ...

  7. django -- url (模版语言 {% url 'test1' param1=5 param2=6 %})

    如果想让form表单提交的url是类似 action="/index-5-6.html" 这样的,可以在html模版语言中使用{% url 'test1' param1=5 par ...

  8. django html模板继承 {%block 标记名} {%endblock%}

    对于url文件 url(r'^tp1/', views.tp1) 对于views文件,跳转到tp1.html 同时将list列表传到前端 def tp1(request): list = [1, 2, ...

  9. Spring -- <context:component-scan>使用说明

    在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...

  10. 什么时候必须使用UI相机? 多个相机的作用原理?

    首先,要从主画布说起,maincanvas,这个有什么限制?主画布是一张默认用来绘制UI的地方,这些UI必须是系统提供的UI组件,在画面下挂一个3D物体或非UI的2D物品是不会被绘制到画布上的,但是仍 ...