Posts 19236 Samsung sw test
Post
Cancel

19236 Samsung sw test

19236 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
#include <iostream>
#include <cstring>
#define WIDTH 4
#define SHARK 100
#define BLANK -1

using namespace std;

typedef struct Fish {
	int num;
	int row;
	int col;
	int direction;	// 위부터 반시계방향으로 1~8
	bool isAlive;
} Fish;

int map[WIDTH][WIDTH];
Fish fish[WIDTH*WIDTH+1];
Fish shark = { 0,0,0,0 };

int getSumOfFishNum();
void eat(int row, int col);
void moveFish();
void changeFish(int index, int dstIndex, int row = 0, int col = 0);
int getNextSharkRow() {
	if (shark.direction == 1 || shark.direction == 2 || shark.direction == 8)
		return -1;
	else if (shark.direction == 4 || shark.direction == 5 || shark.direction == 6)
		return 1;
	else
		return 0;
}
int getNextSharkCol() {
	if (shark.direction == 2 || shark.direction == 3 || shark.direction == 4)
		return -1;
	else if (shark.direction == 6 || shark.direction == 7 || shark.direction == 8)
		return 1;
	else
		return 0;
}

int main() {
	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < WIDTH; j++) {
			Fish temp = { 0, i, j, 0, true};
			cin >> temp.num >> temp.direction;
			fish[temp.num] = temp;
			map[i][j] = temp.num;
		}
	}
	/*
	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < WIDTH; j++)
			cout << map[i][j] << " ";
		cout << endl;
	}
	for (int i = 0; i < WIDTH * WIDTH + 1; i++) {
		cout << fish[i].num << " " << fish[i].row << " " << fish[i].col << " " << fish[i].direction << " " << fish[i].isAlive << endl;
	}
	*/

	eat(0, 0);
	cout << getSumOfFishNum();
}

int getSumOfFishNum() {
	moveFish();

	//상어 옮기기. 가능한 곳에 옮기고, 옮길때마다 값 저장 후 다시 초기화
	int nextRow = getNextSharkRow();
	int nextCol = getNextSharkCol();
	Fish tempShark = shark;
	Fish tempFish[WIDTH*WIDTH+1];
	memcpy(tempFish, fish, sizeof(tempFish));
	int tempMap[WIDTH][WIDTH];
	memcpy(tempMap, map, sizeof(map));
	int deep = 1, maxNum = 0;
	while (true) {
		int r = shark.row + deep*nextRow;
		int c = shark.col + deep*nextCol;
		if (r < 0 || r >= WIDTH || c < 0 || c >= WIDTH) {
			break;
		}
		else if (map[r][c] == BLANK) {
			deep++;
		}
		else {
			eat(r, c);
			int a = getSumOfFishNum();
			maxNum = a > maxNum ? a : maxNum;
			memcpy(map, tempMap, sizeof(map));
			memcpy(fish, tempFish, sizeof(tempFish));
			shark = tempShark;
			deep++;
		}
	}
	if (maxNum == 0)
		return shark.num;
	return maxNum;
}

void eat(int row, int col) {
	int index = map[row][col];
	map[shark.row][shark.col] = BLANK;
	map[row][col] = SHARK;
	shark.row = row;
	shark.col = col;
	shark.num += fish[index].num;
	shark.direction = fish[index].direction;
	fish[index].isAlive = false;
}

void moveFish() {
	int count = 0;
	for (int i = 1; i <= 16; i++) {
		if (!fish[i].isAlive)
			continue;

		if (fish[i].direction == 1 && fish[i].row != 0 && map[fish[i].row - 1][fish[i].col] != SHARK) {
			changeFish(i, map[fish[i].row - 1][fish[i].col], fish[i].row-1, fish[i].col);
			count = 0;
		}
		else if (fish[i].direction == 2 && (fish[i].row != 0 && fish[i].col != 0) && map[fish[i].row - 1][fish[i].col - 1] != SHARK) {
			changeFish(i, map[fish[i].row - 1][fish[i].col - 1], fish[i].row - 1, fish[i].col-1);
			count = 0;
		}
		else if (fish[i].direction == 3 && fish[i].col != 0 && map[fish[i].row][fish[i].col - 1] != SHARK) {
			changeFish(i, map[fish[i].row][fish[i].col - 1], fish[i].row, fish[i].col-1);
			count = 0;
		}
		else if (fish[i].direction == 4 && (fish[i].row != 3 && fish[i].col != 0) && map[fish[i].row + 1][fish[i].col - 1] != SHARK) {
			changeFish(i, map[fish[i].row + 1][fish[i].col - 1], fish[i].row +  1, fish[i].col-1);
			count = 0;
		}
		else if (fish[i].direction == 5 && fish[i].row != 3 && map[fish[i].row + 1][fish[i].col] != SHARK) {
			changeFish(i, map[fish[i].row + 1][fish[i].col], fish[i].row + 1, fish[i].col);
			count = 0;
		}
		else if (fish[i].direction == 6 && (fish[i].row != 3 && fish[i].col != 3)&& map[fish[i].row + 1][fish[i].col +1] != SHARK) {
			changeFish(i, map[fish[i].row + 1][fish[i].col +1], fish[i].row + 1, fish[i].col+1);
			count = 0;
		}
		else if (fish[i].direction == 7 && fish[i].col != 3 && map[fish[i].row][fish[i].col+1] != SHARK) {
			changeFish(i, map[fish[i].row][fish[i].col+1], fish[i].row, fish[i].col+1);
			count = 0;
		}
		else if (fish[i].direction == 8 && (fish[i].row != 0 && fish[i].col != 3) && map[fish[i].row - 1][fish[i].col+1] != SHARK) {
			changeFish(i, map[fish[i].row - 1][fish[i].col+1], fish[i].row - 1, fish[i].col+1);
			count = 0;
		}
		else {
			if (fish[i].direction != 8)
				fish[i].direction++;
			else
				fish[i].direction = 1;
			count++;
			if (count >= 8)
				continue;
			i--;
		}
	}
}

void changeFish(int index, int dstIndex, int row, int col) {
	if (dstIndex == BLANK) {
		map[fish[index].row][fish[index].col] = BLANK;
		map[row][col] = index;
		fish[index].row = row;
		fish[index].col = col;
	}
	else {
		int tempRow = fish[dstIndex].row;
		int tempCol = fish[dstIndex].col;
		map[fish[dstIndex].row][fish[dstIndex].col] = index;
		map[fish[index].row][fish[index].col] = dstIndex;
		fish[dstIndex].row = fish[index].row;
		fish[dstIndex].col = fish[index].col;
		fish[index].row = tempRow;
		fish[index].col = tempCol;
	}
}
This post is licensed under CC BY 4.0 by the author.

1799 bishop

19238 Samsung sw test

Comments powered by Disqus.