[简单思维题]Snuke's Coloring 2-1

题目描述

There is a rectangle in the xy-plane, with its lower left corner at (0,0) and its upper right corner at (W,H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white.
Snuke plotted N points into the rectangle. The coordinate of the i-th (1≤i≤N) point was (xi,yi).
Then, he created an integer sequence a of length N, and for each 1≤i≤N, he painted some region within the rectangle black, as follows:
If ai=1, he painted the region satisfying x<xi within the rectangle.
If ai=2, he painted the region satisfying x>xi within the rectangle.
If ai=3, he painted the region satisfying y<yi within the rectangle.
If ai=4, he painted the region satisfying y>yi within the rectangle.
Find the area of the white region within the rectangle after he finished painting.

Constraints
1≤W,H≤100
1≤N≤100
0≤xi≤W (1≤i≤N)
0≤yi≤H (1≤i≤N)
W, H (21:32, added), xi and yi are integers.
ai (1≤i≤N) is 1,2,3 or 4.

输入

The input is given from Standard Input in the following format:
W H N
x1 y1 a1
x2 y2 a2
:
xN yN aN

输出

Print the area of the white region within the rectangle after Snuke finished painting.
 

样例输入

5 4 2
2 1 1
3 3 4

样例输出

9

WA代码:
#include<cstdio>
#include<iostream>
using namespace std;

struct A{
 int x,y,op;
}a[1010];

int main()
{
    int r,u,n;
    cin>>r>>u>>n;
    int l=0,d=0;
    for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y>>a[i].op;
    for(int i=1;i<=n;i++){
        if(a[i].op==1&&a[i].x>l) {
                //if(a[i].x<r)
                l=a[i].x;
                //else {printf("0
"); return 0;}
        }
        if(a[i].op==2&&a[i].x<r) {
                //if(a[i].x>l)
                r=a[i].x;
                //else {printf("0
"); return 0;}
        }
        if(a[i].op==3&&a[i].y>d) {
                //if(a[i].y<u)
                d=a[i].y;
                //else {printf("0
"); return 0;}
        }
        if(a[i].op==4&&a[i].y<u) {
                //if(a[i].y>d)
                u=a[i].y;
                //else {printf("0
"); return 0;}
        }
    }
    int ans=(r-l)*(u-d);
    if(ans>=0) cout<<ans<<endl;//这是不行的,若(r-l)和(u-d)都小于0,ans会大于0
    else printf("0
");
    return 0;
}
转载请注明出处:https://www.cnblogs.com/lllxq/
原文地址:https://www.cnblogs.com/lllxq/p/9089557.html