SWEA 4615 재미있는 오셀로 게임

1 개요[ | ]

SWEA 4615 재미있는 오셀로 게임
SW Expert 아카데미
# 문제 풀이

틀:SWEA 난이도 3-3

2 C++[ | ]

3 Java[ | ]

import java.util.*;
public class Solution {
	static int N, c;
	static int[][] board;
	static boolean backtrackFlip(int x, int y, int a, int b) {
		if( x<1 || x>N ) return false;
		if( y<1 || y>N ) return false;
		if( board[y][x] == 0 ) return false;
		if( board[y][x] == c ) return true;
		if( backtrackFlip(x+a, y+b, a, b) ) {
			board[y][x] = c;
			return true;
		}
		return false;
	}
	static void stone(int x, int y) {
		board[y][x] = c;
		// up-left-down-right
		backtrackFlip(x, y-1, 0,-1);
		backtrackFlip(x-1, y,-1, 0);
		backtrackFlip(x, y+1, 0, 1);
		backtrackFlip(x+1, y, 1, 0);
		// rightup-rightdown-leftup-leftdown
		backtrackFlip(x+1, y-1, 1,-1);
		backtrackFlip(x+1, y+1, 1, 1);
		backtrackFlip(x-1, y-1,-1,-1);
		backtrackFlip(x-1, y+1,-1, 1);
	}
    public static void main(String[] args) {
   	Scanner sc = new Scanner(System.in);
    	int T = sc.nextInt();
    	int x, y;
    	int black, white;
    	for(int tc=1; tc<=T; tc++) {
        	N = sc.nextInt();
        	int M = sc.nextInt();
        	board = new int[N+1][N+1];
        	board[N/2][N/2] = 2;
        	board[N/2+1][N/2+1] = 2;
        	board[N/2][N/2+1] = 1;
        	board[N/2+1][N/2] = 1;
        	for(int k=0; k<M; k++) {
        		x = sc.nextInt();
        		y = sc.nextInt();
        		c = sc.nextInt();
        		stone(x,y);
        	}
    		black = 0;
    		white = 0;
    		for(int i=1; i<=N; i++) {
    			for(int j=1; j<=N; j++) {
    				if(board[i][j] == 1) black++;
    				if(board[i][j] == 2) white++;
    			}
    		}
            System.out.format("#%d %d %d\n", tc, black, white);
        }
        sc.close();
    }
}

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}