2713:肿瘤面积

2713:肿瘤面积

总时间限制: 
1000ms 
内存限制: 
65536kB
描述

在一个正方形的灰度图片上,肿瘤是一块矩形的区域,肿瘤的边缘所在的像素点在图片中用0表示。其它肿瘤内和肿瘤外的点都用255表示。现在要求你编写一个程序,计算肿瘤内部的像素点的个数(不包括肿瘤边缘上的点)。已知肿瘤的边缘平行于图像的边缘。

输入
只有一个测试样例。第一行有一个整数n,表示正方形图像的边长。其后n行每行有n个整数,取值为0或255。整数之间用一个空格隔开。已知n不大于1000。
输出
输出一行,该行包含一个整数,为要求的肿瘤内的像素点的个数。
样例输入
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
提示

如果使用静态数组来表示图片数据,需要将该数组定义成全局变量。


#include <stdio.h>
#include <iostream>
#include <stack>
#include <string.h>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <string>
using namespace std;
typedef long long LL;
int a[1001][1001];
int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt","w",stdout);
    int n;
    cin >> n;
    int count = 0;
    for(int i = 0; i < n; i++){
        int x = 0;
        int y = 0;
        for(int j = 0; j < n;j ++){
            int temp;
            cin >> temp;
            if(temp == 0){  //扫描每一行中的0元素
                x++;
            }else if(temp == 255 && x > 0 && x < 2){ //扫描每一行 中 位于0元素之间的255
                y++;
            }
        }
        count += y; //最后结果 +y
    }
    cout << count << endl;
    return 0;
}







: 1、写出设计思路、算法思路。 2、写出程序。 3、运行结果截图。 第1、两倍 给定2到15个不同的正整数,你的任务是计算这些数里面有多少个数对满足:数对中一个 数是另一个数的两倍。比如给定 1 4 3 2 9 7 18 22 得到的答案是3,因为2是1的两倍,4是2的两倍,18是9的两倍。 第2肿瘤面积一个方形的灰度图片上,肿瘤是一块矩形的区域,肿瘤的边缘所在的像素点在图片 中用0表示,其他肿瘤内和肿瘤外的点都用255表示。编写一个程序,计算肿瘤内部的像 素的点的个数(不包括肿瘤边缘上的点)。已知肿瘤的边缘平行于图像的边缘。图像数 据中第一行为图像像素的行数和列数,随后为像素数据。比如,图像数据为 7 14 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 255 255 255 255 255 255 0 255 255 255 255 255 255 0 255 255 255 255 255 255 0 255 255 255 255 255 255 0 255 255 255 255 255 255 0 255 255 255 255 255 255 0 255 255 255 255 255 255 0 0 0 0 0 0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 结果为18。 第3、FBI树 二进制串只能由"0"和"1"组成。将由"0"和"1"组成的字符串分为三类:全"0"串称为B串 ,全"1"串称为I串,既含"0"又含"1"的串则称为F串。 二进制串可以转换为FBI树结构,FBI树是一棵二叉树,在该二叉树中包含F节点、B节点 和I节点三种。 可以将一个长度为2n的二进制串S构造为一棵FBI树T,方法为: T的根结点为R,其类型与串S的类型相同; 若串S的长度大于1,将串S从中间分开,分为等长的左右子串S1和S2;由左子串S1构造R 的左子树T1,由右子串S2构造R的右子树T2。 现在给定一个长度为2n的二进制串,请用上述构造方法构造出一棵FBI树,并输出它的后 序遍历序列。 输入数据有2行,第一行是一个整数N(0<=N<=10),第二行是一个长度为2N的二进制串。 如输入数据为 3 11011000 输出为 IIIBIFFIBFBBBFF 第4(http://poj.org/problem?id=1050) To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25250 Accepted: 13051 Description Given a two-dimensional array of positive and negative integers, a sub- rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle. As an example, the maximal sub-rectangle of the array: 0 -2 -7 0 9 2 -6 2 -4 1 -4 1 -1 8 0 –2 is in the lower left corner: 9 2 -4 1 -1 8 and has a sum of 15. Input The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the ar
在C++中计算肿瘤面积,通常是在方形灰度图片上,肿瘤为矩形区域,边缘像素点用0表示,其他点用255表示,要计算肿瘤内部(不包括边缘)像素点的个数。以下是几种实现方法: ### 方法一:通过判断肿瘤的左上角和右下角位置 该方法主要运用二维数组,通过向双重循环遍历找到左上角的肿瘤位置,通过逆向双重循环遍历找到右下角的肿瘤位置,然后计算肿瘤面积。 ```cpp #include <iostream> using namespace std; int main() { int n,x1,y1,x2,y2; cin>>n; int a[n][n]; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { cin>>a[i][j]; } } int p,q,sign=0; for(p=0; p<n; p++) { for(q=0; q<n; q++) { if(a[p][q]==0&&a[p][q+1]==0&&a[p+1][q]==0) { sign=1; y1=q; break; } } if(sign==1) { x1=p; break; } } sign=0; int u,v; for(u=n-1; u>1; u--) { for(v=n-1; v>1; v--) { if(a[u][v]==0&&a[u][v-1]==0&&a[u-1][v]==0) { sign=1; y2=v; break; } } if(sign==1) { x2=u; break; } } int sum; sum=(x2-x1-1)*(y2-y1-1); cout<<sum; return 0; } ``` ### 方法二:通过一个和最后一个0的坐标 该方法通过遍历二维数组,找到第一个0的坐标和最后一个0的坐标,然后根据坐标计算肿瘤面积。 ```cpp #include <bits/stdc++.h> using namespace std; int n; int a[10005][10005],x,y,p,q,first=1; int main(){ cin>>n; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin>>a[i][j]; if(a[i][j]<first){ first=0; x=i,y=j; } if(a[i][j]==0){ p=i,q=j; } } } cout<<(p-x-1)*(q-y-1); return 0; } ``` ### 方法三:记录起始和结束位置的点 该方法在遍历二维数组时,记录第一个0的位置和最后一个0的位置,最后根据位置计算肿瘤面积。 ```cpp #include <iostream> using namespace std; int main() { int n, arr[1005][1005], ax = 0, ay = 0, bx = 0, by = 0; cin >> n; bool first = true; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> arr[i][j]; if (arr[i][j] == 0) { if(first) { ax = i; ay = j; first = false; } bx = i, by = j; } } } if (bx > ax && by > ay) { cout << (by - ay - 1) * (bx - ax - 1); } else { cout << 0; } return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值