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可知,可用一个二进制数表 ...
随机推荐
- 剑指offer 66. 构建乘积数组(Leetcode 238. Product of Array Except Self)
剑指offer 66. 构建乘积数组 题目: 给定一个数组A[0, 1, ..., n-1],请构建一个数组B[0, 1, ..., n-1],其中B中的元素B[i] = A[0] * A[1] * ...
- 经典卷积神经网络——AlexNet
一.网络结构 AlexNet由5层卷积层和3层全连接层组成. 论文中是把网络放在两个GPU上进行,为了方便我们仅考虑一个GPU的情况. 上图中的输入是224×224224×224,不过经过计算(224 ...
- 洛谷 题解 P1220 【关路灯 】
搜索 传参 inline void DFS(int now,int l,int r,int cnt,int sum,int k) /* now为当前点 l为左端点 r为右端点 cnt为当前耗电量 su ...
- 《Tsinghua os mooc》第1~4讲 启动、中断、异常和系统调用
资源 OS2018Spring课程资料首页 uCore OS在线实验指导书 ucore实验基准源代码 MOOC OS习题集 OS课堂练习 Piazza问答平台 暂时无法注册 疑问 为什么用户态和内核态 ...
- macos 更改罗技k810无线键盘的映射
在mac系统中,command键非常关键,但k810接入后, win键被映射为Command,而Alt的位置却是mac内置键盘的Command的位置. 为方便使用,可以把Win键和Alt键做一个对换. ...
- JavaWeb开发常用的前端控件
罗列的下述控件大多依赖jquery插件,故可提前导入jquery插件以免出错 Validform 提供对表单的验证.提交等功能,具体可查阅其官方文档>>>Validform 示例如下 ...
- [转帖]【Oracle】详解Oracle中NLS_LANG变量的使用
[Oracle]详解Oracle中NLS_LANG变量的使用 https://www.cnblogs.com/HDK2016/p/6880560.html NLS_LANG=LANGUAGE_TERR ...
- springboot @vaule注解失效解决办法
在Controller类里面通过@Value将参数注入进来,最后的确成功了.因此基于此经验,我便在其他使用的类里面也采用这样的方式注入参数,但是发现去失效了,报错为NULL,说明参数并没有我们料想的被 ...
- MySQL修改和查看表类型
//修改表类型alter table verify_code engine = MEMORY;//查看表类型show create table verify_code;
- 一次解决黑帽SEO的经历
最近有个朋友跟我说他的网站被黑了,百度快照里显示的是另一个网站,如: 于是查找了些资料,终于找到了问题所在,记录如下: 关于黑帽SEO1.暗链:其实“暗链”就是看不见的网站链接,“暗链”在网站中的链接 ...