-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLv4_리틀프렌즈사천성.cpp
More file actions
102 lines (90 loc) · 2.36 KB
/
Lv4_리틀프렌즈사천성.cpp
File metadata and controls
102 lines (90 loc) · 2.36 KB
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
#include <string>
#include <vector>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
vector<pair<int, int> > pos['Z'+1];
char temp_game[101][101];
const int dy[] = {0,1,0,-1};
const int dx[] = {1,0,-1,0};
bool block_remove(char dest, int m, int n) {
int sy = pos[dest][0].first;
int sx = pos[dest][0].second;
temp_game[sy][sx] = '.';
for (int d = 0; d < 4; ++d) { // 4방향으로 진행
int ny = sy + dy[d];
int nx = sx + dx[d];
// 현재방향으로 한칸씩 전진하면서 양옆으로 쭉 가보고 성공하면 return true;
while (ny >= 0 && ny < m && nx >= 0 && nx < n && (temp_game[ny][nx] == '.' || temp_game[ny][nx] == dest)) { // 전진
if (temp_game[ny][nx] == dest) {
temp_game[ny][nx] = '.';
return true;
}
// 왼쪽 검사
int ldir = (d + 7) % 4;
int ly = ny + dy[ldir];
int lx = nx + dx[ldir];
while (ly >= 0 && ly < m && lx >= 0 && lx < n && (temp_game[ly][lx] == '.' || temp_game[ly][lx] == dest)) {
if (temp_game[ly][lx] == dest) {
temp_game[ly][lx] = '.';
return true;
}
ly = ly + dy[ldir];
lx = lx + dx[ldir];
}
// 오른쪽 검사
int rdir = (d + 5) % 4;
int ry = ny + dy[rdir];
int rx = nx + dx[rdir];
while (ry >= 0 && ry < m && rx >= 0 && rx < n && (temp_game[ry][rx] == '.' || temp_game[ry][rx] == dest)) {
if (temp_game[ry][rx] == dest) {
temp_game[ry][rx] = '.';
return true;
}
ry = ry + dy[rdir];
rx = rx + dx[rdir];
}
ny = ny + dy[d]; // 한칸 전진
nx = nx + dx[d];
}
}
return false;
}
string solution(int m, int n, vector<string> board) {
string answer = "";
for (int i = 0; i < 'Z'; i++)
pos[i].clear();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
temp_game[i][j] = board[i][j];
if (board[i][j] >=65)
pos[board[i][j]].push_back(make_pair(i, j));
}
}
bool flag = true;
while (flag) {
flag = false;
for (int i = 'A'; i <= 'Z'; ++i) {
if (pos[i].empty())
continue;
flag = block_remove(i,m,n);
if (flag) {
pos[i].clear();
answer += i;
break;
}
else {
temp_game[pos[i][0].first][pos[i][0].second] = i;
temp_game[pos[i][1].first][pos[i][1].second] = i;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (temp_game[i][j] != '.' && temp_game[i][j] != '*')
return "IMPOSSIBLE";
}
}
return answer;
}