Counting Squares

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1086    Accepted Submission(s): 540

Problem Description
Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the line

5 8 7 10

specifies the rectangle who's corners are(5,8),(7,8),(7,10),(5,10).

If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.

 
Input
The input format is a series of lines, each containing 4 integers. Four -1's are used to separate problems, and four -2's are used to end the last problem. Otherwise, the numbers are the x-ycoordinates of two points that are opposite corners of a rectangle.

 
Output
Your output should be the number of squares covered by each set of rectangles. Each number should be printed on a separate line.

 
Sample Input
5 8 7 10
6 9 7 8
6 8 8 11
-1 -1 -1 -1
0 0 100 100
50 75 12 90
39 42 57 73
-2 -2 -2 -2
 
Sample Output
8
10000
 
Source
 
import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
boolean ss=false;
while(true){
boolean vis[][]=new boolean[101][101];
int a=input.nextInt();
int b=input.nextInt();
int c=input.nextInt();
int d=input.nextInt();
if(a==-2&&b==-2&&c==-2&&d==-2){
break;
}
boolean ok=false;
int sum=0;
while(!(a==-1&&b==-1&&c==-1&&d==-1)){
for(int i=Math.min(a, c)+1;i<=Math.max(a, c)&&!ok;i++){
for(int j=Math.min(b, d)+1;j<=Math.max(b, d);j++){
if(!vis[i][j]){
sum++;
vis[i][j]=true;
}
}
}
if(sum==10000)
ok=true;
a=input.nextInt();
b=input.nextInt();
c=input.nextInt();
d=input.nextInt();
if(a==-2&&b==-2&&c==-2&&d==-2){
ss=true;
break;
}
}
System.out.println(sum);
if(ss)
break;
}
}
}

Counting Squares_hdu_1264(矩阵).java的更多相关文章

  1. 螺旋矩阵 java实现(待消化)

    import java.util.Scanner; /** * @author:(LiberHome) * @date:Created in 2019/3/4 17:13 * @description ...

  2. [剑指Offer]29-顺时针打印矩阵-Java

    题目链接 https://www.nowcoder.com/practice/9b4c81a02cd34f76be2659fa0d54342a?tpId=13&tqId=11172&t ...

  3. LeetCode 1253. 重构 2 行二进制矩阵 - Java - 统计

    题目链接:https://leetcode-cn.com/contest/weekly-contest-162/problems/reconstruct-a-2-row-binary-matrix/ ...

  4. 19.顺时针打印矩阵 Java

    题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数 ...

  5. 魔术矩阵Java代码

    //该魔术矩阵默认从右上角45度递增 //@漫流——595128841在qq点com //import java.util.Arrays; //用于打印API函数 public class 魔方矩阵 ...

  6. 剑指Offer:面试题20——顺时针打印矩阵(java实现)

    题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数 字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...

  7. leetcode 74 搜索二维矩阵 java

    题目: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: mat ...

  8. LeetCode--054--区螺旋矩阵(java)

    给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...

  9. leetcode.矩阵.766托普里茨矩阵-Java

    1. 具体题目 如果一个矩阵的每一方向由左上到右下的对角线上具有相同元素,那么这个矩阵是托普利茨矩阵.给定一个 M x N 的矩阵,当且仅当它是托普利茨矩阵时返回 True. 示例 1: 输入: ma ...

随机推荐

  1. vi & vim 基本指令(持续更新ing)

    Abstract:1) 文本编辑模式:                      --INSERT--2)一般模式:                      --i.o.a.R--3)命令行命令模式 ...

  2. jQuery选择器 之详述

    jQuery选择器 一. 单词小计 Pervious 上一页sibling  同级first  第一last  最后not  不 Even  偶数    odd  奇数 header  页眉 一.jQ ...

  3. 随机获取oracle数据库中的任意一行数据(rownum)

    最近看oracle资料的时候,了解rownum的概念,以前只知道对数据库表进行简单的增删改查: 看到了rownum的概念后,突然想到了好多业务场景应该都可以适用的,比如在进行随机发奖的时候, 我们就可 ...

  4. CODEVS 1287 矩阵乘法

    1287 矩阵乘法  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 小明最近在为线性代数而头疼,线性代数确实很抽象(也很无聊) ...

  5. 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665

    如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...

  6. uva 469 - Wetlands of Florida

    题目:给你一个矩阵和某些点,找到给的点所处连续的W区域的面积(八个方向). 分析:搜索.floodfill算法,利用搜索直接求解就可以了. 说明:注意读入数据的格式. #include <cst ...

  7. 在PHP中开启CURL扩展,使其支持curl()函数

    在用PHP开发CMS的时候,要用到PHP的curl函数,默认状态下,这个函数需要开启CURL扩展,有主机使用权的,可通过PHP.ini文件开启本扩展,方法如下: 1.打开php.ini,定位到;ext ...

  8. HTML&CSS基础学习笔记1.2-HTML的全局属性?

    HTML元素都有属性,下面的这些全局属性是所有的HTML元素都可以使用的. 常见的有: HTML元素也有一些本身自己独特的属性,我们以后的笔记中有机会,会再为大家介绍哦. 下面是代码测验,具体的内容可 ...

  9. Ubuntu 12.04 更新源

    转载自:http://www.cnblogs.com/eastson/archive/2012/08/24/2654163.html 1.首先备份Ubuntu12.04源列表 sudo cp /etc ...

  10. 向asp.net项目中添加控件AspNetPager

    1.打开项目,把.dll文件放入项目中: 2.在工具栏中添加一个自定义选项卡