문제

문제를 처음볼 때 이해가 쉽게 가는 문제이다.  정사각형을 이루는 모든 꼭지점의 수가 같으면 된다.

처음으로 생각 할 수 있는 것은 N과 M중 작은 숫자를 선택을 해야하는데 이 이상의 정사각형이 이루어 진다면 범위를 벗어나는 오류가 발생하기 때문이다. 요즘에 문제를 풀면서 가장 기본적인 것에서 자꾸 오류를 범하는 경우가 매우 많다. 이 문제를 풀면서도 6개의 상태가 같아야 하는데 5개를 넣는 등 작은 실수를 많이한다. 이를 방지하기 위해서 노트에 먼저 적는 습관을 만들기 위해 노력중이다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.*;
 
public class Main{
    public static int result=0;
    public static int [][]arr;
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int N=sc.nextInt();
        int M=sc.nextInt();
        arr=new int [N][M];
        for(int i=0;i<N;i++)
        {
            String temp=sc.next();
            for(int j=0;j<M;j++)
            {
                char al=temp.charAt(j);
                arr[i][j]=((int)al-48);
            }
        }
        int index=Math.min(N,M);
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<M;j++)
            {
                for(int k=0;k<index;k++)
                {
                    if(i+k<&& j+k<M) {
                        int temp1=arr[i][j];
                        int temp2=arr[i+k][j];
                        int temp3=arr[i][j+k];
                        int temp4=arr[i+k][j+k];
                        if(temp1==temp2&& temp1==temp3 && temp3==temp4 &&temp2==temp4 && temp1==temp4 && temp2==temp3)
                        {
                            result=Math.max(result, (k+1)*(k+1));
                        }
                    }
                }
            }
        }
        System.out.println(result);
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-deco
 
 
ration:none">Colored by Color Scripter

 

https://www.acmicpc.net/problem/1051

 

1051번: 숫자 정사각형

N*M크기의 직사각형이 있다. 각 칸은 한 자리 숫자가 적혀 있다. 이 직사각형에서 꼭짓점에 쓰여 있는 수가 모두 같은 가장 큰 정사각형을 찾는 프로그램을 작성하시오. 이때, 정사각형은 행 또는 열에 평행해야 한다.

www.acmicpc.net

 

'알고리즘 > 문제' 카테고리의 다른 글

크로스워드 만들기  (0) 2019.06.30
컵홀더  (0) 2019.06.30
ALPS식 투표  (0) 2019.06.30
대회 or 인턴  (0) 2019.06.30
잃어버린 괄호  (0) 2019.06.30

+ Recent posts