www.acmicpc.net/problem/14891

 

14891번: 톱니바퀴

첫째 줄에 1번 톱니바퀴의 상태, 둘째 줄에 2번 톱니바퀴의 상태, 셋째 줄에 3번 톱니바퀴의 상태, 넷째 줄에 4번 톱니바퀴의 상태가 주어진다. 상태는 8개의 정수로 이루어져 있고, 12시방향부터

www.acmicpc.net

1. 풀이방법

- 구현문제입니다.. 그냥 문제에서 주는 조건에 맞춰 구현만하면 되는 듯 합니다.

 

- 톱니바퀴 개수도 정해져있고... 쉬운편인 것 같습니다.

 

- 저는 0,1,2,3 톱니바퀴에 대한 회전유무를 판단하는 벡터를 선언해서 돌아갈 지 말지를 체크해준 뒤

   돌렸습니다....!

 

 

 

2. 주의사항

- 처음에 연쇄적인 회전(?) 일까 라고 생각했는데 그건 아니고 4개가 한번에 빡 돌아가는 그런 것...

 

 

 

3. 나의코드

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

char topni[4][8];
int K;
int resultscore;

void inputs() {
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 8; j++) {
			cin >> topni[i][j];
		}
	}
}


void rotate(int index, int dir) { //0,1,2,3
	vector<pair<bool,int>> turn(4); //first= 회전할지 말지, second=방향

	for (int i = 0; i < 4; i++) { //초기화
		turn[i].first = false; turn[i].second = 0;
	}
	turn[index].first = true; turn[index].second = dir;
	//왼쪽확인
	char current=topni[index][6];
	int direction = dir;

	for (int i = index - 1; i >= 0; i--) {
		if (current != topni[i][2]) {
			turn[i].first = true; turn[i].second = (-1)*direction; //반대방향
			current = topni[i][6]; direction *= (-1);
		}
		else break; //이게 회전을 안한다면 그 옆은 볼필요 x
	}
	//오른쪽확인
	direction = dir;
	current = topni[index][2];
	for (int i = index + 1; i < 4; i++) {
		if(current!=topni[i][6]) {
			turn[i].first = true; turn[i].second = (-1)*direction;
			current = topni[i][2]; direction *= (-1);
		}
		else break; //이게 회전을 안한다면 그 옆은 볼필요 x
	}
	
	//회전
	for (int i = 0; i < 4; i++) {
		if (turn[i].first == true) { //회전한다면
			if (turn[i].second == 1) { //시계방향
		  		char tmp=topni[i][7];
				for (int j = 6; j >= 0; j--) topni[i][j + 1] = topni[i][j];
				topni[i][0] = tmp;
			}
			else { //반시계방향
				char tmp = topni[i][0];
				for (int j = 1; j <= 7; j++) topni[i][j - 1] = topni[i][j];
				topni[i][7] = tmp;
			}
		}
	}
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cin.tie(0);
	inputs();
	cin >> K;
	int index, dir;
	for (int i = 0; i < K; i++) {
		cin >> index >> dir;
		rotate(index-1, dir);
	}
	if (topni[0][0] == '1') resultscore += 1;

	if (topni[1][0] == '1') resultscore += 2;

	if (topni[2][0] == '1') resultscore += 4;

	if (topni[3][0] == '1') resultscore += 8;

	cout << resultscore << "\n";

	return 0;
}

'알고리즘 문제풀이 > 구현' 카테고리의 다른 글

백준 14499 [C++]  (0) 2020.12.08
백준 14503 [C++]  (0) 2020.12.08
백준 2840 [C++]  (0) 2020.12.06
백준 2033 C++  (0) 2020.11.25
백준 14890 [C++]  (0) 2020.10.21

+ Recent posts