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

#include <bits/stdc++.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#define foror(i,a,b) for(i=a;i<b;i++)
#define foror2(i,a,b) for(i=a;i>b;i--)
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 1000000000
#define MOD 1000000007
#define mem(a,b) memset((a),b,sizeof(a))
#define TS printf("!!!\n")
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using ll = long long;
using ull= unsigned long long;
//std::ios::sync_with_stdio(false);
using namespace std;
char s[][];
int visit[][];
int dx[]={-,,,};
int dy[]={,-,,};
int a,b;
int sum;
struct point{
int x,y;
char ch;
int num;
};
queue<point> q;
bool check(point jqk)
{
if((jqk.x>=&&jqk.x<=a)&&(jqk.y>=&&jqk.y<=b)&&(s[jqk.x][jqk.y]!='#')&&(s[jqk.x][jqk.y]!='*'))
return true;
return false;
}
char readchar()
{
char tmp = ;
char ch;
bool read = false;
while (ch = getchar())
{
if ((ch >='a'&&ch<='z')||ch=='#'||ch=='.')
{
read = true;
tmp = ch;
}
else if (read)
{
break;
}
else
{
continue;
}
}
return tmp;
}
void bfs()
{
while(!q.empty())
{
point aim=q.front();
q.pop();
//cout << aim.ch << " ";
if(s[aim.x][aim.y]!='*')
for(int i=;i<=;i++)
{
point cur;
cur.x=aim.x+dx[i];
cur.y=aim.y+dy[i];
cur.ch=aim.ch;
cur.num=aim.num+;
if(check(cur))
{
if(s[cur.x][cur.y]>='a'&&s[cur.x][cur.y]<='z')
{
if(s[cur.x][cur.y]==cur.ch)
continue;
else
{
if(visit[cur.x][cur.y]!=cur.num)
continue;
else
{
s[cur.x][cur.y]='*';
}
}
}
else if(s[cur.x][cur.y]=='.')
{
s[cur.x][cur.y]=cur.ch;
visit[cur.x][cur.y]=cur.num;
q.push(cur);
}
}
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int T;
cin >>T;
while(T--)
{
mem(visit,);
char pop;
scanf("%d %d",&a,&b);
point aim;
for(int i=;i<=a;i++)
scanf("%s",s[i]+);
/*for(int i=1;i<=a;i++)
printf("%s\n",s[i]+1);*/
for(int i=;i<=a;i++)
for(int j=;j<=b;j++)
{
pop=s[i][j];
if(pop!='#'&&pop!='.')
{
aim.x=i;
aim.y=j;
aim.ch=pop;
aim.num=;
q.push(aim);
visit[i][j]=;
}
}
if(!q.empty())
bfs();
for(int i=;i<=a;i++)
printf("%s\n",s[i]+);
printf("\n");
printf("\n"); }
//fclose(stdin);
//fclose(stdout);
return ;
}

Training #2 cell battle (BFS)的更多相关文章

  1. poj 2312 Battle City【bfs+优先队列】

      Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7579   Accepted: 2544 Des ...

  2. B - Battle City bfs+优先队列

    来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...

  3. POJ 2312:Battle City(BFS)

    Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9885   Accepted: 3285 Descr ...

  4. Battle City 优先队列+bfs

    Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...

  5. 2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324 Problem F. Grab The Tree Time Limit: 2000/1000 MS ...

  6. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  7. PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

    1013 Battle Over Cities (25 分)   It is vitally important to have all the cities connected by highway ...

  8. C - Battle City BFS+优先队列

    Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...

  9. 2014 Super Training #6 G Trim the Nails --状态压缩+BFS

    原题: ZOJ 3675 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3675 由m<=20可知,可用一个二进制数表 ...

随机推荐

  1. windows7-maven配置

    1.确认jdk安装 2.下载 3.解压缩 4.配置环境变量 (1)计算机属性--高级系统配置--高级--环境变量---系统变量--新建 (2)添加环境变量 MAVEN_HOME PATH中添加到mav ...

  2. 交换分区swap

    一.查看当前的交换分区[root@server0 ~]# free      -mtotal used free shared buff/cache availableMem: 489 140 145 ...

  3. STL标准模板库之set

    目录 一.关联式容器 二.什么是set? 特点 优缺点和适用场景: 三.定义及初始化 四.基本操作 1)容量函数 2)修改函数 3)迭代器 4)其他函数 五.与序列容器的不同点 一.关联式容器 关联式 ...

  4. 【转载】恢复误删文件--DOS命令应用实例(一)

    <电脑爱好者>报转载第一辑第二篇之恢复误删文件--DOS命令应用实例(一)                             恢复误删文件--DOS命令应用实例(一) 上期我们讲述了 ...

  5. 【图像处理与医学图像处理】NV12与YV12的区别

    用videoCapture和IAMStreamConfig拿到的支持的格式列表.发现支持2中图像格式,YV12和NV12.具体是怎么样的内存分布不知道.查了些文档.自己修改了几个图.看出了点端倪YV1 ...

  6. 前端JS之HTML利用XMLHttpRequest()和FormData()进行大文件分段上传

    用于网页向后端上传大文件 ### 前端代码<body> <input type="file" name="video" id="fi ...

  7. Linux 系统信息查询大全

    Linux常用系统命令 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /etc/redhat-release ...

  8. DataNode 详解及HDFS 2.X新特性

    1. 工作机制 一个数据块在 DataNode 上以文件形式存储在磁盘上,包括两个文件,一个是数据本身,一个是元数据包括数据块的长度,块数据的校验和,以及时间戳. DataNode 启动后向 Name ...

  9. Redis提供的持久化机制

    Redis是一种面向“key-value”类型数据的分布式NoSQL数据库系统,具有高性能.持久存储.适应高并发应用场景等优势.它虽然起步较晚,但发展却十分迅速. 近日,Redis的作者在博客中写到, ...

  10. Go语言操作Redis

    Go语言操作Redis Redis介绍 Redis是一个开源的内存数据库,Redis提供了多种不同类型的数据结构,很多业务场景下的问题都可以很自然地映射到这些数据结构上.除此之外,通过复制.持久化和客 ...