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 | 29 | 30 | 31 |
Tags
- SQL
- 코딩테스트
- spring boot
- 구현
- SWEA
- django
- programmers
- hash table
- BFS
- 알고리듬
- 알고리즘
- CSV
- Priority Queue
- 코테
- boj
- Back tracking
- Trie
- Python
- Bruth Force
- 시뮬레이션
- 모의SW역량테스트
- Vue.js
- Algorithm
- gpdb
- JavaScript
- Linked list
- Data Structure
- GitHub
- DFS
- aws
Archives
- Today
- Total
hotamul의 개발 이야기
[Algorithm][C++] BOJ 1600 말이 되고픈 원숭이 본문
myt-algorithm-practice/Samsung SW Certi Adv
[Algorithm][C++] BOJ 1600 말이 되고픈 원숭이
hotamul 2021. 9. 6. 16:02url: https://www.acmicpc.net/problem/1600.
1600번: 말이 되고픈 원숭이
첫째 줄에 정수 K가 주어진다. 둘째 줄에 격자판의 가로길이 W, 세로길이 H가 주어진다. 그 다음 H줄에 걸쳐 W개의 숫자가 주어지는데, 0은 아무것도 없는 평지, 1은 장애물을 뜻한다. 장애물이 있
www.acmicpc.net
풀이 핵심
1. BFS 활용
2. 벽 부수기 문제와 비슷한 풀이
3. 방문 여부는 check[r 좌표][c 좌표][말 점프 횟수]
4. 말 점프 횟수가 K 이면서 dir (방향 index) 가 4 이상이면 break
5. (R - 1, C - 1) 좌표에 도달하면 거리 return, 모든 탐색을 완료해도 도착지에 다다르지 못할 경우 -1 return
코드
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <queue>
using namespace std;
int K, R, C;
int map[200][200];
bool check[200][200][30 + 1];
struct monkey {
int r, c, horse, dist;
};
queue<monkey> q;
int bfs() {
monkey start = { 0,0,0,0 };
q.push(start);
check[start.r][start.c][start.horse] = true;
while (!q.empty()) {
monkey cur = q.front();
q.pop();
if (cur.r == R - 1 && cur.c == C - 1) return cur.dist;
const int dr[] = { 1,-1,0,0,2,2,-2,-2,1,1,-1,-1 };
const int dc[] = { 0,0,1,-1,1,-1,1,-1,2,-2,2,-2 };
for (int dir = 0; dir < 12; dir++) {
int nhorse = cur.horse;
if (cur.horse >= K) {
if (dir >= 4) break;
}
else if (dir >= 4) nhorse += 1;
int nr = cur.r + dr[dir];
int nc = cur.c + dc[dir];
if (nr < 0 || nr >= R || nc < 0 || nc >= C) continue;
if (map[nr][nc] || check[nr][nc][nhorse]) continue;
q.push({ nr,nc,nhorse, cur.dist + 1 });
check[nr][nc][nhorse] = true;
}
}
return -1;
}
int main() {
scanf("%d %d %d", &K, &C, &R);
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
scanf("%d", &map[i][j]);
}
}
printf("%d", bfs());
return 0;
}
'myt-algorithm-practice > Samsung SW Certi Adv' 카테고리의 다른 글
[Algorithm][C++] BOJ 11048 이동하기 (0) | 2021.09.08 |
---|---|
[Algorithm][C++] BOJ 16235 나무 제테크 (0) | 2021.09.08 |
[Algorithm][C++] BOJ 4991 로봇 청소기 (0) | 2021.09.02 |
[Algorithm][C++] BOJ 9376 탈옥 (0) | 2021.09.02 |
[Algorithm][C++] BOJ 5014 스타트링크 (0) | 2021.09.01 |
Comments