Java实现 蓝桥杯油漆问题

标题:油漆面积

X星球的一批考古机器人正在一片废墟上考古。
该区域的地面坚硬如石、平整如镜。
管理人员为方便,建立了标准的直角坐标系。
每个机器人都各有特长、身怀绝技。它们感兴趣的内容也不相同。

经过各种测量,每个机器人都会报告一个或多个矩形区域,作为优先考古的区域。
矩形的表示格式为(x1,y1,x2,y2),代表矩形的两个对角点坐标。
为了醒目,总部要求对所有机器人选中的矩形区域涂黄色油漆。
小明并不需要当油漆工,只是他需要计算一下,一共要耗费多少油漆。
其实这也不难,只要算出所有矩形覆盖的区域一共有多大面积就可以了。
注意,各个矩形间可能重叠。

本题的输入为若干矩形,要求输出其覆盖的总面积。

输入格式:
第一行,一个整数n,表示有多少个矩形(1<=n<10000)
接下来的n行,每行有4个整数x1 y1 x2 y2,空格分开,表示矩形的两个对角顶点坐标。
(0<= x1,y1,x2,y2 <=10000)
输出格式:
一行一个整数,表示矩形覆盖的总面积。

例如,
输入:
3
1 5 10 10
3 1 20 20
2 7 15 17

程序应该输出:
340

再例如,
输入:
3
5 2 10 6
2 7 12 10
8 1 15 15
程序应该输出:
128

import java.util.Scanner;


public class youqiwenti3 {
	 static int n, sum = 0;
	    static int[][] p = new int[1000][10000];
	    public static void main(String[] args) {
	        Scanner in = new Scanner(System.in);
	        n = in.nextInt();
	        for (int i = 0; i < n; i++) {
	            int x1 = in.nextInt();
	            int y1 = in.nextInt();
	            int x2 = in.nextInt();
	            int y2 = in.nextInt();
	            paint(x1, y1, x2, y2);
	        }
	        for (int i = 0; i < p.length; i++) {
	            for (int j = 0; j < p[i].length; j++) {
	                sum += p[i][j];
	            }
	        }
	        System.out.println(sum);
	    }
	    private static void paint(int x1, int y1, int x2, int y2) {
	        // TODO Auto-generated method stub
	        for (int i = x1; i < x2; i++) {
	            for (int j = y1; j < y2; j++) {
	                p[i][j] = 1;
	            }
	        }
	    }

}

其实还可以用io包来优化,上面的也可以过测试用例,下面这个在io输入上进行了优化

import java.io.BufferedReader;
import java.io.InputStreamReader;


public class youqiwenti {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		// 储存每个矩形的坐标
		int[][] shape = new int[n][4];
		for (int i = 0; i < n; i++) {
			String[] nums = br.readLine().split(" +");
			//下标0为x1,1为y1,2为x2,3为y2
			for (int j = 0; j < 4; j++) {
				shape[i][j] = Integer.parseInt(nums[j]);
			}
		}
		br.close();
		// 求出矩形中最大的x和y坐标值
		int maxX = 0;
		int maxY = 0;
		for (int i = 0; i < n; i++) {
			if (shape[i][0] > maxX) {
				maxX = shape[i][0];
			}
			if (shape[i][2] > maxX) {
				maxX = shape[i][2];
			}
			
			if (shape[i][1] > maxY) {
				maxY = shape[i][1];
			}
			if (shape[i][3] > maxY) {
				maxY = shape[i][3];
			}
		}
 
		// 创建一个数组表示所有区域,同时也是为了记录是否访问过
		boolean[][] visited = new boolean[maxX + 1][maxY + 1];
 
		//依次把矩形放入所有区域
		for (int i = 0; i < n; i++) {
			place(i, shape, visited);
		}
 
		int area = 0;
		//遍历所有区域,计算面积
		for (int i = 0; i < visited.length; i++) {
			for (int j = 0; j < visited[i].length; j++) {
				if (visited[i][j]) {
					area++;
				}
			}
		}
 
		System.out.println(area);
	}
 
	/**
	 * 将矩形点放入所有区域中
	 * 
	 * @param i
	 * @param shape
	 * @param visited
	 */
	private static void place(int i, int[][] shape, boolean[][] visited) {
		// 确保x1,y1分别比x2,y2小
		if (shape[i][0] > shape[i][2]) {
			int temp1 = shape[i][0];
			shape[i][0] = shape[i][2];
			shape[i][2] = temp1;
		}
		
		if (shape[i][1] > shape[i][3]) {
			int temp2 = shape[i][1];
			shape[i][1] = shape[i][3];
			shape[i][3] = temp2;
		}
 
		for (int x = shape[i][0]; x < shape[i][2]; x++) {
			for (int y = shape[i][1]; y < shape[i][3]; y++) {
//				if (!visited[x][y]) {//这里没有必要做判断,做了判断消耗的时间反而更多
					visited[x][y] = true;
//				}
			}
		}
 
	}


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值