14891번 톱니바퀴

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

코딩테스트

목록 보기
57/76

약간 노가다로 풀었는데.. 재귀로 충분히 풀 수 있을것 같다.
재귀로도 도전해보자!

#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
vector<vector<int> > top(4, vector<int>(8));
void ro(int x, int y) {
	if (y == 1) rotate(top[x].rbegin(), top[x].rbegin() + 1, top[x].rend());
	else rotate(top[x].begin(), top[x].begin() + 1, top[x].end());
}

void go(int x,int y) {
	if (x==0) {
		if (top[0][2] != top[1][6]) {
			if (top[1][2] != top[2][6]) {
				if (top[2][2] != top[3][6]) {
					ro(3, -y);
				}
				ro(2, y);
			}
			ro(1, -y);
		}
		ro(0, y);
	}
	else if (x == 1) {
		if (top[1][6] != top[0][2]) {
			ro(0, -y);
		}
		if (top[1][2] != top[2][6]) {
			if (top[2][2] != top[3][6]) {
				ro(3, y);
			}
			ro(2, -y);
		}
		ro(1, y);
	}
	else if (x == 2) {
		if (top[2][2] != top[3][6]) ro(3, y * (-1));
		if (top[2][6] != top[1][2]) {
			if (top[1][6] != top[0][2]) {
				ro(0, y);
			}
			ro(1, y * (-1));
		}
		ro(2, y);
	}
	else {
		if (top[3][6] != top[2][2]) {
			if (top[2][6] != top[1][2]) {
				if (top[1][6] != top[0][2]) {
					ro(0, y * (-1));
				}
				ro(1, y);
			}
			ro(2, y * (-1));
		}
		ro(3, y);
	}
}

int main() {
	//freopen("in1.txt", "rt", stdin);
	for (int i = 0; i < 4; i++) {
		char s[9];
		cin >> s;
		for (int j = 0; j < 8; j++) {
			top[i][j] = s[j]-48;
		}
	}
	int k=0;
	cin >> k;
	while (k--) {
		int x, y;
		cin >> x >> y;
		x--;
		go(x,y);
	} 
	int ans = 0;
	for (int i = 0; i < 4; i++) {
		if (top[i][0] == 1) ans += pow(2, i);
	}
	cout << ans << '\n';
	return 0;
}
profile
긍정코딩세상

0개의 댓글

관련 채용 정보