样例输入:

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多线程,多进程编程。

    进程,是目前计算机中为应用程序分配资源的最小单位: 线程,是目前计算机中运行应用程序的最小单位: 在实际系统中,其实进程都是被分为线程来实现的,所以参与时间片轮转的是线程: 但是管理应用程序的资源的单 ...

  2. 水题T,二进制转16进制

    输入一个2进制的数,要求输出该2进制数的16进制表示. 在16进制的表示中,A-F表示10-15 Input第1行是测试数据的组数n,后面跟着n行输入.每组测试数据占1行,包括一个以0和1组成的字符串 ...

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

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

  4. Vue系列之 => 组件切换

    组件切换方式一 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  5. Sitecore CMS中如何命名项目名称

    如何在Sitecore CMS中命名项目,以及配置命名限制,“显示名称”是什么以及如何使用它. 任何其他名称的项目 当创建Sitecore的项目,内容编辑器要求制作者为新建项目提供名称.输入的名称将其 ...

  6. Collections集合工具类的方法

    addAll & shuffle: 返回类型为boolean类型,执行完操作不接收也行: 其中,静态方法,与对象无关,类名点方法名直接调用: 点点点为可变参数,随便填写几个参数都可以: sor ...

  7. Linux基础命令---显示路由表route

    route route指令用于显示或者修改IP路由表.它的主要用途是在使用ifconfig(8)程序配置接口后,通过接口设置到特定主机或网络的静态路由.当使用add或del选项时,路由将修改路由表.如 ...

  8. TensorFire:WEB端的高性能神经网络框架

    TensorFire:WEB端的高性能神经网络框架 摘要: 近日,一种专门用于在网页内执行神经网络算法的JavaScript库——TensorFire引起了人们的关注,这种JavaScript库在浏览 ...

  9. cookie 和 session 的一些事 中间件

    cookie 和 session cookie 1. 保存在浏览器上一组组键值对,服务器让浏览器进行设置. 2. 为什么要用cookie? HTTP协议是无状态.使用cookie保存状态. 3. dj ...

  10. postgresql查询语句

    //查询表名称SELECT tablename FROM pg_tablesWHERE tablename NOT LIKE 'pg%'AND tablename NOT LIKE 'sql_%' O ...