Posts 20057 Samsung sw test
Post
Cancel

20057 Samsung sw test

20057 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#define MAX_WIDTH 499

using namespace std;

int map[MAX_WIDTH][MAX_WIDTH];
bool visit[MAX_WIDTH][MAX_WIDTH];
int width, amountOfSand = 0;
int ten, seven, five, two, one, rest;

int getLostAmountSand();
void setNum(int row, int col) {
	ten = (float)map[row][col] * 0.1;
	seven = (float)map[row][col] * 0.07;
	five = (float)map[row][col] * 0.05;
	two = (float)map[row][col] * 0.02;
	one = (float)map[row][col] * 0.01;
	rest = map[row][col] - (2 * ten + 2 * seven + five + 2 * two + 2 * one);
}
void spreadLeftSand(int row, int col);
void spreadRightSand(int row, int col);
void spreadUpSand(int row, int col);
void spreadDownSand(int row, int col);

int main() {
	cin >> width;
	for (int i = 0; i < width; i++) {
		for (int j = 0; j < width; j++) {
			cin >> map[i][j];
			if (map[i][j] != 0)
				amountOfSand += map[i][j];
		}
	}

	cout << getLostAmountSand();
}

int getLostAmountSand() {
	visit[width / 2][width / 2] = true;
	bool left = true, down = false, right = false, up = false;
	int row = width / 2, col = width / 2;
	for (int i = 1; i < width * width; i++) {
		cout << row << " " << col << endl;

		if (left) {
			col--;
			spreadLeftSand(row, col);
		}
		else if (down) {
			row++;
			spreadDownSand(row, col);
		}
		else if (right) {
			col++;
			spreadRightSand(row, col);
		}
		else if (up) {
			row--;
			spreadUpSand(row, col);
		}
		visit[row][col] = true;
		if (left && !visit[row + 1][col]) {
			left = false;
			down = true;
		}
		else if (down && !visit[row][col + 1]) {
			down = false;
			right = true;
		}
		else if (right && !visit[row - 1][col]) {
			right = false;
			up = true;
		}
		else if (up && !visit[row][col - 1]) {
			up = false;
			left = true;
		}
		for (int j = 0; j < width; j++) {
			for (int k = 0; k < width; k++) {
				cout << map[j][k] << " ";
			}
			cout << endl;
		}
		cout << endl;
	}

	int sand = 0;
	for (int i = 0; i < width; i++)
		for (int j = 0; j < width; j++)
			sand += map[i][j];
	return amountOfSand - sand;
}
void spreadLeftSand(int row, int col) {
	setNum(row, col);
	if (row > 1)
		map[row - 2][col] += two;
	if (row > 0) {
		map[row - 1][col] += seven;
		map[row - 1][col + 1] += one;
	}
	if (row > 0 && col > 0)
		map[row - 1][col - 1] += ten;
	if (col > 1)
		map[row][col - 2] += five;
	if (row < width - 1 && col > 0)
		map[row + 1][col - 1] += ten;
	if (row < width - 1) {
		map[row + 1][col] += seven;
		map[row + 1][col + 1] += one;
	}
	if (row < width - 2)
		map[row + 2][col] += two;
	if (col > 0)
		map[row][col - 1] += rest;
	map[row][col] = 0;
}
void spreadRightSand(int row, int col) {
	setNum(row, col);
	if (row > 1)
		map[row - 2][col] += two;
	if (row > 0) {
		map[row - 1][col] += seven;
		map[row - 1][col - 1] += one;
	}
	if (row > 0 && col < width -1)
		map[row - 1][col + 1] += ten;
	if (col < width - 2)
		map[row][col + 2] += five;
	if (row < width - 1 && col < width-1)
		map[row + 1][col + 1] += ten;
	if (row < width - 1) {
		map[row + 1][col] += seven;
		map[row + 1][col - 1] += one;
	}
	if (row < width - 2)
		map[row + 2][col] += two;
	if (col < width - 1)
		map[row][col + 1] += rest;
	map[row][col] = 0;
}
void spreadUpSand(int row, int col) {
	setNum(row, col);
	if (col > 1)
		map[row][col - 2] += two;
	if (col > 0) {
		map[row][col - 1] += seven;
		map[row + 1][col - 1] += one;
	}
	if (row > 0 && col > 0)
		map[row - 1][col - 1] += ten;
	if (row > 1)
		map[row - 2][col] += five;
	if (row > 0 && col < width-1)
		map[row - 1][col + 1] += ten;
	if (col < width - 1) {
		map[row][col + 1] += seven;
		map[row + 1][col + 1] += one;
	}
	if (col < width - 2)
		map[row][col + 2] += two;
	if (row > 0)
		map[row - 1][col] += rest;
	map[row][col] = 0;
}
void spreadDownSand(int row, int col) {
	setNum(row, col);
	if (col > 1)
		map[row][col - 2] += two;
	if (col > 0) {
		map[row][col - 1] += seven;
		map[row - 1][col - 1] += one;
	}
	if (row < width - 1 && col < width - 1)
		map[row + 1][col + 1] += ten;
	if (row < width - 2)
		map[row + 2][col] += five;
	if (row < width - 1 && col > 0)
		map[row + 1][col - 1] += ten;
	if (col < width - 1) {
		map[row][col + 1] += seven;
		map[row - 1][col + 1] += one;
	}
	if (col < width - 2)
		map[row][col + 2] += two;
	if (row < width - 1)
		map[row + 1][col] += rest;
	map[row][col] = 0;
}
This post is licensed under CC BY 4.0 by the author.

20056 Samsung sw test

20058 Samsung sw test

Comments powered by Disqus.