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 |
Tags
- classloader
- 운영체제
- 상속관계
- la-piscine
- RDD
- SRP
- java
- 라피신
- generics
- Compiler
- 이노베이션아카데미
- pg_hba.conf
- HeapArea
- LSP
- 도커네트워크
- 참조변수
- 자바컴파일러
- JVM
- 42seoul
- StackArea
- javac
- 제네릭스
- 바이트코드
- 42서울
- JIT
- 포함관계
- jdk
- abstract
- MethodArea
- Operating System
Archives
- Today
- Total
while(1) 작심삼일();
[백준 3190번] 뱀 본문
http://www.acmicpc.net/problem/3190
3190번: 뱀
'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임
www.acmicpc.net
풀이)
뱀의 몸에 닿으면 게임을 끝나는 조건을 설정할 때, 처음에 queue로 진행하려했으나, stl에서 자체적으로 제공하는 iterator가 없어서 linkedlist를 직접 구현하는 방식으로 진행했다. 구현문제였기때문에 문제에서 요구한 절차에 따라
방향에 따른 이동 -> 게임종료 조건 확인 -> 사과 먹은 유무에 따른 몸 길이 재 설정 -> 현 시간이 방향을 바꾸어야 하는 시점인지 확인으로 진행했고, 사과 위치, 뱀의 몸이 남아있는 위치, 시간에 따른 방향 설정정보들을 어떤식으로 저장하고 사용할지에 대해 명확하게 떠오르지 않아서 시간이 더 걸렸다.
#include <iostream>
#include <queue>
using namespace std;
int apple[101][2];
int N, K, L, now_x = 1, now_y = 1, time_, move_dir = 1, result = 0;
char dir;
char secondForDir[10001];
typedef struct node{
int x, y;
struct node* link;
} node;
struct node* head = NULL;
void add_list(int x, int y)
{
struct node* add = (struct node*)malloc(sizeof(node));
add->x = x;
add->y = y;
add->link = head;
head = add;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> N >> K;
for (int i = 1; i <= K; i++)
cin >> apple[i][0] >> apple[i][1];
cin >> L;
while (L--)
{
cin >> time_ >> dir;
secondForDir[time_] = dir;
}
head = (struct node*)malloc(sizeof(node));
head->x = 1;
head->y = 1;
head->link = NULL;
while (1)
{
result++;
bool apple_ = false, end = false;
if (move_dir == 1)
now_y += 1;
else if (move_dir == 2)
now_x += 1;
else if (move_dir == 3)
now_y -= 1;
else if (move_dir == 4)
now_x -= 1;
struct node* p = head;
while (p != NULL) {
if (now_x == p->x && now_y == p->y)
end = true;
p = p->link;
}
if (now_x < 1 || now_x > N || now_y < 1 || now_y > N || end)
break ;
add_list(now_x, now_y);
for (int t = 1; t <= K; t++){
if (apple[t][0] == now_x && apple[t][1] == now_y){
apple[t][0] = apple[t][1] = 0;
apple_ = true;
break ;
}
}
if (!apple_)
{
struct node* tp = head;
while (tp->link->link != NULL)
tp = tp->link;
free(tp->link);
tp->link = NULL;
}
if (secondForDir[result] == 'D'){
if (move_dir == 4)
move_dir = 1;
else
move_dir += 1;
}
else if (secondForDir[result] == 'L'){
if(move_dir == 1)
move_dir = 4;
else
move_dir -= 1;
}
}
cout << result;
return (0);
}
제 코드는 절대 완벽하지 않습니다
더 좋은 풀이방법 혹은 코드에 관한 질문이 있으시면 언제든 댓글주세요!
'CS > baekjoon' 카테고리의 다른 글
[백준 9663번] N-Queen (1) | 2021.02.22 |
---|---|
[백준 9251번] LCS (0) | 2021.02.18 |
[백준 2170번] 선 긋기 (0) | 2021.02.17 |
[백준 1916번] 최소비용 구하기 (0) | 2021.02.17 |
[백준 1759번] 암호 만들기 (0) | 2021.02.16 |
Comments