-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1873.cpp
144 lines (139 loc) · 2.09 KB
/
1873.cpp
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
// 1873. 상호의 배틀필드
// 2019.07.02
#include<iostream>
#include<string>
using namespace std;
// U D L R
int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, -1, 1 };
char map[21][21];
char tank;
int xPos;
int yPos;
int h;
int w;
void move(int dir, int x, int y)
{
int xx = x + dx[dir];
int yy = y + dy[dir];
if (xx >= h || yy >= w || xx < 0 || yy < 0 || map[xx][yy] != '.')
{
return;
}
else
{
map[x][y] = '.';
map[xx][yy] = tank;
xPos = xx;
yPos = yy;
}
}
void shoot(char tank, int x, int y)
{
int xx = x;
int yy = y;
int dir = 0;
switch (tank)
{
case '^':
dir = 0;
break;
case 'v':
dir = 1;
break;
case '<':
dir = 2;
break;
case '>':
dir = 3;
break;
}
while (true)
{
xx += dx[dir];
yy += dy[dir];
if (xx >= h || yy >= w || xx < 0 || yy < 0)
{
break;
}
else if (map[xx][yy] == '*')
{
map[xx][yy] = '.';
break;
}
else if (map[xx][yy] == '#')
{
break;
}
}
}
int main()
{
int t;
cin >> t;
for (int testCase = 1; testCase <= t; testCase++)
{
cin >> h >> w;
string line[21];
for (int i = 0; i < h; i++)
{
cin >> line[i];
}
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
map[i][j] = line[i][j];
if (map[i][j] == '<' || map[i][j] == '^' || map[i][j] == 'v' || map[i][j] == '>')
{
xPos = i;
yPos = j;
tank = map[i][j]; // 초기 탱크 모양 저장
}
}
}
int length;
cin >> length;
string order;
cin >> order;
for (int i = 0; i < length; i++)
{
switch (order[i])
{
case 'U':
move(0, xPos, yPos);
tank = '^';
map[xPos][yPos] = '^';
break;
case 'D':
move(1, xPos, yPos);
tank = 'v';
map[xPos][yPos] = 'v';
break;
case 'L':
move(2, xPos, yPos);
tank = '<';
map[xPos][yPos] = '<';
break;
case 'R':
move(3, xPos, yPos);
tank = '>';
map[xPos][yPos] = '>';
break;
case 'S':
shoot(tank, xPos, yPos);
break;
}
}
cout << "#" << testCase << " ";
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
cout << map[i][j];
}
cout << endl;
}
}
return 0;
}