프로그래머스
방문 길이 C++
by paysmile
2019. 10. 23.
#include <string>
#include <cstring>
using namespace std;
const int MAX = 12;
int visited[MAX][MAX][MAX][MAX];
struct Move {
int x, y;
};
Move mv[4] = { {0,1}, {0,-1}, {1,0}, {-1,0} };//U, D, R, L
int solution(string dirs)
{
int answer = 0;
memset(visited, -1, sizeof(visited));
int currenti = 5, currentj = 5;
for (int i = 0; i < dirs.size(); i++) {
int movei, movej;
if (dirs[i] == 'U') {
movei = currenti + mv[0].x;
movej = currentj + mv[0].y;
}
else if (dirs[i] == 'D') {
movei = currenti + mv[1].x;
movej = currentj + mv[1].y;
}
else if (dirs[i] == 'R') {
movei = currenti + mv[2].x;
movej = currentj + mv[2].y;
}
else if (dirs[i] == 'L') {
movei = currenti + mv[3].x;
movej = currentj + mv[3].y;
}
if (movei >= 0 && movei <= 10 && movej >= 0 && movej <= 10) {
if (visited[currenti][currentj][movei][movej] == -1 || visited[movei][movej][currenti][currentj] == -1) {
answer++;
visited[currenti][currentj][movei][movej] = 1;
visited[movei][movej][currenti][currentj] = 1;
}
currenti = movei;
currentj = movej;
}
}
return answer;
}