본문 바로가기
SWE/코테

[성실코딩 23일차] 백준 #3184 양 X

by S나라라2 2018. 11. 19.
반응형
#include < iostream >
#include < string >

using namespace std;

int R, C;
int map[251][251];

int dr[4] = {0, 0, 1, -1};  // 동서남북
int dc[4] = {1, -1, 0, 0};

int ans_o = 0;
int ans_v = 0;

int temp_o = 0;
int temp_v = 0;

void goDFS(int _r, int _c) {
	// 양, 늑대 갯수 세기
	if (map[_r][_c]=='o') { // 양
		temp_o++;
	}
	else if (map[_r][_c]=='v') { // 늑대
		temp_v++;
	}
	// 방문 표시
	map[_r][_c] = 1;
	
	// 4방향 이동
	for (int k = 0; k < 4; k++) {
		int nr = _r + dr[k];
		int nc = _c + dc[k];
		if ((nr < 0 || nr >= R) ||
			(nc < 0 || nc >= C)) {  // boundary check
			continue;
		}
		if ( map[nr][nc]!='#' && map[nr][nc] !=1 ) { // 울타리 아니고, 방문한 적도 없으면 dfs
			goDFS(nr, nc);
		}
	}
}

int main() {

	// 입력
	cin >> R >> C;
	for (int r = 0; r < R; r++) {
		string str;
		cin >> str;
		for (int c = 0; c < C; c++) {
			map[r][c] = str[c];
		}
	}

	// 연산
	for (int i = 0; i < R; i++) {
		for (int j = 0; j < C; j++) {
			if (map[i][j]!=-1) {
	
				goDFS(i,j);
				// goDFS방문후에 양,늑대 갯수 비교
				if (temp_o > temp_v) {  // 양이 더 많을 경우, 이김
					ans_o += temp_o;
				}
				else {					// 늑대가 더 많거나 같을 경우, 이김
					ans_v += temp_v;
				}
				// 초기화
				temp_o = 0;
				temp_v = 0;

			}
		}
	}

	cout << ans_o << " " << ans_v << endl;

	return 0;
}
반응형