Posts 14499 Samsung sw test
Post
Cancel

14499 Samsung sw test

14499 Samsung sw test

알고리즘

1
2
3
1. 각 명령별로 검사. 초기 주사위 상태 (top : 1, north : 2, east : 3, south : 5, western : 4, bottom : 6)
2. 명령대로 주사위를 굴리고 -> 그 위치가 맵 밖이면 다음 명령 실행
					맵 안이면 이동시키고, 주사위 상태 변경 -> 이후 이동한 칸에 숫자 적용하고 -> 상단 프린트 -> 다음 명령 실행

코드

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <cstring>
#define MAX_WIDTH 20
#define MAX_COMMAND 1000

using namespace std;

typedef struct Location {
	int row;
	int col;
} Location;
typedef struct Dice {
	int top;
	int north;
	int east;
	int south;
	int western;
	int bottom;
}Dice;

int width;
int height;
int map[MAX_WIDTH][MAX_WIDTH];
Location locationDice;
int numOfCommand;
int command[MAX_COMMAND];
int dice[7];


void getProperties();
Dice diceChange(Dice d, int direction);
bool diceMove(int command);
void game();

int main() {
	getProperties();
	game();
}

void getProperties() {
	cin >> height >> width >> locationDice.row >> locationDice.col >> numOfCommand;

	for (int i = 0; i < MAX_WIDTH; i++)
		memset(map, 0, sizeof(int) * MAX_WIDTH);
	memset(dice, 0, sizeof(int) * 7);

	for (int i = 0; i < height; i++) {
		for (int j = 0; j < width; j++)
			cin >> map[i][j];
	}

	for (int i = 0; i < numOfCommand; i++)
		cin >> command[i];
}

void game() {
	Dice d = { 1, 2, 3, 5, 4, 6 };

	for (int i = 0; i < numOfCommand; i++) {
		if (diceMove(command[i])) {
			d = diceChange(d, command[i]);
			if (map[locationDice.row][locationDice.col] == 0)
				map[locationDice.row][locationDice.col] = dice[d.bottom];
			else {
				dice[d.bottom] = map[locationDice.row][locationDice.col];
				map[locationDice.row][locationDice.col] = 0;
			}

			cout << dice[d.top] << endl;
		}
	}
}

bool diceMove(int command) {
	//temp를 먼저 옮기고, 범위 밖이면 false리턴, 아니면 수정 후 true리턴
	Location temp = { locationDice.row, locationDice.col };
	if (command == 1)
		temp.col++;
	else if (command == 2)
		temp.col--;
	else if (command == 3)
		temp.row--;
	else if (command == 4)
		temp.row++;

	if (temp.col < 0 || temp.col >= width || temp.row < 0 || temp.row >= height)
		return false;

	locationDice.col = temp.col;
	locationDice.row = temp.row;
	return true;
}

Dice diceChange(Dice d, int direction) {
	//1 : 동, 2: 서, 3: 북, 4 : 남
	Dice temp;
	if (direction == 1) {
		temp.top = d.western;
		temp.north = d.north;
		temp.east = d.top;
		temp.south = d.south;
		temp.western = d.bottom;
		temp.bottom = d.east;
	}
	else if (direction == 2) {
		temp.top = d.east;
		temp.north = d.north;
		temp.east = d.bottom;
		temp.south = d.south;
		temp.western = d.top;
		temp.bottom = d.western;
	}
	else if (direction == 3) {
		temp.top = d.south;
		temp.north = d.top;
		temp.east = d.east;
		temp.south = d.bottom;
		temp.western = d.western;
		temp.bottom = d.north;
	}
	else if (direction == 4) {
		temp.top = d.north;
		temp.north = d.bottom;
		temp.east = d.east;
		temp.south = d.top;
		temp.western = d.western;
		temp.bottom = d.south;
	}
	return temp;
}
This post is licensed under CC BY 4.0 by the author.

14003 LIS5

14500 Samsung sw test

Comments powered by Disqus.