围成面积

题目描述

编程计算由“*”号围成的下列图形的面积。面积计算方法是统计*号所围成的闭合曲线中水平线和垂直线交点的数目。如下图所示,在10×10的二维数组中,有“*”围住了15个点,因此面积为15。

 

输入格式

10×10的图形。

输出格式

输出面积。

输入样例 

0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 0 0 0
0 0 0 0 1 0 0 1 0 0
0 0 0 0 0 1 0 0 1 0
0 0 1 0 0 0 1 0 1 0
0 1 0 1 0 1 0 0 1 0
0 1 0 0 1 1 0 1 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0

输出样例 

15
复制代码
#include<iostream>
using namespace std;
int map[100][100]={};
int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int head=1, tail=1, cnt=0;
struct node{
    int x, y;
}que[10001]; 

void bfs(int x, int y){
    // 入队 
    que[tail].x=1;
    que[tail].y=1;
    map[x][y]=1; 
    tail++;
    while(head<tail){
        // 继续入队。
        for(int i=0; i<=3; i++){
            int tx=que[head].x+dir[i][0];
            int ty=que[head].y+dir[i][1];
            if(map[tx][ty]==0&&tx>=0&&tx<=11&&ty>=0&&ty<=11){ // 此处避免边界都是0,中间是1的情况,所以边界判断的时候范围要扩大。
                que[tail].x=tx;
                que[tail].y=ty;
                map[tx][ty]=1;
                tail++;
            }
        } 
        head++;
    } 
    return;
}

int main(){
    for(int i=1; i<=10; i++)
        for(int j=1; j<=10; j++)
            cin>>map[i][j];
    bfs(1,1); 
    for(int i=1; i<=10; i++)
        for(int j=1; j<=10; j++)
            if(map[i][j]==0)
                cnt++;
    cout<<cnt;
    return 0;
}
复制代码

 

 
posted @   Hi,小董先生  阅读(167)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示