当前位置:网站首页>信息学奥赛一本通 1359:围成面积
信息学奥赛一本通 1359:围成面积
2022-06-27 22:21:00 【君义_noip】
【题目链接】
【题目考点】
1. 搜索:连通块问题
【解题思路】
解法1:遍历外圈
遍历整个地图的外圈(第1行、第1列、第10行,第10列),从外圈所有标记为0的位置开始搜索,把搜索到的位置标记为2。
此时所有值为2的位置都是图形外面的位置,值为1的位置是图形的边线,值为0的位置为图形内。统计值为0的位置是数量,即为该图形的面积。
解法2:构造外圈连通块
由于图形的边线就可以在整个地图的外圈,为了让整个图形外面的区域构成一个完整的连通块,我们可以人为扩大地图边界。原来地图的行、列是1到10。现在扩展出第0行、第0列、第11行、第11列,这几行几列的标记都为0。扩展后的行列与原图形外面的位置会形成一个完整的连通块。此时只需要从(0,0)位置开始一次搜索,就可以将整个连通块中每个位置都标记为2。后面还是统计值为0的位置的数量。
本题可以用深搜或广搜来解决。
【题解代码】
解法1:遍历外圈
- 深搜
#include<bits/stdc++.h>
using namespace std;
#define N 15
int n, a[N][N], ans;//a[i][j]:(i,j)位置标记的数字
int dir[4][2] = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};
void dfs(int sx, int sy)
{
for(int i = 0; i < 4; ++i)
{
int x = sx + dir[i][0], y = sy + dir[i][1];
if(x >= 1 && x <= n && y >= 1 && y <= n && a[x][y] == 0)
{
a[x][y] = 2;
dfs(x, y);
}
}
}
int main()
{
n = 10;//地图范围:行列1~n
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
cin >> a[i][j];
for(int i = 1; i <= n; ++i)//遍历外圈
{
if(a[1][i] == 0)
{
a[1][i] = 2;
dfs(1, i);
}
if(a[n][i] == 0)
{
a[n][i] = 2;
dfs(n, i);
}
if(a[i][1] == 0)
{
a[i][1] = 2;
dfs(i, 1);
}
if(a[i][n] == 0)
{
a[i][n] = 2;
dfs(i, n);
}
}
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
{
if(a[i][j] == 0)
ans++;//面积加1
}
cout << ans;
return 0;
}
- 广搜
#include<bits/stdc++.h>
using namespace std;
#define N 15
struct Node
{
int x, y;
Node(){
}
Node(int a, int b):x(a), y(b){
}
};
int n, a[N][N], ans;//a[i][j]:(i,j)位置标记的数字
int dir[4][2] = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};
void bfs(int sx, int sy)
{
queue<Node> que;
a[sx][sy] = 2;
que.push(Node(sx, sy));
while(que.empty() == false)
{
Node u = que.front();
que.pop();
for(int i = 0; i < 4; ++i)
{
int x = u.x + dir[i][0], y = u.y + dir[i][1];
if(x >= 1 && x <= n && y >= 1 && y <= n && a[x][y] == 0)
{
a[x][y] = 2;
que.push(Node(x, y));
}
}
}
}
int main()
{
n = 10;//地图范围:行列1~n
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
cin >> a[i][j];
for(int i = 1; i <= n; ++i)//遍历外圈
{
if(a[1][i] == 0)
bfs(1, i);
if(a[n][i] == 0)
bfs(n, i);
if(a[i][1] == 0)
bfs(i, 1);
if(a[i][n] == 0)
bfs(i, n);
}
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
{
if(a[i][j] == 0)
ans++;//面积加1
}
cout << ans;
return 0;
}
解法2:构造外圈连通块
- 深搜
#include<bits/stdc++.h>
using namespace std;
#define N 15
int n, a[N][N], ans;//a[i][j]:(i,j)位置标记的数字
int dir[4][2] = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};
void dfs(int sx, int sy)
{
for(int i = 0; i < 4; ++i)
{
int x = sx + dir[i][0], y = sy + dir[i][1];
if(x >= 0 && x <= n+1 && y >= 0 && y <= n+1 && a[x][y] == 0)//地图范围:0~n+1
{
a[x][y] = 2;
dfs(x, y);
}
}
}
int main()
{
n = 10;//扩展边界后,地图范围:行列 0~n+1
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
cin >> a[i][j];
a[0][0] = 2;
dfs(0, 0);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
{
if(a[i][j] == 0)
ans++;//面积加1
}
cout << ans;
return 0;
}
- 广搜
#include<bits/stdc++.h>
using namespace std;
#define N 15
struct Node
{
int x, y;
Node(){
}
Node(int a, int b):x(a), y(b){
}
};
int n, a[N][N], ans;//a[i][j]:(i,j)位置标记的数字
int dir[4][2] = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};
void bfs(int sx, int sy)
{
queue<Node> que;
a[sx][sy] = 2;
que.push(Node(sx, sy));
while(que.empty() == false)
{
Node u = que.front();
que.pop();
for(int i = 0; i < 4; ++i)
{
int x = u.x + dir[i][0], y = u.y + dir[i][1];
if(x >= 0 && x <= n+1 && y >= 0 && y <= n+1 && a[x][y] == 0)//地图范围为0~n+1
{
a[x][y] = 2;
que.push(Node(x, y));
}
}
}
}
int main()
{
n = 10;//扩展边界后,地图范围:行列 0~n+1
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
cin >> a[i][j];
bfs(0, 0);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
{
if(a[i][j] == 0)
ans++;//面积加1
}
cout << ans;
return 0;
}
边栏推荐
猜你喜欢

MongoDB-在windows电脑本地安装一个mongodb的数据库

Logging log usage
![[paper reading | deep reading] sdne:structural deep network embedding](/img/6a/b2edf326f6e7ded83deb77219654aa.png)
[paper reading | deep reading] sdne:structural deep network embedding

The limits of Technology (11): interesting programming

【论文阅读|深读】SDNE:Structural Deep Network Embedding

HCIP/HCIE Routing&Switching / Datacom备考宝典系列(十九)PKI知识点全面总结(公钥基础架构)

吴恩达《机器学习》课程总结(13)_聚类

Comprehensive evaluation of free, easy-to-use and powerful open source note taking software

MATLB|改进的前推回代法求解低压配电网潮流

表单form 和 表单元素(input、select、textarea等)
随机推荐
证券注册账户安全吗,会有风险吗?
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
投资场内ETF基金是靠谱吗,场内ETF基金安全吗
[paper reading | deep reading] sdne:structural deep network embedding
Installation and use of Zotero document management tool
Local visualization tool connects to redis of Alibaba cloud CentOS server
Mysql database tourism management system_ Jsp+mysql tourism management system based on SSM [easy to understand]
Is the stock investment exchange group safe? Is it reliable to open an account for free?
云原生运维文章计划
Using two stacks to implement queues [two first in first out is first in first out]
logging日志的使用
用两个栈实现队列[两次先进后出便是先进先出]
股票投资交流群安全吗?入群免费开户靠谱嘛?
代码整洁之道--格式
[untitled]
券商买股票用什么app是比较好的,比较安全的
#796 Div.2 C. Manipulating History 思维
Scu| gait switching and target navigation of micro swimming robot through deep reinforcement learning
Technical debt wall: a way to make technical debt visible and negotiable
What is promise