Training #2 cell battle (BFS)
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)的更多相关文章
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- B - Battle City bfs+优先队列
来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...
- POJ 2312:Battle City(BFS)
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9885 Accepted: 3285 Descr ...
- Battle City 优先队列+bfs
Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...
- 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 ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- 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 ...
- C - Battle City BFS+优先队列
Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...
- 2014 Super Training #6 G Trim the Nails --状态压缩+BFS
原题: ZOJ 3675 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3675 由m<=20可知,可用一个二进制数表 ...
随机推荐
- django 之(五) --- 验证码|富文本|邮箱短信
验证码 在用户登录,注册以及一些敏感操作的时候,我们为了防止服务器被暴力请求,或爬虫爬取,我们可以使用验证码进行过滤,减轻服务器的压力. 原生实现: 库名:pip install Pillow ...
- 转发表(MAC表)、ARP表、路由表总结
原文:https://cloud.tencent.com/developer/article/1173761 转发表(MAC表).ARP表.路由表总结 我是东东东 发表于我是东东强订阅 1.5K ...
- Blynk系列随笔
Blynk系列随笔 1.基于Blynk服务器搭建物联网测试Demo 2.本地 Blynk服务器搭建
- 使用tensorflow训练SSD(一):相关环境的配置
在使用TensorFlow进行目标检测时,首先需要下载tensorflow object detection API模型,该模型的下载地址为https://github.com/tensorflow/ ...
- python知识储备目录
1.with as 的实现原理 https://www.cnblogs.com/lice-blog/p/11581741.html 2.事务的四个隔离级别 3.rabitMQ 4.c3算法 5.wit ...
- 环境变量配置文件profile
环境变量配置文件 在Ubuntu中有如下几个文件可以设置环境变量1./etc/profile:在登录时,操作系统定制用户环境时使用的第一个文件,此文件为系统的每个用户设置环境信息,当用户第一次登录时, ...
- gRPC安装的小问题
INSTALL.md提到下述前提条件 #Pre-requisites ##Linux ```sh $ [sudo] apt-get install build-essential autoconf l ...
- java中Super到底是什么意思?必须举例说明!
[学习笔记] 3."超"关键字(super keyword) Super是一个参考(或说指针)指向他紧邻的父类(见下面的例子).用super可以指向被隐藏的父类的同名成员. 3.1 ...
- Pandas 读取超过 65536 行的 Excel 文件
Excel 文件的格式曾经发生过一次变化,在 Excel 2007 以前,使用扩展名为 .xls 格式的文件,这种文件格式是一种特定的二进制格式,最多支持 65,536 行,256 列表格.从 Exc ...
- 什么是云解析DNS?
产品概述 云解析DNS(Alibaba Cloud DNS)是一种安全.快速.稳定.可扩展的权威DNS服务,云解析DNS为企业和开发者将易于管理识别的域名转换为计算机用于互连通信的数字IP地址,从而将 ...