728x90
https://www.acmicpc.net/problem/13901

이 문제는 격자가 주어지고 로봇의 현재 위치, 장애물들의 위치가 좌표로 주어지고 방향 순서가 주어졌을때 조건에 따라서
로봇이 더이상 갈 수 없는 위치를 좌표로 출력하는 문제이다.

문제를 보고 하라는대로 하면 바로 정답을 맞출 수 있겠다 싶었다.
그래서 하나의 방문배열을 만들고 해당하는 방향에 따라서 끝까지 가서 더이상 갈 수 없는지 확인하였다.
move라는 변수를 두었으며 움직였을 경우 true 만약 움직이지 못했을 경우 false기 때문에 false일 경우 탈출하여 현재 위치값을 출력하였다.
정답 코드
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define endl "\n"
#define MAX 1e9
struct coordinate {
int x;
int y;
};
//int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
//int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
int dx[] = {0, -1, 1, 0, 0};
int dy[] = {0, 0, 0, -1, 1};
int R, C, k, startx, starty;
int graph[1001][1001]; // 0 : 빈칸, -1 : 장애물
vector<int> dir;
bool check(int x, int y) {
if(x >= 0 && x < R && y >= 0 && y < C) return true;
return false;
}
void solve() {
graph[startx][starty] = 1;
while(true) {
bool move = false; // 방향 사이클을 돌았을 때 움직였는지 체크하기위함
for (auto &it: dir) {
int nx = startx + dx[it];
int ny = starty + dy[it];
while (true) {
if (nx >= 0 && nx < R && ny >= 0 && ny < C) {
if (graph[nx][ny] == 0) {
graph[nx][ny] = 1;
nx += dx[it];
ny += dy[it];
move = true;
} else {
startx = nx - dx[it];
starty = ny - dy[it];
break;
}
} else {
startx = nx - dx[it];
starty = ny - dy[it];
break;
}
}
}
if(!move) {
break;
}
}
cout << startx << " " << starty;
}
void input() {
cin >> R >> C >> k;
for(int i = 0; i < k; i++) {
int r, c;
cin >> r >> c;
graph[r][c] = -1;
}
cin >> startx >> starty;
for(int i = 0; i < 4; i++) {
int a;
cin >> a;
dir.push_back(a);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
input();
solve();
}

728x90
'Algorithm' 카테고리의 다른 글
| [백준] 15900번 나무탈출 (C++) (0) | 2025.08.11 |
|---|---|
| [백준] 1726번 로봇 (C++) (0) | 2025.08.10 |
| [백준] 17124번 두 개의 배열 (C++) (0) | 2025.08.08 |
| [백준] 17141번 연구소 2 (C++) (0) | 2025.08.07 |
| [백준] 2473번 세 용액 (C++) (0) | 2025.08.06 |