슈콩

[SWEA] 재미있는 오셀로 게임 본문

Algorithms/SWEA

[SWEA] 재미있는 오셀로 게임

shukong 2025. 11. 7. 23:21

 

 

 

[문제]

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

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

 

[소스 코드]

import java.util.*;
import java.io.*;
public class Solution {
	static int[] dr = {-1,-1,0,1,1,1,0,-1};
	static int[] dc = {0,1,1,1,0,-1,-1,-1};
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		int T = Integer.parseInt(br.readLine());
		for(int tc=1;tc<=T;tc++) {
			st = new StringTokenizer(br.readLine());
			int n = Integer.parseInt(st.nextToken());
			int m = Integer.parseInt(st.nextToken());
			int[][] map = new int[n+1][n+1];
			map[n/2][n/2] = map[n/2+1][n/2+1] = 2;
			map[n/2+1][n/2] = map[n/2][n/2+1] = 1;
			for(int i=0;i<m;i++) {
				st = new StringTokenizer(br.readLine());
				int r = Integer.parseInt(st.nextToken());
				int c = Integer.parseInt(st.nextToken());
				int color = Integer.parseInt(st.nextToken());
				map[r][c] = color;
				for(int d=0;d<8;d++) {
					int nr = r + dr[d];
					int nc = c + dc[d];
					while(nr>0 && nr<=n && nc>0 && nc<=n) {
						if(map[nr][nc]==0) break;
						if(map[nr][nc]!=color) {
							nr += dr[d];
							nc += dc[d];
						}
						else {
							while(!(r==nr && c==nc)) {
								nr -= dr[d];
								nc -= dc[d];
								map[nr][nc] = color;
							}
							break;
						}
					}
				}
			}
			int blackCnt = 0;
			int whiteCnt = 0;
			for(int i=1;i<=n;i++) {
				for(int j=1;j<=n;j++) {
					if(map[i][j]==1) {
						blackCnt++;
					}
					if(map[i][j]==2) {
						whiteCnt++;
					}
				}
			}
			System.out.println("#"+tc+" "+blackCnt+" "+whiteCnt);
		}
	}
}

'Algorithms > SWEA' 카테고리의 다른 글

[SWEA] 오목 판정  (0) 2025.11.09
[SWEA] 7일차 - 암호생성기  (0) 2025.11.09
[SWEA] 진기의 최고급 붕어빵  (0) 2025.11.06
[SWEA] 3일차 - 회문 2  (0) 2025.11.06
[SWEA] 2일차 - Sum  (0) 2025.11.06