[BOJ] 3085. 사탕 게임

동스토리 ㅣ 2020. 10. 6. 23:39

반응형

문제

www.acmicpc.net/problem/3085

 

3085번: 사탕 게임

첫째 줄에 상근이가 먹을 수 있는 사탕의 최대 개수를 출력한다.

www.acmicpc.net

 

해설

완전탐색


코드

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

char map[51][51];
int n;
int cnt;
int max_cnt;

void check_a() { //가로

	for (int i = 0; i < n; i++) {
		max_cnt = max(max_cnt, cnt);
		cnt = 1;
		for (int j = 0; j < n; j++) {
			if (j + 1 < n) {
				if (map[i][j] == map[i][j + 1]) {
					cnt++;
				}
				else {
					max_cnt = max(max_cnt, cnt);
					cnt = 1;
				}
			}
		}
	}
}

void check_b() { //세로

	for (int j = 0; j < n; j++) {
		max_cnt = max(max_cnt, cnt);
		cnt = 1;
		for (int i = 0; i < n; i++) {
			if (i + 1 < n) {
				if (map[i][j] == map[i + 1][j]) {
					cnt++;
				}
				else {
					max_cnt = max(max_cnt, cnt);
					cnt = 1;
				}
			}
		}
	}
}

int main() {

	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n;
	char x;
	char temp;
	int max1 = 0, max2 = 0;


	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> x;
			map[i][j] = x;
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {

			if (j + 1 < n) {
				temp = map[i][j];
				map[i][j] = map[i][j + 1];
				map[i][j + 1] = temp;
				check_a();
				check_b();
				temp = map[i][j];
				map[i][j] = map[i][j + 1];
				map[i][j + 1] = temp;
			}
		}
	}

	for (int j = 0; j < n; j++) {
		for (int i = 0; i < n; i++) {

			if (i + 1 < n) {
				temp = map[i][j];
				map[i][j] = map[i + 1][j];
				map[i + 1][j] = temp;
				check_a();
				check_b();
				temp = map[i][j];
				map[i][j] = map[i + 1][j];
				map[i + 1][j] = temp;
			}
		}
	}
	cout << max_cnt;
}

 

 

 

반응형

'Development > Algorithm' 카테고리의 다른 글

[BOJ] 10815. 숫자 카드  (0) 2020.10.17
[BOJ] 1920. 수 찾기  (0) 2020.10.17
[BOJ] 2583. 영역 구하기  (0) 2020.10.06
[BOJ] 11729. 하노이 탑 이동 순서  (0) 2020.10.04
[BOJ] 2606. 바이러스  (0) 2020.10.04