【编程题解】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!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值