Posts 14503 Samsung sw test
Post
Cancel

14503 Samsung sw test

14503 Samsung sw test

코드

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
#include <iostream>
#define MAX_WIDTH 50

using namespace std;

int direction;
int location[2];
int map[MAX_WIDTH][MAX_WIDTH];
int height, width;

int robot();
bool changeDirection();

int main() {
	cin >> height >> width;
	cin >> location[0] >> location[1] >> direction;
	for (int i = 0; i < height; i++)
		for (int j = 0; j < width; j++)
			cin >> map[i][j];

	cout << robot();
}

int robot() {
	bool doesStop = false;

	while (!doesStop) {
		map[location[0]][location[1]] = 2;

		while (!changeDirection()) {
			if (direction == 0)
				location[0] += 1;
			else if (direction == 1)
				location[1] -= 1;
			else if (direction == 2)
				location[0] -= 1;
			else if (direction == 3)
				location[1] += 1;

			if (map[location[0]][location[1]] == 1) {
				doesStop = true;
				break;
			}
		}
	}

	int count = 0;
	for (int i = 0; i < height; i++)
		for (int j = 0; j < width; j++)
			if (map[i][j] == 2)
				count++;
	return count;
}

bool changeDirection() {
	for (int i = 0; i < 4; i++) {
		if (direction == 0) {
			direction = 3;
			if (map[location[0]][location[1] - 1] == 0) {
				location[1] -= 1;
				return true;
			}
		}
		else if (direction == 1) {
			direction = 0;
			if (map[location[0] - 1][location[1]] == 0) {
				location[0] -= 1;
				return true;
			}
		}
		else if (direction == 2) {
			direction = 1;
			if (map[location[0]][location[1] + 1] == 0) {
				location[1] += 1;
				return true;
			}
		}
		else if (direction == 3) {
			direction = 2;
			if (map[location[0] + 1][location[1]] == 0) {
				location[0] += 1;
				return true;
			}
		}
	}

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

14502 Samsung sw test

14863 서울에서 경산까지

Comments powered by Disqus.