在一个正方形的灰度图片上,肿瘤是一块矩形的区域,肿瘤的边缘所在的像素点在图片中用0表示。其它肿瘤内和肿瘤外的点都用255表示。现在要求你编写一个程序,计算肿瘤内部的像素点的个数(不包括肿瘤边缘上的点)。已知肿瘤的边缘平行于图像的边缘。
Input
只有一个测试样例。第一行有一个整数n,表示正方形图像的边长。其后n行每行有n个整数,取值为0或255。整数之间用一个空格隔开。已知n不大于1000。
Output
输出一行,该行包含一个整数,为要求的肿瘤内的像素点的个数。
Sample
Input | Output |
---|---|
5 255 255 255 255 255 255 0 0 0 255 255 0 255 0 255 255 0 0 0 255 255 255 255 255 255 | 1 |
对于此题,可使用两个嵌套的for循环完成,代码如下:
#include <iostream>
#include <cstring>
using namespace std;
const int M = 1010;
char mp[M][M];
int main()
{
int siz;
cin >> siz;
int stx, sty, edx, edy;
for(int i = 1; i <= siz; i++)
{
for(int j = 1; j <= siz; j++)
{
int n;
cin >> n;
if(n == 255) mp[i][j] = '1';
else mp[i][j] = '0';
if(mp[i][j] == '0')
{
if(mp[i - 1][j] != '0' && mp[i][j - 1] != '0')
stx = i, sty = j;
if(mp[i - 1][j] == '0' && mp[i][j - 1] == '0')
edx = i, edy = j;
}
}
}
int a = edx - stx - 1, b = edy - sty - 1;
cout << a * b;
}