求面积

求面积

题目描述:

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

1493190158743349178.jpeg

输入格式:

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

提示:

建议用深搜(回溯)和广搜各做一次。

时间限制:1000ms
空间限制:128MByte

#include<bits/stdc++.h>
using namespace std;
int ans, x, y, x1, y2;
int b[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
bool ch[15][15];

int main()
{
    for(int i =1; i <= 10; i++)
        for(int j =1; j <= 10; j++) cin>>ch[i][j];
    queue<int> q;
    q.push(0), q.push(0);
    while(!q.empty()){
        x = q.front();
        q.pop();
        y = q.front();
        q.pop();
        for(int i =0; i <4; i++){
            x1 = x + b[i][0], y2 = y + b[i][1];
            if(x1 >=0 && x1 <= 11 && y2 >= 0 && y2 <= 11 && ch[x1][y2] == 0){
                ch[x1][y2] = 1;
                q.push(x1), q.push(y2);
            }
        }
    }
    for(int i =1; i <= 10; i++)
        for(int j =1; j <= 10; j++) if(!ch[i][j]) ans++;
    cout<<ans;
    return 0;
}

 

posted @ 2018-07-13 14:40  wbss  阅读(533)  评论(0编辑  收藏  举报