17825번 주사위 윷놀이

동도리동·2021년 10월 18일
0

코딩테스트

목록 보기
64/76

테스트 케이스 하나가 자꾸 안맞은 코드... 주의

#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>
#include <tuple>
using namespace std;
struct Loc {
	int pan, num, sum;
};
int map[4][30];
int res[11];
int ans = 0;
vector<int> v;
bool check(int Circle, int Pos, int ch[][30]) {
	if (Circle == 0) {
		if (Pos == 20) {
			if (ch[1][12] == 1 || ch[2][16] == 1 || ch[3][22] == 1) return false;
			else if (ch[0][Pos] == 1) return false;
		}
	}
	else if (Circle == 1) {
		if (ch[2][Pos + 4] == 1 || ch[3][Pos + 10] == 1) return false;
		if (Pos == 12) if (ch[0][20] == 1) return false;
	}
	else if (Circle == 2) {
		if (ch[1][Pos - 4] == 1 || ch[3][Pos + 6] == 1) return false;
		if (Pos == 16) if (ch[0][20] == 1) return false;
	}
	else if (Circle == 3) {
		if (ch[1][Pos - 10] == 1 || ch[2][Pos - 6] == 1) return false;
		if (Pos == 22) if (ch[0][20] == 1) return false;
	}
	return true;
}
void go(int L) {
	if (L == 10) {
		vector<Loc> horse(5);
		vector<bool> ok(5);
		int ch[5][30];
		for (int i = 0; i < 5; i++) {
			ok[i] = true;
			horse[i].num = 0; horse[i].pan = 0; horse[i].sum = 0;
			for (int j = 0; j < 30; j++) {
				ch[i][j] = 0;
			}
		}
		for (int i = 0; i < 10; i++) {
			//cout << res[i] << " "; res에는 언제 몇번째 말을 움직여야 하는지 저장
			int t = res[i];
			int xx = horse[t].num + v[i];
			ch[horse[t].pan][horse[t].num] = 0;
			if (xx > 25) return;
			if (xx == 5 && ok[t]) {
				horse[t].pan = 1;
				ok[t] = false;
			}
			else if (xx == 10&&ok[t]) {
				horse[t].pan = 2;
				ok[t] = false;
			}
			else if (xx == 15 && ok[t]) {
				horse[t].pan = 3;
				ok[t] = false;
			}
			if (check(horse[t].pan,horse[t].num,ch) == false) return;
			ch[horse[t].pan][xx] = 1;
			if(xx>20&& horse[t].pan==0) ch[horse[t].pan][xx] = 0;
			else if(xx>12 && horse[t].pan==1) ch[horse[t].pan][xx] = 0;
			else if(xx>16&& horse[t].pan==2) ch[horse[t].pan][xx] = 0;
			else if(xx > 22&& horse[t].pan==3) ch[horse[t].pan][xx] = 0;
			horse[t].num = xx;
			horse[t].sum += map[horse[t].pan][xx];
		}
		int sum = 0;
		for (int i = 0; i < 5; i++) {
			sum += horse[i].sum;
		}
		if (ans < sum) ans = sum;
	}
	else {
		for (int i = 0; i < 4; i++) {
			res[L] = i;
			go(L + 1);
		}
	}
}
int main() {
	freopen("in1.txt", "r", stdin);
	int x;
	for (int i = 0; i < 10; i++) {
		cin >> x;
		v.push_back(x);
	}
	for (int i = 1; i <= 20; i++) {
		map[0][i] = i * 2;
	}
	map[1][5] = 10; map[2][10] = 20; map[3][15] = 30;
	map[1][6] = 13; map[1][7] = 16; map[1][8] = 19;
	map[2][11] = 22; map[2][12] = 24;
	map[3][16] = 28; map[3][17] = 27; map[3][18] = 26;
	map[1][9] = map[2][13] = map[3][19] = 25;
	map[1][10] = map[2][14] = map[3][20] = 30;
	map[1][11] = map[2][15] = map[3][21] = 35;
	map[1][12] = map[2][16] = map[3][22] = 40;
	go(0);
	cout << ans << '\n';
	return 0;
}

다시 풀어보자...

참고자료 https://yabmoons.tistory.com/303

profile
긍정코딩세상

0개의 댓글

관련 채용 정보