1. 程式人生 > >CSP201409-2:畫圖

CSP201409-2:畫圖

info 考研 中國 alloc 測試 gin ccf efi amp

引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中國計算機學會(CCF)發起的"計算機職業資格認證"考試,針對計算機軟件開發、軟件測試、信息管理等領域的專業人士進行能力認證。認證對象是從事或將要從事IT領域專業技術與技術管理人員,以及高校招考研究生的復試對象。

  • 問題描述

  在一個定義了直角坐標系的紙上,畫一個(x1,y1)(x2,y2)的矩形指將橫坐標範圍從x1x2,縱坐標範圍從y1y2之間的區域塗上顏色。

  下圖給出了一個畫了兩個矩形的例子。第一個矩形是(1,1) (4, 4),用綠色和紫色表示。第二個矩形是

(2, 3)(6, 5),用藍色和紫色表示。圖中,一共有15個單位的面積被塗上顏色,其中紫色部分被塗了兩次,但在計算面積時只計算一次。在實際的塗色過程中,所有的矩形都塗成統一的顏色,圖中顯示不同顏色僅為說明方便。

技術分享圖片

  給出所有要畫的矩形,請問總共有多少個單位的面積被塗上顏色。

  • 輸入格式

  輸入的第一行包含一個整數n,表示要畫的矩形的個數。

  接下來n行,每行4個非負整數,分別表示要畫的矩形的左下角的橫坐標與縱坐標,以及右上角的橫坐標與縱坐標。

  • 輸出格式

  輸出一個整數,表示有多少個單位的面積被塗上顏色。

  • 樣例輸入

    2

    1 1 4 4

    2 3 6 5

  • 樣例輸出

    15

  • 評測用例規模與約定

    1<=n<=1000<=橫坐標、縱坐標<=100

  • 源代碼

# include <stdio.h>

# include <stdlib.h>

# include <memory.h>

# define N 101

//坐標

struct pos {

int x1;

int y1;

int x2;

int y2;

};

int main(void)

{

int n; //矩形個數

int area = 0;

int canvas[N][N] = {0}; //坐標系中所有的點,0代表未被染色,1代表已經被染色

scanf("%d", &n);

pos *pPst = (pos *)malloc(sizeof(pos) * n);

memset(pPst, 0, sizeof(pos) * n);

for (int i = 0; i < n; i++)

{

scanf("%d", &(pPst[i].x1));

scanf("%d", &(pPst[i].y1));

scanf("%d", &(pPst[i].x2));

scanf("%d", &(pPst[i].y2));

}

for (int i = 0; i < n; i++)

{

for (int m = pPst[i].x1; m < pPst[i].x2; m++)

{

for (int n = pPst[i].y1; n < pPst[i].y2; n++)

{

if (canvas[m][n] == 0)

{

area += 1;

canvas[m][n] = 1;

}

}

}

}

printf("%d\n", area);

free(pPst);

return 0;

}

CSP201409-2:畫圖