SPOJ - AMR11J ——(BFS)
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?
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)的更多相关文章
- SPOJ LAS(BFS)题解
题目:VJ 思路: BFS+回溯,但是要剪枝,看了dalao的题解,超时+WA无数发,终于过了 #include<cstdio> #include<cstring> #incl ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
- 层层递进——宽度优先搜索(BFS)
问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...
随机推荐
- JavaScript中的可枚举属性与不可枚举属性
在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的.可枚举性决定了这个属性能否被for…in查找遍历到. 一.怎么判断属性是否可枚举 js中基本包 ...
- RSA_JS_PHP加密解密
root@DESKTOP-I4OIMJC /cygdrive/e/html/RSA_JS_PHP/openssl/bin # ./openssl.exe OpenSSL> genrsa -out ...
- svn关键词BASE, HEAD, COMMITTED, PREV的深入理解
svn关键词BASE, HEAD, COMMITTED, PREV可以很方便用于日常操作中,但是很多人对他们的工作原理和方式不是太了解. 在这里我将使用用例,诠释他们的作用和意图. 先给出svn手册中 ...
- PHP程序连接多个redis实例做缓存
1.redis配置: $CONFIG_REDIS = array( array('host' => '192.168.19.29', 'port' => '6379', 'dbIn ...
- mysql 更改密码
Mac 安装mysql时会生成一个默认密码: 这个可以在通知中找到,如果你需要更改密码则继续看下面, 今天给mac安装了MySQL,安装过程非常的顺利,但是在用一个可视化工具进行连接时,需要输入密码, ...
- 关于java中word转html
http://www.dewen.org/q/5820/java%E4%B8%AD%E6%80%8E%E4%B9%88%E5%B0%86word%E6%96%87%E6%A1%A3%E8%BD%ACh ...
- 机器学习,数据挖掘,统计学,云计算,众包(crowdsourcing),人工智能,降维(Dimension reduction)
机器学习 Machine Learning:提供数据分析的能力,机器学习是大数据时代必不可少的核心技术,道理很简单:收集.存储.传输.管理大数据的目的,是为了“利用”大数据,而如果没有机器学习技术分析 ...
- leetcode341
数据结构设计类题目,参考网上的代码: /** * // This is the interface that allows for creating nested lists. * // You sh ...
- 「小程序JAVA实战」小程序的组件(23)
转自:https://idig8.com/2018/08/11/xiaochengxu-chuji-23/ 开始了解下小程序的组件.源码:https://github.com/limingios/wx ...
- Subversion Self Signed Certificates
When connecting to Subversion repositories using SSL connections the SVN client checks the server ce ...