样例输入:

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 组模拟赛(一)的更多相关文章

  1. 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(一)

    D题:马的管辖 二进制枚举方案.判断该方案是否全部能被覆盖,将最优方案存下来并进行剪枝. #include<iostream> #include<cstring> #inclu ...

  2. 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(三)一笔画

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...

  3. 计蒜客 2019 蓝桥杯省赛 B 组模拟赛(三)数字拆分

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...

  4. 蒜厂年会|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    样例输入: 3 1 -2 1 样例输出: 2 方法一: 将环形数组拆分成为普通数组,(通过搬运复制数据到尾部),再求前缀和,找出最大前缀和.因为枚举了每一个起点,所以最大连续和也一定出现在前缀和中.. ...

  5. 轻重搭配|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    样例输入: 6 1 9 7 3 5 5 样例输出: 4 思路:贪心,选错贪心思路,只能过一小部分数据,正确贪心思路:从前一半遍历,在后一半中找到比当前元素的两倍大的数(因为这里指针不会后移,所以可以采 ...

  6. 后缀字符串|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    样例输入: 3 ba a aba 样例输出: 2 3 1 思路一:暴力,只能过50%数据,枚举每一个字符串,内层枚举其他字符串判断是否以这个字符串为后缀 思路二:哈希表,存储每一个后缀的数目,stri ...

  7. LIS|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9; int f[N], a[N]; int n; // ...

  8. 倍数|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    思路:从l枚举到r肯定超时,这时我们要转变思路!题目让我们求一个区间内的d的倍数,只需要求出r/d - l/d就是区间内d倍数的个数. 代码: #include <iostream> us ...

  9. 找质数|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    找质数 思路:数据大,用线性筛,筛选素数表,最后查表:题目让我们查找相加等于n的两个数,那么我们就枚举1个素数a,在素数表中查找是否存在n-a也是素数. 注意事项:数据大,不宜用输入输出流,cout. ...

随机推荐

  1. python 将文件大小转换为human readable 的大小表示

    定义了一个函数, def HRS(size):    units=('B','KB','MB','GB','TB','PB')    for i in range(len(units)-1,-1,-1 ...

  2. Python二分法查找

    1.1二分前提是有序,,否则不可以2分,2分查找的时间复杂度是O(log n):排序后二分查找到适当的位置插入数值 lst = [37,99,73,48,47,40,40,25,99,51] def ...

  3. 如何让多个dz论坛共用一个用户数据库

    用户数据库在论坛中是可以独立备份的,备份方法:论坛后台——站长——数据库,备份所有ucenter数据表,也就是用户数据.其他DZ论坛搭建完成以后,可以上传用户数据库,将备份文件使用上传至网站所使用的主 ...

  4. Rpgmakermv(24 )yep_coreengine

    ==左部为原文,右边我做了简要翻译=================================== Introduction and Instructions ================= ...

  5. ModuleNotFoundError: No module named '_pydevd_bundle.pydevd_cython' error on debug

    现象:pycharm调试代码出现错误:ModuleNotFoundError: No module named '_pydevd_bundle.pydevd_cython' error on debu ...

  6. 20155228 2016-2017-2《Java程序设计》课程总结

    20155228 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1 简要内容 了解「Trainer/Trainee」是最好的师生关系,对即将到来的翻转课堂有 ...

  7. [openjudge-动态规划]怪盗基德的滑翔翼

    题目描述 描述 怪盗基德是一个充满传奇色彩的怪盗,专门以珠宝为目标的超级盗窃犯.而他最为突出的地方,就是他每次都能逃脱中村警部的重重围堵,而这也很大程度上是多亏了他随身携带的便于操作的滑翔翼. 有一天 ...

  8. 什么是Unicode

    写这篇博客的原因, 从做软件开始,什么ASCII码, Unicode,UTF-8,UTF-16,UTF-32......这些鬼东西总是经常碰到,只知道这些鬼是编码格式,其他的就啥都不清楚了,既然总是遇 ...

  9. 2017-2018-2 20165215 实验二 Java面向对象程序设计

    20165215 实验二 Java面向对象程序设计 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:张家佳 学号:20165215 指导教师:娄嘉鹏 实验日期:2018年4月16日 ...

  10. Python数据类型深入学习之数字

    一. 数字常量 1. 下面来看看Python的数字常量中都要哪些类型: 数字 常量 129,-89,0 一般整数 9999848499999L,4594646469l 长整型数(无限大小) 1.232 ...