【编程题解】Workaholic Xiaoming

Description

XiaoMing is a workaholic and has a lot of things to do, so he has an infinite grid pattern. In the initial state, all the grids are blank.

Xiaoming then performs n operations, each of which selects a row or a column, and selects two end point grids on that row or column, populating all the grids in the interval with the two end points with what to do (including the two end points).

How many cells will be filled after n operations? The same cell will be filled only once.

Input

The first line contains a positive integer n.

The next n rows, each containing four integers x1,y1,x2, and y2, represent the lattice coordinates of an operation.

If x1 is equal to x2, it fills a column, and if y1 is equal to y2, it fills a row.

Ensure that each operation is stained on one row or column.

Data range:1≤n≤10000,−10^9≤x1,y1,x2,y2≤10^9

Output

Contains a positive integer representing the number of cells filled.

Sample Input 1 

3
1 2 3 2
2 5 2 3
1 4 3 4

Sample Output 1

8

Personal Answer  (using language:JAVA)  Not necessarily right

import java.math.*;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[][] input = new int[n][4];
        for (int i = 0; i < n; i++) {
            for(int j =0; j<4;j++){
                input[i][j]=scanner.nextInt();
            }
        }

        class Grid{
            private int x;
            private int y;

            public Grid(int x, int y) {
                this.x = x;
                this.y = y;
            }

            @Override
            public boolean equals(Object obj)
            {
                Grid tempGrid= (Grid) obj;
                if (x==tempGrid.x&&y==tempGrid.y) return true;
                else return false;
            }
            @Override
            public int hashCode()
            {
                return new Integer(x).hashCode()+new Integer(y).hashCode();
            }
        }

        HashSet<Grid> hs =new HashSet<Grid>();

        for(int i=0;i<n;i++){
            if(input[i][0]==input[i][2]) {
                if (input[i][1] < input[i][3]) {
                    for (int j = input[i][1]; j <= input[i][3]; j++) {
                        hs.add(new Grid(input[i][0], j));
                    }
                }
                else if(input[i][1] > input[i][3]){
                    for (int j = input[i][3]; j <= input[i][1]; j++) {
                        hs.add(new Grid(input[i][0], j));
                    }
                }
                else{
                    hs.add(new Grid(input[i][0], input[i][1]));
                }
            }else{
                if (input[i][0] <input[i][2]) {
                    for (int j = input[i][0]; j <= input[i][2]; j++) {
                        hs.add(new Grid(j, input[i][1]));
                    }
                }
                else if (input[i][0] >input[i][2]){
                    for (int j = input[i][2]; j <= input[i][0]; j++) {
                        hs.add(new Grid(j, input[i][1]));
                    }
                }
                else{
                    hs.add(new Grid(input[i][0], input[i][1]));
                }
            }
        }

        System.out.println(hs.size());
    }
}

Welcome to communicate!

猜你喜欢

转载自blog.csdn.net/Ximerr/article/details/106751918