킹솔이
[백준/C++] 14503 로봇 청소기 본문
#include<iostream>
using namespace std;
int map[50][50] = { 1, };
int main() {
int n, m;
cin >> n >> m;
int r, c, d;
cin >> r >> c >> d;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> map[i][j];
}
}
int dx[4] = { 0,1,0,-1 };
int dy[4] = { -1,0,1,0 };
int count = 0;
while (true) {
map[r][c] = 2;
int temp = (d + 3) % 4;
if (count < 4) {
if (map[r + dy[temp]][c + dx[temp]] == 0) {
d = temp;
r = r + dy[temp];
c = c + dx[temp];
count = 0;
continue;
}
else if (map[r + dy[temp]][c + dx[temp]] == 1 || map[r + dy[temp]][c + dx[temp]] == 2) {
d = temp;
count++;
continue;
}
}
else {
if (map[r + dy[(d + 2) % 4]][c + dx[(d + 2) % 4]] != 1) {
r = r + dy[(d + 2) % 4];
c = c + dx[(d + 2) % 4];
count = 0;
continue;
}
else break;
}
}
int answer = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (map[i][j] == 2) answer++;
}
}
cout << answer;
return 0;
}
'Algorithm' 카테고리의 다른 글
[프로그래머스/C++] 서머코딩/윈터코딩(~2018) 쿠키 구입 (0) | 2020.03.09 |
---|---|
[프로그래머스/C++] 서머코딩/윈터코딩(~2018) 방문길이 (0) | 2020.02.19 |
[백준/C++] 17487 타자 연습 (0) | 2020.01.28 |
[백준/C++] 1268 임시반장 정하기 (0) | 2020.01.22 |
[프로그래머스/C++] 2020 KAKAO BLIND RECRUITMENT 문자열 압축 (0) | 2020.01.22 |