[SWEA] 4615. 재미있는 오셀로 게임 D3 - Simulation

제출일 : 2019-11-16

문제 풀이 시간 : 45M

난이도 : ★★☆

Problem

link : https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWQmA4uK8ygDFAXj

input

첫 번째 줄에 테스트 케이스의 수 T가 주어진다.

각 테스트 케이스의 첫 번째 줄에는 보드의 한 변의 길이 N과 플레이어가 돌을 놓는 횟수 M이 주어진다. N은 4, 6, 8 중 하나이다.

그 다음 M줄에는 돌을 놓을 위치와 돌의 색이 주어진다.

돌의 색이 1이면 흑돌, 2이면 백돌이다.

만약 3 2 1이 입력된다면 (3, 2) 위치에 흑돌을 놓는 것을 의미한다.

돌을 놓을 수 없는 곳은 입력으로 주어지지 않는다.

Output

각 테스트 케이스마다 게임이 끝난 후 보드 위의 흑돌, 백돌의 개수를 출력한다.

흑돌이 30개, 백돌이 34인 경우 30 34를 출력한다.

Example

input

10
5
0 1 1 0 0
0 0 1 0 3
0 1 0 1 0
0 0 0 0 0
1 0 5 0 0
5
0 0 1 1 0
0 0 1 0 2
0 0 0 1 0
0 1 0 0 0
1 0 5 0 0
…

output

#1 9
#2 8
…

Solution & Inpression

시뮬레이션 문제

예전에 한번 풀었던 문제여서 수월하게 풀수 있었다.

요즘 시뮬레이션 문제가 어렵게 느껴져 시뮬레이션 문제를 많이 풀어보려 생각중이다

항상 시뮬레이션 문제를 풀때는 문제를 정확히 읽고 요구사항대로 작성을 하면 되지만 쉬운일이 아니다.....

Code

언어 : JAVA

메모리 : 33,700 kb

실행시간 : 165 ms

import java.io.FileInputStream;
import java.util.Scanner;

public class Solution {
    static int N;// 보드 한변의 길이 (4, 6, 8 중 하나)
    static int M;// 플레이어가 돌을 놓는 횟수

    // 상, 하, 좌, 우, 좌상, 우하, 우상, 좌하
    static final int[] dr = { -1, 1, 0, 0, -1, 1, -1, 1 };
    static final int[] dc = { 0, 0, -1, 1, -1, 1, 1, -1 };

    static int[][] map;

    public static void main(String[] args) throws Exception {
        System.setIn(new FileInputStream("res/input.txt"));
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int tc = 1; tc <= T; tc++) {
            N = sc.nextInt();
            M = sc.nextInt();
            init();
            for (int i = 0; i < M; i++) {
                int r = sc.nextInt() - 1;
                int c = sc.nextInt() - 1;
                int player = sc.nextInt();// 1이면 흑돌 2이면 백돌
                put(r, c, player);
            }
            System.out.println("#" + tc + " " + sum());
        } // end of TC
        sc.close();
    }

    private static String sum() {
        int[] res = new int[2];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (map[i][j] == 1)
                    res[0]++;
                else if (map[i][j] == 2)
                    res[1]++;
            }
        }
        return res[0] + " " + res[1];
    }

    private static void init() {
        map = new int[N][N];
        int half = N / 2;
        map[half - 1][half] = 1;
        map[half][half - 1] = 1;
        map[half - 1][half - 1] = 2;
        map[half][half] = 2;
    }

    private static void put(int r, int c, int player) {
        map[r][c] = player;
        for (int dir = 0; dir < 8; dir++) {
            check(r, c, dir);
        }
    }

    private static void check(int r, int c, int dir) {
        int nr = r + dr[dir];
        int nc = c + dc[dir];

        while (true) {
            if (!isRange(nr, nc) || map[nr][nc] == 0)
                break;

            if (map[r][c] != map[nr][nc]) {
                nr += dr[dir];
                nc += dc[dir];
            } else
                break;
        }

        if (isRange(nr, nc) && map[nr][nc] == map[r][c]) {
            while (r != nr || c != nc) {
                map[nr][nc] = map[r][c];
                nr -= dr[dir];
                nc -= dc[dir];
            }
        }

    }

    private static boolean isRange(int r, int c) {
        if (0 <= r && r < N && 0 <= c && c < N)
            return true;
        return false;
    }
}