Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Unreal Engine 5
- 브루트포스
- DFS
- 수학
- 구현
- 백준
- BFS
- 다이나믹 프로그래밍
- 자료구조
- 유니티
- 투 포인터
- 정렬
- 스택
- 재귀
- c++
- 그리디 알고리즘
- ue5
- 그래프
- 유니온 파인드
- 누적 합
- XR Interaction Toolkit
- Team Fortress 2
- 알고리즘
- VR
- 다익스트라
- 우선순위 큐
- 백트래킹
- 시뮬레이션
- 트리
- 문자열
Archives
- Today
- Total
1일1알
백준 1491번 나선 C++ 본문
남서쪽이 (0, 0)이고 남동쪽이 (N-1, 0)이라고 했으니 ↓→↑← 순서로 돌아야 한다.
쭉 직진하다가 벽을 만나면 저 순서대로 도는 것을 반복하다가 만약 사방이 막혀있거나 이미 방문한 곳이라면 그곳이 나선이 끝나는 곳이다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using ll = long long;
int dRow[4] = { 1,0,-1,0 };
int dCol[4] = { 0,1,0,-1 };
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
vector<vector<bool>> visited(n, vector<bool>(m, false));
int row = 0;
int col = 0;
int dir = 0;
visited[row][col] = true;
int cnt = 0;
while (true) {
if (cnt >= 4) break;
int nextRow = row + dRow[dir];
int nextCol = col + dCol[dir];
if (nextRow < 0 || nextRow >= n || nextCol < 0 || nextCol >= m || visited[nextRow][nextCol]) {
cnt++;
dir = (dir + 1) % 4;
continue;
}
cnt = 0;
row = nextRow;
col = nextCol;
visited[row][col] = true;
}
cout << row << " " << col;
};
'알고리즘' 카테고리의 다른 글
백준 3987번 보이저 1호 C++ (0) | 2022.02.01 |
---|---|
백준 10431번 줄세우기 C++ (0) | 2022.01.31 |
백준 2777번 숫자 놀이 C++ (0) | 2022.01.29 |
백준 8980번 택배 C++ (0) | 2022.01.28 |
백준 1083번 소트 C++ (0) | 2022.01.27 |