uva 201 squares

来源:互联网 发布:义乌美工招聘 编辑:程序博客网 时间:2024/05/17 23:53

AC校验 https://vjudge.net/problem/19423

中文题

这里写图片描述

题目

A children’s board game consists of a square array of dots that contains lines connecting some of the
pairs of adjacent dots. One part of the game requires that the players count the number of squares of
certain sizes that are formed by these lines. For example, in the gure shown below, there are 3 squares
| 2 of size 1 and 1 of size 2. (The \size” of a square is the number of lines segments required to form
a side.)
Your problem is to write a program that automates the process of counting all the possible squares.
Input
The input le represents a series of game boards. Each board consists of a description of a square array
of
n
2
dots (where 2

n

9) and some interconnecting horizontal and vertical lines. A record for a
single board with
n
2
dots and
m
interconnecting lines is formatted as follows:
Line 1:
n
the number of dots in a single row or column of the array
Line 2:
m
the number of interconnecting lines
Each of the next
m
lines are of one of two types:
H
i j
indicates a horizontal line in row
i
which connects
the dot in column
j
to the one to its right in column
j
+ 1
or
V
i j
indicates a vertical line in column
i
which connects
the dot in row
j
to the one below in row
j
+ 1
Information for each line begins in column 1. The end of input is indicated by end-of- le. The rst
record of the sample input below represents the board of the square above.
Output
For each record, label the corresponding output with
Problem #1
',

Problem #2
‘, and so forth. Output
for a record consists of the number of squares of each size on the board, from the smallest to the largest.
lf no squares of any size exist, your program should print an appropriate message indicating so. Separate
output for successive input records by a line of asterisks between two blank lines, like in the sample
below.
Sample Input
4
16
H 1 1
H 1 3
H 2 1
H 2 2
H 2 3
H 3 2
H 4 2
H 4 3
V 1 1
V 2 1
V 2 2
V 2 3
V 3 2
V 4 1
V 4 2
V 4 3
2
3
H 1 1
H 2 1
V 2 1
Sample Output
Problem #1
2 square (s) of size 1
1 square (s) of size 2


Problem #2
No completed squares can be found.

import java.util.ArrayList;import java.util.HashMap;import java.util.Scanner;/** * Created by 95112 on 10/10/2017. */public class Squares {    static Point[][] map;    public static void main(String[] args)    {        int amountAnswers = 0;        ArrayList<HashMap> allAnswers = new ArrayList<>();        Scanner scanner = new Scanner(System.in);        while (scanner.hasNextInt()) {            HashMap<Integer,Integer> answers = new HashMap<>();            int n = scanner.nextInt();            int m = scanner.nextInt();            answers.put(-1,n);            answers.put(0,0);            String key = "";            map = new Point[n + 1][n + 1];            for (int i = 0; i < n + 1; i++)                for (int j = 0; j < n + 1; j++)                    map[i][j] = new Point();            int x, y;            for (int i = 0; i < m; i++) {                key = scanner.next();                x = scanner.nextInt();                y = scanner.nextInt();                if (key.equals("H")) {                    map[x][y].right = true;                } else {                    map[y][x].down = true;                }            }            int sum = 0;            for (int i = 1; i < n; i++)                for (int j = 1; j < n; j++) {                    x = i;                    y = j;                    int maxSize = 0;                    int pointSum = 0;                    while (map[x][y].down && (x <= n)) {                        maxSize++;                        x++;                    }                    for (int z = 1; z <= maxSize; z++) {                        if ((i + z) > n || (j + z) > n)                            continue;                        if (checkSquare(i, j, z)) {                            answers.put(0,1);                            if (answers.containsKey(z))                            {                                int count = answers.get(z);                                count ++;                                answers.put(z,count);                            }                            else                            {                                answers.put(z,1);                            }                            pointSum++;                        }                    }                    sum += pointSum;                }                allAnswers.add(answers);            amountAnswers++;        }        for (int i =0; i< amountAnswers ;i++)        {            HashMap<Integer,Integer> answer = allAnswers.get(i);            int cc = i+1;            System.out.println("Problem #"+cc);            System.out.println("");            if (answer.get(0) == 0)            {                System.out.println("No completed squares can be found.");            }            for ( int j = 1; j <= answer.get(-1);j++)            {                if(answer.containsKey(j))                {                    System.out.println(answer.get(j)+" square (s) of size "+j);                }            }            if (i != amountAnswers-1) {                System.out.println("");                System.out.println("**********************************");                System.out.println("");            }        }    }    private static boolean checkSquare(int x,int y,int size)    {        for (int i = 0 ; i < size; i++)        {            if (map[x+i][y].down != true )                return false;            if (map[x+i][y+size].down != true)                return false;            if (map[x][y+i].right != true)                return false;            if (map[x+size][y+i].right !=true)                return false;        }        return true;    }}class Point{    public boolean down = false;    public boolean right = false;}/*44H 1 1H 2 1V 1 1V 2 1416H 1 1H 1 3H 2 1H 2 2H 2 3H 3 2H 4 2H 4 3V 1 1V 2 1V 2 2V 2 3V 3 2V 4 1V 4 2V 4 3 */