c++隊(duì)列練習(xí)·圖形面積
題目描述
編程計(jì)算由“*”號(hào)圍成的下列圖形的面積。面積計(jì)算方法是統(tǒng)計(jì)*號(hào)所圍成的閉合曲線 中水平線和垂直線交點(diǎn)的數(shù)目。如下圖所示,在 10*10 的二維數(shù)組中,有“*”圍住了 15 個(gè)點(diǎn),因此面積為 15。
?

輸入
一個(gè)10*10的矩陣其中1表示*號(hào)
輸出
一個(gè)數(shù),求被星號(hào)圍起來的面積
樣例輸入
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<bits/stdc++.h>
using
namespace
std;
int
screen[105][105];
typedef
struct
vec
{
????
int
i,j;
}Ract;
queue<Ract> que;
int
di[]={1,0,-1,0};
int
dj[]={0,-1,0,1};
void
flood(
int
i,
int
j)
{
????
int
d;
????
Ract pos{i,j};
????
screen[i][j]=1;
????
que.push(pos);
????
while
(!que.empty())
????
{
????????
pos=que.front();
????????
que.pop();
????????
for
(
int
d=0;d<4;d++)
????????
{
????????????
int
ti=di[d]+pos.i;
????????????
int
tj=dj[d]+pos.j;
????????????
if
(screen[ti][tj]==0&&ti<=10&&tj<=10&&ti>=1&&tj>=1)
????????????
{
????????????????
screen[ti][tj]=1;
????????????????
que.push({ti,tj});
????????????
}
????????
}
????
}
}
int
main()
{
????
int
i,j;
????
for
(
int
i=1;i<=10;i++)
????
{
????????
for
(
int
j=1;j<=10;j++)
????????
{
????????????
scanf
(
"%d"
,&screen[i][j]);
????????
}
????
}
????
for
(
int
i=1;i<=10;i++)
????
{
????????
for
(
int
j=1;j<=10;j++)
????????
{
????????????
if
(i==1||j==1||i==10||j==10)
????????????
{
????????????????
if
(screen[i][j]==0)
????????????????????
flood(i,j);
????????????
}
????????
}
????
}
????
int
cnt=0;
????
for
(
int
i=1;i<=10;i++)
????
{
????????
for
(
int
j=1;j<=10;j++)
????????
{
????????????
if
(screen[i][j]==0)
????????????
{
????????????????
cnt++;
????????????
}
????????
}
????
}
????
printf
(
"%d"
,cnt);
????
return
0;
}
標(biāo)簽: