抠图|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)



样例输入:
3
4 5
1 0 0 0 1
1 0 1 0 1
1 0 1 0 1
1 0 0 0 1
5 6
1 1 1 1 1 1
1 0 1 0 1 1
1 0 1 0 1 1
1 0 0 0 1 1
1 1 1 1 1 1
10 10
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 1 0 1 0
1 0 0 0 0 0 1 0 1 0
1 0 0 1 0 0 1 0 0 0
1 0 0 0 0 0 1 1 1 1
1 0 0 0 0 0 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 0 0 0 1 0 0 0
1 1 1 0 1 0 1 0 1 0
1 1 1 0 0 0 1 0 0 0
样例输出:
0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0
思路:找连通分量,置1为0,可以用dfs 或者bfs。首先将4个边界为1的点加入搜索,将与该搜索点相连的1全部置为0。
代码一:dfs
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 505;
int a[N][N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int n, m;
bool isvalid(int x,int y){
if(x <1 || x>n || y <1 || y> m) return false;
return true;
}
//dfs搜索同一连通分量下的 1
void dfs(int x,int y){
a[x][y] = 0;//标记为0
for(int i= 0 ;i<4;i++){
int xx = x + dx[i];
int yy = y + dy[i];
if(isvalid(xx,yy) && a[xx][yy] > 0){
dfs(xx,yy);
}
}
}
int main() {
int _;
scanf("%d", &_);
while (_--) {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
}
}
//边界为1的点满足搜索条件,加入搜索
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] > 0 && (i == 1 || i == n || j == 1 || j == m)) {
dfs(i,j);
}
}
}
//打印数组
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if(j==m)
printf("%d", a[i][j]);
else
printf("%d ", a[i][j]);
}
printf("\n");
}
}
return 0;
}
代码二:bfs
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 505;
int a[N][N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
typedef struct Node {
int x, y;
};
int main() {
int _;
scanf("%d", &_);
while (_--) {
int n, m;
scanf("%d %d", &n, &m);
queue<Node> q;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
//边界为1的点 加入到队列
if (a[i][j] > 0 && (i == 1 || i == n || j == 1 || j == m)) {
q.push({i, j});
a[i][j] = 0;
}
}
}
//bfs搜索这些点的四周 若为1加入队列
while (!q.empty()) {
int x = q.front().x, y = q.front().y;
q.pop();
for (int i = 0; i < 4; i++) {
int tx = x + dx[i];
int ty = y + dy[i];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && a[tx][ty] > 0) {
q.push({tx, ty});
a[tx][ty] = 0;
}
}
}
//输出
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if(j==m)
printf("%d", a[i][j]);
else
printf("%d ", a[i][j]);
}
printf("\n");
}
}
return 0;
}
抠图|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)的更多相关文章
- 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(一)
D题:马的管辖 二进制枚举方案.判断该方案是否全部能被覆盖,将最优方案存下来并进行剪枝. #include<iostream> #include<cstring> #inclu ...
- 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(三)一笔画
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...
- 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(三)数字拆分
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...
- 蒜厂年会|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)
样例输入: 3 1 -2 1 样例输出: 2 方法一: 将环形数组拆分成为普通数组,(通过搬运复制数据到尾部),再求前缀和,找出最大前缀和.因为枚举了每一个起点,所以最大连续和也一定出现在前缀和中.. ...
- 轻重搭配|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)
样例输入: 6 1 9 7 3 5 5 样例输出: 4 思路:贪心,选错贪心思路,只能过一小部分数据,正确贪心思路:从前一半遍历,在后一半中找到比当前元素的两倍大的数(因为这里指针不会后移,所以可以采 ...
- 后缀字符串|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)
样例输入: 3 ba a aba 样例输出: 2 3 1 思路一:暴力,只能过50%数据,枚举每一个字符串,内层枚举其他字符串判断是否以这个字符串为后缀 思路二:哈希表,存储每一个后缀的数目,stri ...
- LIS|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9; int f[N], a[N]; int n; // ...
- 倍数|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)
思路:从l枚举到r肯定超时,这时我们要转变思路!题目让我们求一个区间内的d的倍数,只需要求出r/d - l/d就是区间内d倍数的个数. 代码: #include <iostream> us ...
- 找质数|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)
找质数 思路:数据大,用线性筛,筛选素数表,最后查表:题目让我们查找相加等于n的两个数,那么我们就枚举1个素数a,在素数表中查找是否存在n-a也是素数. 注意事项:数据大,不宜用输入输出流,cout. ...
随机推荐
- C++/JAVA/C#子类调用父类函数情况[留存]
时间久了就容易记不清了,特留存备用查看 c++ 1.构造函数调用 常用初始化列表 或者显示调用 1.1同一个类中构造函数调用构造函数 尽量不要这样做,因为结果不确定!避免麻烦(C++11增加 ...
- codeforces 979C Kuro and Walking Route
题意: 给出一棵树,其中有两个点,x和y,限制走了x之后的路径上不能有y,问可以走的路径(u,v)有多少条,(u,v)和(v,u)考虑为两条不同的路径. 思路: 简单树形dp,dfs统计在x到y路径( ...
- 压缩和解压缩(I)
ZipArchive 压缩方法 -(void)zipArchiveWithFiles { //创建解压缩对象 ZipArchive *zip = [[ZipArchive alloc]init]; / ...
- 解释器模式 Interpreter
代码例子 参考 1.解释器模式定义 给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. 说明:解释器模式设计到文法规则和抽象语法树. 2.解释器模式的结构 ...
- mybatis源码解析8---执行mapper接口方法到执行mapper.xml的sql的过程
上一篇文章分析到mapper.xml中的sql标签对应的MappedStatement是如何初始化的,而之前也分析了Mapper接口是如何被加载的,那么问题来了,这两个是分别加载的到Configura ...
- 区块链区块的生成和链接,比特币btc的产生,UTXO的生成和消耗,比特币系统
区块链区块的生成和链接,比特币btc的产生,UTXO的生成和消耗,比特币系统 区块链区块的生成和链接,比特币btc的产生,UTXO的生成和消耗,比特币系统
- storm的trident编程模型
storm的基本概念别人总结的, https://blog.csdn.net/pickinfo/article/details/50488226 编程模型最关键最难就是实现局部聚合的业务逻辑聚合类实现 ...
- MyEclipse10.7安装Aptana后重启:An internal error has occurred. No more handles [Could not detect registered XULRunner to use]
问题描述: 当安装Aptana插件后重启MyEclipse10.7,发生错误: An internal error has occurred. No more handles [Could not d ...
- The logback manual #02# Architecture
索引 Logback's architecture Logger, Appenders and Layouts Effective Level(有效等级)又名Level Inheritance Ret ...
- 搭建ssm框架
我现在在着手搭建一个项目ssm+angularsJs的框架 以下是目录结构 将所有的依赖全部引入到父工程中,然后在子工程中需要的时候,再引入,父工程只是用来引入依赖 <!-- 集中定义依赖版本号 ...