Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

최's 먹공로그

BOJ1018 체스판 다시 칠하기 본문

APS

BOJ1018 체스판 다시 칠하기

ChoiSH313 2022. 5. 22. 17:24

<Solution>

1. 지민이가 만들고 싶은 상태의 체스판과 비교해서 cnt 비교하여 최종 cnt 갱신

2. 검사할 때 +8 할 경우 map을 넘어가지 않는지 검사 필수

 


<Source Code>

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ1018_체스판 {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
		int N = Integer.parseInt(st.nextToken());
		int M = Integer.parseInt(st.nextToken());
		int[][] map = new int[N][M];
		for(int i = 0; i < N; i++)
		{
			String strTemp = br.readLine();
			for(int j = 0; j < M; j++)
			{
				map[i][j] = strTemp.charAt(j) == 'W' ? 0:1;
			}
		}
		
		// 지민이가 바꾸려는 체스판
		int[][] check_map1 = {{0,1,0,1,0,1,0,1},
							  {1,0,1,0,1,0,1,0},
							  {0,1,0,1,0,1,0,1},
							  {1,0,1,0,1,0,1,0},
							  {0,1,0,1,0,1,0,1},
							  {1,0,1,0,1,0,1,0},
							  {0,1,0,1,0,1,0,1},
							  {1,0,1,0,1,0,1,0}};
		int[][] check_map2 = {{1,0,1,0,1,0,1,0},
				 			  {0,1,0,1,0,1,0,1},
				 			  {1,0,1,0,1,0,1,0},
				 			  {0,1,0,1,0,1,0,1},
				 			  {1,0,1,0,1,0,1,0},
				 			  {0,1,0,1,0,1,0,1},
				 			  {1,0,1,0,1,0,1,0},
				 			  {0,1,0,1,0,1,0,1}};
		// map 돌면서 check_map1,2랑 비교해서 바꿔야 되는 애들 카운트 갱신
		int result_cnt = 9999;
		int temp_cnt1 = 0;
		int temp_cnt2 = 0;
		for(int i = 0; i < N; i++)
		{
			for(int j = 0; j < M; j++)
			{
				if(i + 7 < N) {
					if(j + 7 < M) {
						int cmi = -1;
						for(int ii = i; ii < i + 8; ii++)
						{
							cmi++;
							int cmj = -1;
							for(int jj = j; jj < j + 8; jj++)
							{
								cmj++;
								if(map[ii][jj] != check_map1[cmi][cmj])
								{
									temp_cnt1++;
								}
								if(map[ii][jj] != check_map2[cmi][cmj])
								{
									temp_cnt2++;
								}
							}
						}
						result_cnt = temp_cnt1 < result_cnt ? (temp_cnt1 < temp_cnt2 ? temp_cnt1 : temp_cnt2) : temp_cnt2 < result_cnt ? temp_cnt2 : result_cnt;
						temp_cnt1 = 0;
						temp_cnt2 = 0;
					}
					else
						break;
				}
				else
					break;
			}
		}
		System.out.println(result_cnt);
	}
}

 


싸피 때 나보다 퇴화됨....

'APS' 카테고리의 다른 글

BOJ2178 미로탐색  (0) 2022.05.22
BOJ1260 DFS와 BFS  (0) 2022.05.22
BOJ11559_Puyo Puyo C++  (0) 2021.05.23
BOJ11559_Puyo Puyo  (1) 2019.09.05
BOJ17136_색종이 붙이기  (0) 2019.09.03