알고리즘/프로그래머스
Programers 공원 산책/ C++
내이름은 킹햄찌
2023. 3. 29. 00:52
https://school.programmers.co.kr/learn/courses/30/lessons/172928
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
아이디어
일반 구현문제임, 이동 경로를 한번에 이동하지 않고 한칸씩 이동하며 X가 있는지 확인을 잘하는 것이 뽀인트
#include <string>
#include <vector>
using namespace std;
class Point {
public:
Point() {};
void move(int _y, int _x) {
y = _y;
x = _x;
}
int getY() {
return y;
}
int getX() {
return x;
}
private:
int y = 0;
int x = 0;
};
int dy[4] = { -1,1,0,0 };
int dx[4] = { 0,0,-1,1 };
int dic2Idx(char c) {
if (c == 'N') return 0;
if (c == 'S') return 1;
if (c == 'W') return 2;
if (c == 'E') return 3;
}
vector<int> solution(vector<string> park, vector<string> routes) {
vector<int> answer;
Point cur;
for (int i = 0; i < park.size(); i++) {
for (int j = 0; j < park[i].size(); j++) {
if (park[i][j] == 'S') { cur.move(i, j); break; }
}
}
for (auto route : routes) {
bool impassable;
int a = route[2] - '0';
int y = cur.getY();
int x = cur.getX();
for (int i = 0; i < (route[2] - '0'); i++) {
impassable = true;
y += dy[dic2Idx(route[0])];
x += dx[dic2Idx(route[0])];
if (y >= park.size() || y < 0 || x >= park[y].size() || x < 0) break;
if (park[y][x] == 'X') break;
impassable = false;
}
if (impassable) continue;
cur.move(y, x);
}
answer.push_back(cur.getY());
answer.push_back(cur.getX());
return answer;
}