본문 바로가기
백준 알고리즘/구현

백준 주사위 윷놀이 C++

by paysmile 2021. 7. 21.

https://www.acmicpc.net/problem/17825

 

17825번: 주사위 윷놀이

주사위 윷놀이는 다음과 같은 게임판에서 하는 게임이다. 처음에는 시작 칸에 말 4개가 있다. 말은 게임판에 그려진 화살표의 방향대로만 이동할 수 있다. 말이 파란색 칸에서 이동을 시작하면

www.acmicpc.net

 

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
vector<pair<int, int>> horse; //길번호, 인덱스
int dice[10];
int answer;
int r1[21] = { 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40 }; // 0->출발점, 42-> 도착점
int r2[3] = { 13,16,19 };
int r3[2] = { 22,24 };
int r4[3] = { 28,27,26 };
int r5[4] = { 25,30,35,40 };
pair<int, int> MoveOneRoot(int i, int j, int count);
pair<int, int> MoveTwoRoot(int i, int j, int count);
pair<int, int> MoveThreeRoot(int i, int j, int count);
pair<int, int> MoveFourRoot(int i, int j, int count);
pair<int, int> MoveFiveRoot(int i, int j, int count);

pair<int, int> MoveOneRoot(int i, int j, int count) {
	pair<int, int> ans;
	if (j == 5) {
		if (count >= 3) ans = MoveTwoRoot(2, 2, count - 3);
		else ans = make_pair(2, count-1);
	}
	else if (j == 10) {
		if (count >= 2) ans = MoveThreeRoot(3, 1, count - 2);
		else ans = make_pair(3, count-1);
	}
	else if (j == 15) {
		if (count >= 3) ans = MoveFourRoot(4, 2, count - 3);
		else ans = make_pair(4, count-1);
	}
	else {
		ans = make_pair(1, j + count);
	}
	return ans;
}

pair<int, int> MoveTwoRoot(int i, int j, int count) {
	pair<int, int> ans;
	if (j + count > 2)	ans = make_pair(5, j + count - 3);
	else ans = make_pair(2, j + count);
	return ans;
}

pair<int, int> MoveThreeRoot(int i, int j, int count) {
	pair<int, int> ans;
	if (j + count > 1)	ans = make_pair(5, j+count-2);
	else ans = make_pair(3, j + count);
	return ans;
}

pair<int, int> MoveFourRoot(int i, int j, int count) {
	pair<int, int> ans;
	if (j + count > 2)	ans = make_pair(5,j+count - 3);
	else ans = make_pair(4, j + count);
	return ans;
}

pair<int, int> MoveFiveRoot(int i, int j, int count) {
	pair<int, int> ans;
	ans = make_pair(5, j + count);
	return ans;
}

void MoveDice(int count, int sum) {
	if (count < 10) {
		for (int k = 0; k < 4; k++) {
			pair<int, int> temp;
			temp = horse[k];
			if (temp == make_pair(0, 0))	continue;

			int movei = horse[k].first;
			int movej = horse[k].second;
			pair<int, int> tmp;
			if (movei == 1) {
				tmp = MoveOneRoot(movei, movej, dice[count]);
			}
			else if (movei == 2) {
				tmp = MoveTwoRoot(movei, movej, dice[count]);
			}
			else if (movei == 3) {
				tmp = MoveThreeRoot(movei, movej, dice[count]);
			}
			else if (movei == 4) {
				tmp = MoveFourRoot(movei, movej, dice[count]);
			}
			else if (movei == 5) {
				tmp = MoveFiveRoot(movei, movej, dice[count]);
			}

			//도착지점 도착시 말 없애기 + 도착지점에 다른말이 있으면 선택 안하기
			if (tmp.first == 1 && tmp.second >= 21) {
				horse[k] = make_pair(0, 0);
				MoveDice(count + 1, sum);
				horse[k] = temp;
			}
			else if (tmp.first == 5 && tmp.second >= 4) {
				horse[k] = make_pair(0, 0);
				MoveDice(count + 1, sum);
				horse[k] = temp;
			}
			else {
				bool flag = false;
				for (int index = 0; index < 4; index++) {
					if (horse[index] == tmp) {
						flag = true;
						break;
					}
					if (tmp.second == 20 && horse[index] == make_pair(5, 3)) {
						flag = true;
						break;
					}
					if (horse[index].second == 20 && tmp == make_pair(5, 3)) {
						flag = true;
						break;
					}
				}
				if (flag == false) {
					horse[k] = tmp;
					switch (tmp.first) {
					case 1:
						MoveDice(count + 1, sum + r1[tmp.second]);
						break;
					case 2:
						MoveDice(count + 1, sum + r2[tmp.second]);
						break;
					case 3:
						MoveDice(count + 1, sum + r3[tmp.second]);
						break;
					case 4:
						MoveDice(count + 1, sum + r4[tmp.second]);
						break;
					case 5:
						MoveDice(count + 1, sum + r5[tmp.second]);
						break;
					}
					horse[k] = temp;
				}
			}
		}
	}
	else {
		answer = max(answer, sum);
	}
}
int main(void) {
	int count = 0;
	int sum = 0;
	for (int i = 0; i < 4; i++) {
		horse.push_back(make_pair(1, 0));
	}
	for (int i = 0; i < 10; i++) {
		cin >> dice[i];
	}
	MoveDice(count, sum);
	cout << answer << endl;
}

'백준 알고리즘 > 구현' 카테고리의 다른 글

마법사 상어와 블리자드 C++  (0) 2021.09.18
백준 모노미노도미노 2 C++  (0) 2021.07.26
마법사 상어와 비바라기  (0) 2021.06.28
상어 중학교  (0) 2021.06.28
새로운 게임 2  (0) 2021.04.20