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. 递归-PrintDigit

    static void PrintDigit(double n,int len,int count=-1) { if (n <0) { n = System.Math.Abs(n); Conso ...

  2. Git速成学习第四课:解决冲突

    Git速成学习笔记整理于廖雪峰老师的官网网站:https://www.liaoxuefeng.com/ 我们继续练习,准备新的feature1分支. $ git checkout -b feature ...

  3. pacemaker入门

    原文链接:https://blog.csdn.net/a964921988/article/details/82628478 因为数据库部署在Linux上,需要做数据库集群实现高可用,而所有的Post ...

  4. vue-cli3创建vue项目之vue.config.js配置

    module.exports = { // 基本路径 publicPath: '/', // 输出文件目录 outputDir: 'dist', // eslint-loader 是否在保存的时候检查 ...

  5. Sightseeing tour 【混合图欧拉回路】

    题目链接:http://poj.org/problem?id=1637 Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total ...

  6. 学习笔记:CentOS7学习之十六:LVM管理和ssm存储管理器使用

    目录 学习笔记:CentOS7学习之十六:LVM管理和ssm存储管理器使用 16.1 LVM的工作原理 16.1.1 LVM常用术语 16.1.2 LVM优点 16.2 创建LVM的基本步骤 16.2 ...

  7. Design In-Memory File System

    Design an in-memory file system to simulate the following functions: ls: Given a path in string form ...

  8. 使用pycharm开发web——django2.1.5(五)表单和通用视图

    看了刘江老师教程这么多天,卧槽,我才发现他也曾跻身于行伍之间,interesting 刘老师这波讲解很到位,告诉你如何编写单例视图的时候忽然告诉你,其实不用这么麻烦,我们有通用视图,那些总是要做相似的 ...

  9. Photon Server 实现注册与登录(五) --- 服务端、客户端完整代码

    客户端代码:https://github.com/fotocj007/PhotonDemo_Client 服务端代码:https://github.com/fotocj007/PhotonDemo_s ...

  10. django中的缓存 跨域问题(同源策略)

    django缓存机制 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一次的的后台操作 ...