Posts 20056 Samsung sw test
Post
Cancel

20056 Samsung sw test

20056 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <cstring>
#include <vector>
#define MAX_WIDTH 50

using namespace std;

typedef struct Fire {
	int mass;
	int direction;	//0이 위, 시계방향으로 0~7
	int speed;
} Fire;
typedef struct Cell {
	vector<Fire> fire;
} Cell;

int width, numOfCommand, numOfFire;
Cell map[MAX_WIDTH][MAX_WIDTH];
Cell tempMap[MAX_WIDTH][MAX_WIDTH];

int getRestFireBallMass();
void moveFireBall();
void splitFireBall();
int nextRow(int row, int speed, bool isUp) {
	int reminder = speed % width;
	if (isUp) {
		if (row < reminder)
			return width - (reminder - row);
		else
			return row - reminder;
	}
	else {
		if ((width - 1 - row) < reminder)
			return reminder - width + row;
		else
			return row + reminder;
	}
}
int nextCol(int col, int speed, bool isLeft) {
	int reminder = speed % width;
	if (isLeft) {
		if (col < reminder)
			return width - (reminder - col);
		else
			return col - reminder;
	}
	else {
		if ((width - 1 - col) < reminder)
			return reminder - width + col;
		else
			return col + reminder;
	}
}

int main() {
	cin >> width >> numOfFire >> numOfCommand;
	for (int i = 0; i < numOfFire; i++) {
		int row, col;
		cin >> row >> col;
		row--;
		col--;
		Fire temp;
		cin >> temp.mass >> temp.speed >> temp.direction;
		map[row][col].fire.push_back(temp);
	}

	cout << getRestFireBallMass();
}

int getRestFireBallMass() {
	for (int i = 0; i < numOfCommand; i++) {
		//이동
		moveFireBall();
		//분리
		splitFireBall();
	}

	int mass = 0;
	for (int i = 0; i < width; i++) {
		for (int j = 0; j < width; j++) {
			for (int k = 0; k < map[i][j].fire.size(); k++)
				mass += map[i][j].fire[k].mass;
		}
	}

	return mass;
}

void moveFireBall() {
	for (int i = 0; i < width; i++) {
		for (int j = 0; j < width; j++) {
			tempMap[i][j].fire.clear();
		}
	}
	for (int row = 0; row < width; row++) {
		for (int col = 0; col < width; col++) {
			for (int i = 0; i < map[row][col].fire.size(); i++) {
				int direction = map[row][col].fire[i].direction;
				int speed = map[row][col].fire[i].speed;
				//스피드가 엄청 빠를때를 처리 안함
				if (direction == 0)
					tempMap[nextRow(row, speed, true)][col].fire.push_back(map[row][col].fire[i]);
				else if (direction == 1)
					tempMap[nextRow(row, speed, true)][nextCol(col, speed, false)].fire.push_back(map[row][col].fire[i]);
				else if (direction == 2)
					tempMap[row][nextCol(col, speed, false)].fire.push_back(map[row][col].fire[i]);
				else if (direction == 3)
					tempMap[nextRow(row, speed, false)][nextCol(col, speed, false)].fire.push_back(map[row][col].fire[i]);
				else if (direction == 4)
					tempMap[nextRow(row, speed, false)][col].fire.push_back(map[row][col].fire[i]);
				else if (direction == 5)
					tempMap[nextRow(row, speed, false)][nextCol(col, speed, true)].fire.push_back(map[row][col].fire[i]);
				else if (direction == 6)
					tempMap[row][nextCol(col, speed, true)].fire.push_back(map[row][col].fire[i]);
				else if (direction == 7)
					tempMap[nextRow(row, speed, true)][nextCol(col, speed, true)].fire.push_back(map[row][col].fire[i]);
			}

		}
	}

	for (int i = 0; i < width; i++) {
		for (int j = 0; j < width; j++) {
			map[i][j].fire.resize(tempMap[i][j].fire.size());
			copy(tempMap[i][j].fire.begin(), tempMap[i][j].fire.end(), map[i][j].fire.begin());
		}
	}
}
void splitFireBall() {
	for (int row = 0; row < width; row++) {
		for (int col = 0; col < width; col++) {
			if (map[row][col].fire.size() >= 2) {
				int mass = 0, speed = 0;
				bool isEven = true, isOdd = true;
				for (int i = 0; i < map[row][col].fire.size(); i++) {
					mass += map[row][col].fire[i].mass;
					speed += map[row][col].fire[i].speed;
					if (map[row][col].fire[i].direction % 2 == 0) {
						isEven = isEven && true;
						isOdd = isOdd && false;
					}
					else {
						isEven = isEven && false;
						isOdd = isOdd && true;
					}
				}
				mass /= 5;
				speed /= map[row][col].fire.size();
				map[row][col].fire.clear();
				if (mass != 0) {
					if (isEven || isOdd) {
						map[row][col].fire.push_back({ mass, 0, speed });
						map[row][col].fire.push_back({ mass, 2, speed });
						map[row][col].fire.push_back({ mass, 4, speed });
						map[row][col].fire.push_back({ mass, 6, speed });
					}
					else {
						map[row][col].fire.push_back({ mass, 1, speed });
						map[row][col].fire.push_back({ mass, 3, speed });
						map[row][col].fire.push_back({ mass, 5, speed });
						map[row][col].fire.push_back({ mass, 7, speed });
					}
				}
			}
		}
	}
}
This post is licensed under CC BY 4.0 by the author.

19238 Samsung sw test

20057 Samsung sw test

Comments powered by Disqus.