자바로 백준 1025 풀기

hong030·2023년 8월 17일
0

풀이)
1. 왼쪽 아래에서 위로 올라가는 형태
2. 왼쪽 위에서 아래로 내려가는 형태
3. 오른쪽 아래에서 위로 올라가는 형태
4. 오른쪽 위에서 아래로 내려가는 형태
5. 행만 움직이는 형태
6. 열만 움직이는 형태
이 6가지 유형을 만족하는 모든 수를 찾기 위해 브루스포스 방식으로 코드를 짠다.

코드)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
 
 
public class Main {
 
    static int n, m;
    static int[][] arr;
    static int result = -1;
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
 
        arr = new int[10][10];
 
        for (int i = 0; i < n; i++) {
            String s1 = br.readLine();
            for (int j = 0; j < m; j++) {
                arr[i][j] = Integer.parseInt(String.valueOf(s1.charAt(j)));
            }
        }
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < m; ++j)
                for (int mi = -n; mi < n; ++mi)
                    for (int mj = -m; mj < m; ++mj)
                    {
                        if (mi == 0 && mj == 0) { // 둘다 움직이지 않을 때
                            continue;
                        }
 
                        int t = 0;
                        int newI = i;
                        int newJ = j;
                        while (newI >= 0 && newI < n && newJ >= 0 && newJ < m) // 위치가 0>= && <범위를 설정해줍니다.
                        {
                            t = 10 * t + arr[newI][newJ]; // 기존에 담긴 숫자가 있다면 *10해주고 더해줍니다. 
                            if (Math.abs(Math.sqrt(t) - (int)Math.sqrt(t)) < 1e-10){ // 완전 제곱수인지 판별합니다.
                                result = Math.max(result, t);
                            }
                            newI += mi; /
                            newJ += mj;
                        }
 
                    }
        System.out.println(result);
    }
}
profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

1개의 댓글

comment-user-thumbnail
2023년 8월 17일

즐겁게 읽었습니다. 유용한 정보 감사합니다.

답글 달기