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
- 시간 복잡도
- 백준 1406
- c#
- 백준 5397
- 구현
- 해시구현
- 1764
- 5397
- 원판 돌리기
- 백준
- C/C++ 구현
- heap
- AVL 시간 복잡도
- 버킷 정렬
- Stack 이란
- 해시 구현
- 별 찍기 10
- 백준 1158
- qorwns
- 백준 2447
- dfs
- 조세퍼스 순열
- 백준 17779
- 백준 17471
- 자료구조
- ㅣ풀이
- 게리멘더링2
- 풀이
- 백준 17822
- 스택의 특징
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 3190] 뱀 본문
https://www.acmicpc.net/problem/3190
3190번: 뱀
문제 'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임은 NxN 정사각 보드위에서 진행되고, 몇몇 칸에는 사과가 놓여져 있다. 보드의 상하좌우 끝에 벽이 있다. 게임이 시작할때 뱀은 맨위 맨좌측에 위치하고 뱀의 길이는 1 이다. 뱀은 처음에 오른쪽을 향한다. 뱀은 매 초마다 이동을 하는데 다음과 같은 규칙을 따
www.acmicpc.net
요구사항
1. 뱀 이동시키며 주어진 조건에 따라 처리하기
풀이
1. 뱀 이동, 시간 증가
2. 이동된 칸에 따라 처리(0, 1, 2, 맵 바깥)
3. 방향 바뀌는 시간에 도달하였을 경우, 방향 변경
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
struct pos{
int x;
int y;
};
struct pos1 {
int x;
char y;
};
int map[101][101];
pos1 dir[101];
int dr[4] = { -1,0,1,0 };
int dc[4] = { 0,-1,0,1 };
int n, l, k;
deque<pos> dq;
int ans = 0;
int maxv = 0;
int getdir(int d, char ch) {
if (d == 0) {
if (ch == 'L') {
return 1;
}
else {
return 3;
}
}
else if (d == 1) {
if (ch == 'L') {
return 2;
}
else {
return 0;
}
}
else if (d == 2) {
if (ch == 'L') {
return 3;
}
else {
return 1;
}
}
else if (d == 3) {
if (ch == 'L') {
return 0;
}
else {
return 2;
}
}
}
void solve() {
map[0][0] = 1;
dq.push_back({ 0,0 });
int d = 3;
int cnt = 0;
int time = 0;
while (true) {
int r = dq.front().x;
int c = dq.front().y;
//cout << "R" << r << " C " << c << endl;
int nr = r + dr[d];
int nc = c + dc[d];
if (nr < 0 || nc < 0 || nr >= n || nc >= n) {
ans = cnt+1;
break;
}
if (map[nr][nc] == 1) {
ans = cnt+1;
break;
}
else if (map[nr][nc] == 0) {
int br = dq.back().x;
int bc = dq.back().y;
map[br][bc] = 0;
map[nr][nc] = 1;
dq.pop_back();
dq.push_front({ nr,nc });
}
else if (map[nr][nc] == 2) {
dq.push_front({ nr,nc });
map[nr][nc] = 1;
}
cnt++;
if (cnt <= maxv) {
if (cnt == dir[time].x) {
d = getdir(d,dir[time].y);
time++;
}
}
}
}
int main() {
cin >> n >> k;
for (int i = 0; i < k; i++) {
int x, y;
cin >> x >> y;
map[x - 1][y - 1] = 2;
}
cin >> l;
for (int i = 0; i < l; i++) {
char ch;
int x;
cin >> x >> ch;
dir[i].x = x;
dir[i].y = ch;
maxv = max(x, maxv);
}
solve();
cout << ans << endl;
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 17779] 게리멘더링 2 (0) | 2020.02.01 |
---|---|
[백준 13458] 시험감독 (0) | 2020.01.31 |
[백준 17822] 원판 돌리기 (0) | 2020.01.30 |
[백준 13460] 구슬 탈출2 (0) | 2020.01.29 |
[백준 3020] 개똥벌레 (0) | 2020.01.14 |
Comments