1. 程式人生 > >P2956 [USACO09OCT]機器人犁田The Robot Plow

P2956 [USACO09OCT]機器人犁田The Robot Plow

題目描述

Farmer John has purchased a new robotic plow in order to relieve him from the drudgery of plowing field after field after field. It achieves this goal but at a slight disadvantage: the robotic plow can only plow in a perfect rectangle with sides of integer length.

Because FJ's field has trees and other obstacles, FJ sets up the plow to plow many different rectangles, which might end up overlapping. He's curious as to just how many squares in his field are actually plowed after he programs the plow with various plow instructions, each of which describes a rectangle by giving its lower left and upper right x,y coordinates.

As usual, the field is partitioned into squares whose sides are parallel to the x and y axes. The field is X squares wide and Y squares high (1 <= X <= 240; 1 <= Y <= 240). Each of the I (1 <= I <= 200) plow instructions comprises four integers: Xll, Yll, Xur, and Yur (1 <= Xll <= Xur; Xll <= Xur <= X; 1 <= Yll <= Yur; Yll <= Yur <= Y) which are the lower left and upper right coordinates of the rectangle to be plowed. The plow will plow all the field's squares in the range (Xll..Xur, Yll..Yur) which might be one more row and column than would initially be assumed (depending on how you go about your assumptions, of course).

Consider a field that is 6 squares wide and 4 squares high. As FJ issues a pair of plowing instructions (shown), the field gets plowed as shown by '*' and '#' (normally plowed field all looks the same, but '#' shows which were most recently plowed):

......             **....             #####. 
......  (1,1)(2,4) **....  (1,3)(5,4) #####. 
......             **....             **.... 
......             **....             **.... 

A total of 14 squares are plowed.

POINTS: 25

Farmer John為了讓自己從無窮無盡的犁田工作中解放出來,於是買了個新機器人幫助他犁田。這個機器人可以完成犁田的任務,可惜有一個小小的缺點:這個犁田機器人一次只能犁一個邊的長度是整數的長方形的田地。

因為FJ的田地有樹和其它障礙物,所以FJ設定機器人去犁很多不同的長方形。這些長方形允許重疊。他給機器人下了P個指令,每個指令包含一個要犁長方形的地。這片田地由長方形的左下角和右上角座標決定。他很好奇最後到底有多少個方格的地被犁過了。

一般來說,田地被分割為很多小方格。這些方格的邊和x軸或y軸平行。田地的寬度為X個方格,高度為Y個方格 (1 <= X <= 240; 1 <= Y <= 240). FJ執行了I (1 <= I <= 200)個指令,每個指令包含4個整數:Xll, Yll, Xur, Yur (1 <= Xll <= Xur; Xll <= Xur <=X; 1 <= Yll <= Yur; Yll <= Yur <= Y), 分別是要犁的長方形的左下角座標和右上角座標。機器人會犁所有的橫座標在Xll..Xur並且縱座標在Yll..Yur範圍內的所有方格的地。可能這個長方形會比你想象的多一行一列(就是說從第Xll列到第Xur列一共有Xur - Xll + 1列而不是Xur - Xll列)。

考慮一個6方格寬4方格高的田地。FJ進行了2個操作(如下),田地就被犁成"*"和"#"了。雖然一般被犁過的地看起來都是一樣的。但是標成"#"可以更清晰地看出最近一次被犁的長方形。

一共14個方格的地被犁過了。

輸入輸出格式

輸入格式:

 

* Line 1: Three space-separated integers: X, Y, and I

* Lines 2..I+1: Line i+1 contains plowing instruction i which is described by four integers: Xll, Yll, Xur, and Yur

 

輸出格式:

 

* Line 1: A single integer that is the total number of squares plowed

 

輸入輸出樣例

輸入樣例#1: 複製

6 4 2 
1 1 2 4 
1 3 5 4 

輸出樣例#1: 複製

14 

說明

As in the task's example.

 

 

 

這個題用一個數組標記題圖,以及耕過的地標記為1,否則為0.最後統計地圖中1的個數即可。

#include<iostream>
#include<algorithm>
using namespace std;

int map[250][250];
int main() {
	int length,wide;
	int number;
	int ys,ye,xs,xe;
	cin>>length>>wide>>number;
	while(number--){
		cin>>xs>>ys>>xe>>ye;
		for(int i=xs;i<=xe;i++){
			for(int j=ys;j<=ye;j++){
				map[i][j] = 1;
			}
		}
	}
	int ans = 0;
	for(int i=0;i<=length;i++){
		for(int j=0;j<=wide;j++){
			if(map[i][j]==1){
				ans++;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}