当前位置: 首页 > news >正文

专业网站设计公司哪家好aso优化公司

专业网站设计公司哪家好,aso优化公司,做第三方seo优化网站,网站开发仓库管理系统需求分析题目描述 由数字 0 0 0 组成的方阵中,有一任意形状闭合圈,闭合圈由数字 1 1 1 构成,围圈时只走上下左右 4 4 4 个方向。现要求把闭合圈内的所有空间都填写成 2 2 2。例如: 6 6 6\times 6 66 的方阵( n 6 n6 n6&…

题目描述

由数字 0 0 0 组成的方阵中,有一任意形状闭合圈,闭合圈由数字 1 1 1 构成,围圈时只走上下左右 4 4 4 个方向。现要求把闭合圈内的所有空间都填写成 2 2 2。例如: 6 × 6 6\times 6 6×6 的方阵( n = 6 n=6 n=6),涂色前和涂色后的方阵如下:

0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1

输入格式

每组测试数据第一行一个整数 n ( 1 ≤ n ≤ 30 ) n(1 \le n \le 30) n(1n30)

接下来 n n n 行,由 0 0 0 1 1 1 组成的 n × n n \times n n×n 的方阵。

方阵内只有一个闭合圈,圈内至少有一个 0 0 0

输出格式

已经填好数字 2 2 2 的完整方阵。

样例 #1

样例输入 #1

6
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1

样例输出 #1

0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1

提示

对于 100 % 100\% 100% 的数据, 1 ≤ n ≤ 30 1 \le n \le 30 1n30

一、错误分析

题意就是把被1包围的0改成2。
那么只需要找到包围起来的第一个0的坐标,就可以把所有被包围的0改成2。
第一个0的坐标是第一个1的右下角?
那么就有了下面错误的代码,WA了一个测试点

//错误代码
#include <iostream>
#include <queue>
using namespace std;
int n;
int dir[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int a[33][33];
struct node
{int x, y;
};
int main()
{cin >> n;int sx = 0, sy = 0;for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++){cin >> a[i][j];if (a[i][j] == 1 && sx == 0){sx = i, sy = j;}}sx++;sy++;// bfs广度优先queue<node> q;q.push({sx, sy});while (!q.empty()){node p = q.front();q.pop();for (int i = 0; i < 4; i++){int nx = p.x + dir[i][0], ny = p.y + dir[i][1];if (!(nx>n||ny>n||nx<1||ny<1)&&a[nx][ny] == 0){q.push({nx, ny});a[nx][ny] = 2;}}}for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){cout << a[i][j] << " ";}cout << endl;}
}

那么当墙有厚度的时候,这种找0的方法是错误的。
比如下面这组测试数据。

6 
1 1 1 1 1 1 
1 0 0 0 0 0 
1 1 1 1 1 1 
1 1 0 0 1 1 
1 1 0 0 1 1 
1 1 1 1 1 1 

二、正确分析

先将二维数组初始化为2,将有1的地方改为1,那么被1包围之外的2就是连续的了,只需要使用dfs或bfs就能够把所有包围之外的2改为0。
二维数组需要往外扩展一圈,这样就能保证包围之外的2是连续的。
如[1,n]的区间拓展为[0,n+1].

方法1.DFS

#include <iostream>
#include <queue>
using namespace std;
int n;
int dir[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int a[35][35];
struct node
{int x, y;
};
void dfs(int x,int y)
{if(x>n+1||y>n+1||x<0||y<0) return ;if(a[x][y] == 1||a[x][y]==0) return;a[x][y]=0;for (int i = 0; i < 4; i++){dfs(x + dir[i][0],y + dir[i][1]);}
}
int main()
{for(int i=0;i<33;i++)for(int j=0;j<33;j++){a[i][j]=2;}cin >> n;for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++){int t;cin >> t;if(t==1) a[i][j]=1;}dfs(0,0);for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){cout << a[i][j] << " ";}cout << endl;}
}

方法2.BFS

#include <iostream>
#include <queue>
using namespace std;
int n;
int dir[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int a[35][35];
struct node
{int x, y;
};
int main()
{for(int i=0;i<33;i++)for(int j=0;j<33;j++){a[i][j]=2;}cin >> n;for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++){int t;cin >> t;if(t==1) a[i][j]=1;}queue<node> q;q.push({0, 0});while (!q.empty()){node p = q.front();q.pop();for (int i = 0; i < 4; i++){int nx = p.x + dir[i][0], ny = p.y + dir[i][1];if (!(nx>n+1||ny>n+1||nx<0||ny<0)&&a[nx][ny] == 2){q.push({nx, ny});a[nx][ny] = 0;}}}for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++){cout << a[i][j] << " ";}cout << endl;}
}
http://www.tj-hxxt.cn/news/111227.html

相关文章:

  • 网站建设团队介绍怎么写苏州新闻今天最新消息新闻事件
  • 株洲网站制作百度广告费
  • 童装 技术支持 东莞网站建设网站推广排名优化
  • 网站开发的api全网整合营销推广方案
  • 静安网站建设哪家好广告公司网上接单平台
  • 网站建设第三方平台网站优化软件费用
  • 模板做图 网站有哪些内容mac日本官网入口
  • 电子商务网站建设的目的是开展网络营销台州seo优化
  • 做母婴网站赚钱百度收录提交申请网站
  • 网站做推广有用天堂网长尾关键词挖掘网站
  • 做网站jsp和php中国新冠一共死去的人数
  • 学网站建设有什么用如何优化企业网站
  • 中国建设银行电脑版深圳seo论坛
  • 毕业设计做购物网站如何做个人网站
  • 佛山做网站的哪个好广州seo黑帽培训
  • dz论坛如何做网站地图企业网站托管
  • 怎么做优惠券的网站微博推广方式有哪些
  • 网站搭建代理上google必须翻墙吗
  • 怎么联系网站开发团队网络营销可以做什么工作
  • 网页美工设计素材seo关键字优化价格
  • 网站如何添加浮动窗口新闻发布平台
  • 如何做网站打广告百度关键词收录排名
  • 海口招商建设有限公司网站南宁网站运营优化平台
  • asp.net 多网站中国教师教育培训网
  • 网站开发培训哪里好百度 seo排名查询
  • 银川网站制作公司免费广告
  • 海口网站模板系统优云优客百度推广效果怎么样
  • 甘肃项目信息网厦门seo培训学校
  • 投资做网站如何做好一个品牌推广
  • 免费做网站平台草根seo视频大全