www.acmicpc.net/problem/14503

 

14503번: 로봇 청소기

로봇 청소기가 주어졌을 때, 청소하는 영역의 개수를 구하는 프로그램을 작성하시오. 로봇 청소기가 있는 장소는 N×M 크기의 직사각형으로 나타낼 수 있으며, 1×1크기의 정사각형 칸으로 나누어

www.acmicpc.net

1. 풀이방법.

- 문제의 조건대로 차례로 구현을 해주면 되며,

- 문제에서 첫행,첫열,끝행,끝열은 모두 벽이다 라는 조건도 맞추어 주었으므로 경계의 범위도 신경쓰지 않아도 되서

- 매우 편한 문제였습니다.

- 저 같은 경우 3,4 번의 종료조건을 먼저 체크해주고 그것의 결과에따라 변동을 주는 쪽으로 코드를 짰습니다.

 

 

 

2. 주의사항

- 딱히 없습니다. (저는 청소할 구역은 0, 벽은 1, 청소한 구역은 2 로 설정하였습니다.)

 

 

 

3.나의코드

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

int dx[4] = { -1,0,1,0 }; //북동남서
int dy[4] = { 0,1,0,-1 }; //0,1,2,3
int N, M;
int rx, ry, rd;
int robomap[51][51]; //빈칸은 0, 벽은 1
int cleancount;

void inputs() {
	cin >> N >> M;
	cin >> rx >> ry >> rd;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			cin >> robomap[i][j];
		}
	}
}

void cleanroom() {
	while (1) {
		if (robomap[rx][ry] == 0) {
			robomap[rx][ry] = 2; //청소
			cleancount++;
		}
		bool lastcheck = false;
		for (int i = 0; i < 4; i++) {//종료조건 검사
			if (robomap[rx + dx[i]][ry + dy[i]] == 0) { lastcheck = true; break; }
		}
		if (lastcheck == false) {
			if (robomap[rx + dx[(rd + 2) % 4]][ry + dy[(rd + 2) % 4]] == 1) { 
				cout << cleancount << "\n"; break;
			}
			else {
				rx += dx[(rd + 2) % 4]; ry += dy[(rd + 2) % 4];
			}
		}
		else if (lastcheck == true) {
			while (1) {
				if (robomap[rx + dx[(rd + 3) % 4]][ry + dy[(rd + 3) % 4]] == 0) {
					rd = (rd + 3) % 4;
					rx += dx[rd]; ry += dy[rd];
					break;
				}
				else {
					rd = (rd + 3) % 4;
				}
			}
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	inputs();
	cleanroom();

	return 0;
}

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

백준 17144 [C++]  (0) 2020.12.08
백준 14499 [C++]  (0) 2020.12.08
백준 14891 [C++]  (0) 2020.12.06
백준 2840 [C++]  (0) 2020.12.06
백준 2033 C++  (0) 2020.11.25

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