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. System.Security.Cryptography.CryptographicException: 系统找不到指定的文件

    默认为false 改为true

  2. IOZONE测试工具使用方法(转载)

    IOZONE主要用来测试操作系统文件系统性能的测试工具,该工具所测试的范围主要有,write , Re-write, Read, Re-Read, Random Read, Random Write, ...

  3. spring aop实现拦截接口请求打印日志

    在spring配置 1编写自己的注解类 2.编写注解解析类 3.配置spring aop代理 (下面我使用注解 如使用配置 配置切点即可,有两种代理默认jdk代理 设置true 为cglib代理) / ...

  4. 高效率terminal和sublime 相互启动

    在日常的工作中,我们经常使用到terminal和Sublime .今天给大家介绍下怎样高效率的实现terminal和sublime 相互启动 (这里说的是MAC环境,我用的是Sublime Text ...

  5. 【311】Python 构建 ArcMap 标注表达式

    参考:构建标注表达式(官方帮助) 参考:计算字段示例(官方帮助) 说明:以上两者的方法略有不同,一个是通过字段表达式显示标注,一个通过字段计算新的字段,使用的工具方法也不同,前者通过 Layer.la ...

  6. Asp .net core api+Entity Framework core 实现数据CRUD数据库中(附Git地址)

    最近在学dotNetCore 所以尝试了一下api 这个功能 不多说了大致实现如下 1.用vs2017建立一个Asp.net  Core Web 应用程序 在弹出的对话框中选择 Web API 项目名 ...

  7. 在Windows10系统中配置和运行MongoDB数据库,linux开启mongdb

    参考链接:http://jingyan.baidu.com/article/11c17a2c03081ef446e39d02.html linux中开启mongodb服务: 1.  进入到/data/ ...

  8. Linux常用命令----基本文件系统常用命令

    1.查看当前工作目录---pwd sunny@sunny-ThinkPad-T450:~$ pwd /home/sunny sunny@sunny-ThinkPad-T450:~$ cd Worksp ...

  9. Storm配置说明

    配置项 配置说明 storm.zookeeper.servers ZooKeeper服务器列表 storm.zookeeper.port ZooKeeper连接端口 storm.local.dir s ...

  10. Linux实战教学笔记35:企业级监控Nagios实践(下)

    七,服务器端Nagios图形监控显示和管理 前面搭建的Nagios服务虽然能显示信息,能报警.但是在企业工作中还会需要一个历史趋势图,跟踪每一个业务的长期趋势,并且能以图形的方式展示,例如:根据磁盘的 ...