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

주사위 윷놀이 C++

by paysmile 2021. 4. 20.

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

#include <iostream>
#include <vector>

using namespace std;
int dice[10];
pair<int, int> horse[4]; //루트, 인덱스
int way1[22] = { 0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,41 };
int way2[9] = { 10,13,16,19,25,30,35,40,41};
int way3[8] = { 20,22,24,25,30,35,40,41};
int way4[9] = { 30,28,27,26,25,30,35,40,41};
int way5[5] = { 25,30,35,40,41 };
vector<pair<int,int>> v;

int MoveDice(int count, int answer) {
	int value = answer;

	if (count >= 10)
		return answer;

	for (int i = 0; i < 4; i++) {
		if (horse[i] == make_pair(0, 0))	continue;
		pair<int, int> temp = horse[i];

		int movei = horse[i].second + dice[count];

		if (horse[i].first == 1) {
			bool flag = false;
			if (movei > 20) {
				v.push_back({ 0,0 });
				horse[i] = make_pair(0, 0);
				int temp_value = MoveDice(count + 1, answer);
				if (temp_value > value)	value = temp_value;
				horse[i] = temp;
				v.pop_back();
				continue;
			}

			if (movei == 20)	horse[i] = make_pair(5, 3);
			else if (movei == 5)	horse[i] = make_pair(2, 0);
			else if (movei == 10)	horse[i] = make_pair(3, 0);
			else if (movei == 15)	horse[i] = make_pair(4, 0);
			else horse[i] = make_pair(1, movei);

			for (int k = 0; k < 4; k++) {
				if (horse[k] == horse[i] && k != i) {
					flag = true;
					break;
				}
			}

			if (flag == true)	horse[i] = temp;
			else {
				v.push_back({ 1,movei });
				int temp_value = MoveDice(count + 1, answer + way1[movei]);
				if (temp_value > value)	value = temp_value;
				horse[i] = temp;
				v.pop_back();
			}
		}
		else if (horse[i].first == 2) {
			bool flag = false;

			if (movei > 7) {
				v.push_back({ 0,0 });
				horse[i] = make_pair(0, 0);
				int temp_value = MoveDice(count + 1, answer);
				if (temp_value > value)	value = temp_value;
				horse[i] = temp;
				v.pop_back();
			}
			else {
				if (movei == 4)	horse[i] = make_pair(5, 0);
				else if (movei == 5)	horse[i] = make_pair(5, 1);
				else if (movei == 6)	horse[i] = make_pair(5, 2);
				else if (movei == 7)	horse[i] = make_pair(5, 3);
				else {
					horse[i] = make_pair(2, movei);
				}
				for (int k = 0; k < 4; k++) {
					if (horse[k] == horse[i] && k != i) {
						flag = true;
						break;
					}
				}
				if (flag == true) {
					horse[i] = temp;
				}
				else {
					v.push_back({ 2,movei });
					int temp_value = MoveDice(count + 1, answer + way2[movei]);
					if (temp_value > value)	value = temp_value;
					horse[i] = temp;
					v.pop_back();
				}
			}
		}
		else if (horse[i].first == 3) {
			bool flag = false;

			if (movei > 6) {
				v.push_back({ 0,0 });
				horse[i] = make_pair(0, 0);
				int temp_value = MoveDice(count + 1, answer);
				if (temp_value > value)	value = temp_value;
				horse[i] = temp;
				v.pop_back();
			}
			else {
				if (movei == 3)	horse[i] = make_pair(5, 0);
				else if (movei == 4)	horse[i] = make_pair(5, 1);
				else if (movei == 5)	horse[i] = make_pair(5, 2);
				else if (movei == 6)	horse[i] = make_pair(5, 3);
				else {
					horse[i] = make_pair(3, movei);
				}
				for (int k = 0; k < 4; k++) {
					if (horse[k] == horse[i] && k != i) {
						flag = true;
						break;
					}
				}
				if (flag == true) {
					horse[i] = temp;
				}
				else {
					v.push_back({ 3,movei });
					int temp_value = MoveDice(count + 1, answer + way3[movei]);
					if (temp_value > value)	value = temp_value;
					horse[i] = temp;
					v.pop_back();
				}
			}
		}
		else if (horse[i].first == 4) {
			bool flag = false;

			if (movei > 7) {
				v.push_back({ 0,0 });
				horse[i] = make_pair(0, 0);
				int temp_value = MoveDice(count + 1, answer);
				if (temp_value > value)	value = temp_value;
				horse[i] = temp;
				v.pop_back();
			}
			else {
				if (movei == 4)	horse[i] = make_pair(5, 0);
				else if (movei == 5)	horse[i] = make_pair(5, 1);
				else if (movei == 6)	horse[i] = make_pair(5, 2);
				else if (movei == 7)	horse[i] = make_pair(5, 3);
				else {
					horse[i] = make_pair(4, movei);
				}
				for (int k = 0; k < 4; k++) {
					if (horse[k] == horse[i] && k != i) {
						flag = true;
						break;
					}
				}
				if (flag == true) {
					horse[i] = temp;
				}
				else {
					v.push_back({ 4,movei });
					int temp_value = MoveDice(count + 1, answer + way4[movei]);
					if (temp_value > value)	value = temp_value;
					horse[i] = temp;
					v.pop_back();
				}
			}
		}
		else if (horse[i].first == 5) {
			bool flag = false;

			if (movei > 3) {
				v.push_back({ 0,0 });
				horse[i] = make_pair(0, 0);
				int temp_value = MoveDice(count + 1, answer);
				if (temp_value > value)	value = temp_value;
				horse[i] = temp;
				v.pop_back();
			}
			else {
				horse[i] = make_pair(5, movei);
				for (int k = 0; k < 4; k++) {
					if (horse[k] == horse[i] && k != i) {
						flag = true;
						break;
					}
				}
				if (flag == true) {
					horse[i] = temp;
				}
				else {
					v.push_back({ 5,movei });
					int temp_value = MoveDice(count + 1, answer + way5[movei]);
					if (temp_value > value)	value = temp_value;
					horse[i] = temp;
					v.pop_back();
				}
			}
		}
	}
	return value;
}

int main(void) {
	for (int i = 0; i < 10; i++)	cin >> dice[i];

	for (int i = 0; i < 4; i++)	horse[i] = make_pair(1, 0);

	cout << MoveDice(0, 0) << endl;
}

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

상어 중학교  (0) 2021.06.28
새로운 게임 2  (0) 2021.04.20
모노미노도미노2 C++  (0) 2021.04.18
컨테이너 벨트  (0) 2021.04.18
청소년 상어 C++  (0) 2021.04.17