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可知,可用一个二进制数表 ...
随机推荐
- 什么是ECS?
- JAVA数据结构和算法 3-简单排序
排序中的两种基本操作是比较和交换.在插入排序中还有移动. 冒泡排序:两两比较相邻元素,如果较大数位于较小数前面,则交换: 每一趟遍历将一个最大的数移到序列末尾,共遍历N-1趟. 如果执行完一趟之后没有 ...
- SQL FIND_IN_SET() 判断某一个数是否存在于数据表某个以逗号分隔开字段数据中
数据表中的字段存储的是以逗号分隔开的字符串, 例如 (1,2,6,8) 以前不知道这个用法, 碰到比如 8 是否包含在改字符串里面只能一个个取出来, 然后解析成数组,再判断是否在该数组中,效率极低: ...
- PowerPoint储存此文件时发生错误 出现错误的问题解决方法
.单击“文件”,单击“选项”,然后单击“加载项”. . 在管理下拉框中选择“COM加载项”,单击“转到”按钮. . 检查是否存在有任何加载项,清除所有复选框来禁用它们. . 关闭PPT并重新启动,测试 ...
- 【计算机网络】-传输层-Internet传输协议-TCP
[计算机网络]-传输层-Internet传输协议-TCP TCP介绍 在不可靠的互联网上提供一个可靠的端到端字节流 面向连接的.可靠的.端到端的.基于字节流的传输协议 TCP位置 TCP服务模型 应用 ...
- LeetCode 答案(python)1-17
1.给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], targe ...
- 【广搜】Knight Moves
题目描述 Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights fr ...
- prometheus-常用资源对象
监控 Kubernetes 常用资源对象 Prometheus 来自动发现 Kubernetes 集群的节点,用到了 Prometheus 针对 Kubernetes 的服务发现机制kubernete ...
- Memcache与Redis有什么区别?
Redis 和 Memcache 都是基于内存的数据存储系统.Memcached是高性能分布式内存缓存服务,其本质上就是一个内存key-value数据库.Redis是一个开源的key-value存储系 ...
- 关于RESTful API
添几篇文章: 一.What Is REST? 二.RESTful API最佳实践 三.MS Azure——API Design 这些文章里面也推荐了一些关联文章,微软那篇最详细,非常值得一读.