SWEA-14413 격자판 칠하기

‍bng4535·2022년 11월 16일
0

문제
https://swexpertacademy.com/main/code/problem/problemSubmitDetail.do

내 풀이
생성되는 격자판의 경우의 수는 두가지 이므로 두 격자판을 만들고 두 격자판을 모두 만족 시킬 수 없을 때 impossible, 그렇지 않으면 poissible

import java.util.Scanner;
class Solution
{
    public static void main(String args[]) throws Exception
    {
     
 
     
        Scanner sc = new Scanner(System.in);
        int T;
        T=sc.nextInt();
     
 
        for(int test_case = 1; test_case <= T; test_case++)
        {
         int n = sc.nextInt(); 
         int m = sc.nextInt(); 
          char[][] test = new char[n][m] ; 
            for(int i=0; i<n; i++) 
                test[i] = sc.next().toCharArray(); 
             
        char[][] arr = new char [n][m];
        char[][] arr2 = new char[n][m]; 
            for(int i=0; i<n; i++) {
                for(int j=0; j<m; j++) 
                {
                    if (i%2==0){
                        if(j%2==0) {arr[i][j] = '#'; arr2[i][j]='.';}
                        else {arr[i][j] = '.'; arr2[i][j] = '#';} 
                    }else{
                        if(j%2==0) {arr[i][j] = '.';  arr2[i][j] = '#';}
                        else {arr[i][j] = '#'; arr2[i][j] = '.';}
                    }
                }
        }
            boolean check1 = true; 
            boolean check2 = true; 
            for(int i=0; i<n; i++) 
                for(int j=0; j<m; j++) {
                    if (test[i][j] =='?' ) continue; 
                    if(test[i][j] != arr[i][j] ) check1 = false; 
                     if(test[i][j] != arr2[i][j] ) check2 = false; 
 
                }
                    
            if(!check1 && !check2) System.out.println("#"+test_case +" impossible");
                else  System.out.println("#"+test_case +" possible");
             
           
         
        }
          
    }
}

다른 풀이
격자판의 성질을 이용하면 간단하게 풀 수 있다.
열과 행의 합이 하나는 홀수 다른 하나는 짝수가 나와야 하며 둘이 동시에 나오는 경우 체스판 형태가 불가능

T = int(input())
for t in range(T):
    N, M = map(int, input().split())
    arr = [list(input()) for _ in range(N)]
    board = [0, 0, 0, 0]
    
    for col in range(N):
        for row in range(M):
            if arr[col][row] == '#':
                if (col+row) % 2 == 0:
                    board[0] += 1
                elif (col+row) % 2 == 1:
                    board[1] += 1
            elif arr[col][row] == '.':
                if (col + row) % 2 == 0:
                    board[2] += 1
                elif (col + row) % 2 == 1:
                    board[3] += 1

    if (board[0] and board[1]) or (board[2] and board[3]) or (board[0] and board[2]) or (board[1] and board[3]):
        answer = 'impossible'
    else:
        answer = 'possible'
        
    print(f"#{t+1}", answer)

소스코드 출처 : https://hei-jayden.tistory.com/311

profile
공부 기록

0개의 댓글