상세 컨텐츠

본문 제목

[코드트리] 오목

💯ProblemSolving/문제 풀이

by :Eundms 2024. 10. 9. 11:48

본문

       문제 요약        

- 문제

19 x 19 에서 흑/백/결정x 3가지 조건 확인

 

         아이디어        

 

5번 동일한 방향으로 움직여야 함

 

         소스코드        

import java.io.*;
import java.util.*;
public class Main {
    static int[][] way = {{-1,0},{1,0},{0,1},{1,1},{-1,1}};
    static int[][] box;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        box = new int[19][19];
        for (int i = 0; i < 19; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 0; j < 19; j++) {
                box[i][j] = Integer.parseInt(st.nextToken());
            }
        }

        for (int i = 0 ; i < 19; i++) {
            for (int j = 0; j < 19; j++) {
                if (box[i][j] == 0) continue;
                for (int w = 0; w < 5; w++) {
                    if (check(i, j, box[i][j], w)) {
                        int midX = i + 1 + way[w][0] * 2;
                        int midY = j + 1 + way[w][1] * 2;
                        System.out.println(box[i][j]);
                        System.out.println(midX+" "+midY);
                        return;
                    }
                }
            }
        }
        System.out.println(0);
    }
    
    static boolean check(int x, int y, int color, int w) {
        int cnt = 1; // check 함수 부를 때 초기 값이 그거라 
        while(cnt < 5) {
            int nx = x + way[w][0];
            int ny = y + way[w][1];
            if (nx < 0 || nx >= 19 || ny < 0 || ny >= 19)return false;
            if (box[nx][ny] == color) {
                x = nx;
                y = ny;
                cnt += 1;
            } else {
                break;
            }
        }
        return cnt == 5;
    }
    
}
import java.io.*;
import java.util.*;
public class Main {
    static int N, M;
    static char[][] box;
    static int[][] way = {{0,1}, {0,-1}, {1,0}, {-1,0}, {-1,-1}, {1,-1}, {1,1}, {-1,1}};
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());

        box = new char[N][M];
        for (int i = 0; i < N; i++) {
            String line = br.readLine();
            for (int j = 0; j < M; j++) {
                box[i][j] = line.charAt(j);
            }
        }

        int res = 0;

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (box[i][j] == 'L') {
                    for(int w = 0; w < 8; w++) { // 가능한 방향
                        int cnt = 1;
                        int curX = i ; // 시작점
                        int curY = j ;
                        while (true) {
                            int nx = curX + way[w][0]; // 다음 위치 
                            int ny = curY + way[w][1];
                            if(nx < 0 || nx >= N || ny < 0 || ny >= M)break;
                            if (box[nx][ny] != 'E') {
                                break;
                            }
                            cnt += 1;
                            curX = nx;
                            curY = ny;
                        }
                        if(cnt >= 3) {
                            res += 1;
                        }

                    }            
                }
            }
        }
        System.out.println(res);
    }   
}

 

728x90

관련글 더보기

댓글 영역