Posts 3190 Samsung sw test
Post
Cancel

3190 Samsung sw test

3190 Samsung sw test

알고리즘

1
2
3
4
5
1. 그냥 대가리 옮기고 꼬리 잡히는지, 칸 밖인지 확인하고, 또 사과있는지 확인하고, 또 시간이 바꿀때가 됐는지 확인
2. 뱀 몸은 vector로 저장 / 사과 위치는 2차원 bool 배열로 저장하여 메모리랑 참조시간 아낌. /
  시간은 Direction 구조체에 저장하고, directionIndex를 선언하여 directionIndex에 있는 시간에 맞으면 방향 바꾸고 index++.

3. 구현문제이므로 내가 생각하는 게임이랑 실제 문제 조건은 다를 수 있다. 따라서 예시를 통해 확인하라.

코드

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <vector>
#include <cstring>
#define MAX_APPLE 100
#define MAX_DIRECTION 100

using namespace std;

typedef struct Location {
	int row;
	int col;
} Location;
typedef struct Direction {
	int second;
	char direction;
} Direction;

int width;
int numOfApple;
bool appleMap[MAX_APPLE][MAX_APPLE];
int numOfChange;
Direction direction[MAX_DIRECTION];

void getProperties();
int getSecond();

int main() {
	getProperties();
	cout << getSecond();
}

void getProperties() {
	cin >> width;
	cin >> numOfApple;
	memset(appleMap, 0, sizeof(bool) * MAX_APPLE * MAX_APPLE);
	for (int i = 0; i < numOfApple; i++) {
		int row, col;
		cin >> row >> col;
		appleMap[row - 1][col - 1] = true;
	}

	cin >> numOfChange;
	for (int i = 0; i < numOfChange; i++) {
		cin >> direction[i].second >> direction[i].direction;
	}
}

int getSecond() {
	int second = 0;
	vector<Location> snake;
	snake.push_back({ 0, 0 });
	int currentDirection = 2;
	int directionIndex = 0;
	bool isEnd = false;

	//현재 헤드 제일 뒤에, 꼬리는 제일 앞에
	while (!isEnd) {
		second++;
		Location s = snake.back();
		if (currentDirection == 1)
			snake.push_back({ s.row - 1, s.col });
		else if (currentDirection == 2)
			snake.push_back({ s.row, s.col + 1 });
		else if (currentDirection == 3)
			snake.push_back({ s.row + 1, s.col });
		else if (currentDirection == 4)
			snake.push_back({ s.row, s.col - 1 });

		Location t = snake.back();

		if (t.row < 0 || t.row >= width || t.col < 0 || t.col >= width)
			isEnd = true;
		for (int i = 0; i < snake.size(); i++) {
			if (t.row == snake[i].row && t.col == snake[i].col && i != snake.size() - 1)
				isEnd = true;
		}

		if (appleMap[t.row][t.col])
			appleMap[t.row][t.col] = false;
		else
			snake.erase(snake.begin());

		if (direction[directionIndex].second == second) {
			if (currentDirection == 1)
				currentDirection = direction[directionIndex].direction == 'D' ? 2 : 4;
			else if (currentDirection == 2)
				currentDirection = direction[directionIndex].direction == 'D' ? 3 : 1;
			else if (currentDirection == 3)
				currentDirection = direction[directionIndex].direction == 'D' ? 4 : 2;
			else
				currentDirection = direction[directionIndex].direction == 'D' ? 1 : 3;
			directionIndex++;
		}
	}

	return second;
}
This post is licensed under CC BY 4.0 by the author.

2933 미네랄

3197 백조의 호수

Comments powered by Disqus.