样例输入:

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. html5粒子连线

    <html> <head> <title></title> <meta charset="utf-8" /> <s ...

  2. Js闭包学习笔记

    好多内容摘抄了大神的博客内容,只为分享记录.如有冒犯,请见谅 参考文章 http://www.cnblogs.com/libin-1/p/5962269.html http://www.cnblogs ...

  3. kali linux wmtools安装

    1,选择挂载盘时选择自动检测 2,点击安裝vmware tools安裝 3.tar -xzf 壓縮包名 4../vmware-install.pl 5,reboot

  4. win10系统进入BIOS

    按住shift+重启,在重启过程中界面会出现“疑难解答”,点击后,在新的界面点击“高级选项”,之后在新界面上点击“UEFI固件设置”,最后点击重启,重启过程中点击Delete键,就进入了BIOS界面了 ...

  5. CNN那么多的网络有什么区别吗?如何对CNN网络进行修改?

    https://www.zhihu.com/question/53727257/answer/136261195 http://blog.csdn.net/csmqq/article/details/ ...

  6. KL距离,Kullback-Leibler Divergence

    http://www.cnblogs.com/ywl925/p/3554502.html http://www.cnblogs.com/hxsyl/p/4910218.html http://blog ...

  7. 开源词袋模型DBow3原理&源码(二)ORB特征的保存和读取

    util里提供了create_voc_step0用于批量生成features并保存,create_voc_step1读入features再生成聚类中心,比较适合大量语料库聚类中心的生成. 提取一张图的 ...

  8. Flask视图函数报fmalformed url rule错误的原因

    Flask视图函数报fmalformed url rule错误,原因可能是包含中文字符了 把标点符号都重新写一遍英文格式的,可能就不会报这个了

  9. Linux基础命令---ifdown、ifup

    ifup ifup指令用来启动网络接口设备,设备必须是定义在“/etc/sysconfig/network-scripts/ifcfg-ethX”或者“/etc/sysconfig/network”的 ...

  10. golang学习笔记10 beego api 用jwt验证auth2 token 获取解码信息

    golang学习笔记10 beego api 用jwt验证auth2 token 获取解码信息 Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放 ...