SW Expert Academy의 상호의 배틀필드(1873 / D3) 문제이다.
[ 문제풀이 ]
1) 이 문제는 주어진 명령에 맞게 탱크의 진행방향과, 맵의 상태를 잘 조절해가면서 진행해나가야 하는 문제이다.
가장 핵심은 탱크의 진행방향이라고 생각한다.
처음 입력 받을 때, 탱크의 생김새를 보고 현재 탱크가 바라보고 있는 곳을 진행방향으로 설정해주자.
첫 진행방향을 설정했다면, 이제부터는 명령어들을 해결해보자.
'U' / 'D' / 'L' / 'R'의 명령어가 나온다면 명령어가 가르키는 해당 방향으로 탱크의 방향을 바꾼 후, 그 방향으로 전진할 수
있다면 전진을 하고, 그렇지 않다면 제자리에서 방향만 바꿔줘야 한다.
여기서 전진할 수 없다는 것은, 전진하려는 칸이 맵의 범위를 벗어난 경우, 벽돌인 경우, 강철인경우, 물인 경우에는
전진을 할 수 없다. 즉, 평지일 때만 탱크는 전진이 가능하다.
'S'의 명령어가 나오게 된다면, 포탄을 발사하는데, 현재 탱크가 바라보고 있는 방향을 포탄을 발사하게 된다.
포탄은 중간에 강철로 된 벽을 만나게 되면 그 자리에서 소멸하게 되고, 맵 밖으로 나가도 소멸되고, 벽돌로 만들어진
벽에 부딪히면 그 벽돌로 만들어진 지형을 평지로 만들고 소멸하게 된다.
[ 소스코드 ]
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | #include<iostream> #include<vector> #include<cstring> #define endl "\n" #define MAX 20 using namespace std; int H, W, N; char MAP[MAX][MAX]; vector<char> Cmd; pair<pair<int, int>, int> Tank; int dx[] = { 0, 0, 1, -1 }; int dy[] = { 1, -1, 0, 0 }; void Initialize() { Tank.first.first = Tank.first.second = Tank.second = -1; Cmd.clear(); memset(MAP, 0, sizeof(MAP)); } void Input() { cin >> H >> W; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> MAP[i][j]; if (MAP[i][j] == '<' || MAP[i][j] == '>' || MAP[i][j] == '^' || MAP[i][j] == 'v') { Tank.first.first = i; Tank.first.second = j; if (MAP[i][j] == '<') Tank.second = 1; else if (MAP[i][j] == '>') Tank.second = 0; else if (MAP[i][j] == '^') Tank.second = 3; else if (MAP[i][j] == 'v') Tank.second = 2; } } } cin >> N; for (int i = 0; i < N; i++) { char A; cin >> A; Cmd.push_back(A); } } void Shooting() { int x = Tank.first.first; int y = Tank.first.second; int dir = Tank.second; int nx = x + dx[dir]; int ny = y + dy[dir]; while (1) { if (nx < 0 || ny < 0 || nx >= H || ny >= W) break; if (MAP[nx][ny] == '#') break; else if (MAP[nx][ny] == '*') { MAP[nx][ny] = '.'; break; } else { nx = nx + dx[dir]; ny = ny + dy[dir]; } } } void Solution() { for (int i = 0; i < Cmd.size(); i++) { char C = Cmd[i]; if (C == 'S') Shooting(); else if (C == 'U') { Tank.second = 3; int x = Tank.first.first; int y = Tank.first.second; int nx = x + dx[Tank.second]; int ny = y + dy[Tank.second]; if (nx < 0 || ny < 0 || nx >= H || ny >= W) { MAP[x][y] = '^'; } else { if (MAP[nx][ny] == '.') { Tank.first.first = nx; Tank.first.second = ny; MAP[nx][ny] = '^'; MAP[x][y] = '.'; } else MAP[x][y] = '^'; } } else if (C == 'D') { Tank.second = 2; int x = Tank.first.first; int y = Tank.first.second; int nx = x + dx[Tank.second]; int ny = y + dy[Tank.second]; if (nx < 0 || ny < 0 || nx >= H || ny >= W) { MAP[x][y] = 'v'; } else { if (MAP[nx][ny] == '.') { Tank.first.first = nx; Tank.first.second = ny; MAP[nx][ny] = 'v'; MAP[x][y] = '.'; } else MAP[x][y] = 'v'; } } else if (C == 'L') { Tank.second = 1; int x = Tank.first.first; int y = Tank.first.second; int nx = x + dx[Tank.second]; int ny = y + dy[Tank.second]; if (nx < 0 || ny < 0 || nx >= H || ny >= W) { MAP[x][y] = '<'; } else { if (MAP[nx][ny] == '.') { Tank.first.first = nx; Tank.first.second = ny; MAP[nx][ny] = '<'; MAP[x][y] = '.'; } else MAP[x][y] = '<'; } } else if (C == 'R') { Tank.second = 0; int x = Tank.first.first; int y = Tank.first.second; int nx = x + dx[Tank.second]; int ny = y + dy[Tank.second]; if (nx < 0 || ny < 0 || nx >= H || ny >= W) { MAP[x][y] = '>'; } else { if (MAP[nx][ny] == '.') { Tank.first.first = nx; Tank.first.second = ny; MAP[nx][ny] = '>'; MAP[x][y] = '.'; } else MAP[x][y] = '>'; } } } } void Print() { for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cout << MAP[i][j]; } cout << endl; } } void Solve() { int Tc; cin >> Tc; for (int T = 1; T <= Tc; T++) { Initialize(); Input(); Solution(); cout << "#" << T << " "; Print(); } } int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("Input.txt", "r", stdin); Solve(); return 0; } | cs |
'[ SWEA Code ] > # SWEA - ' 카테고리의 다른 글
[ SWEA 2112 ] 보호 필름 (C++) (2) | 2019.04.09 |
---|---|
[ SWEA 3378 ] 스타일리쉬 들여쓰기 (C++) (0) | 2019.04.03 |
[ SWEA 1213 ] String (C++) (0) | 2019.03.04 |
[ SWEA 1251 ] 하나로 (C++) (2) | 2019.03.04 |
[ SWEA 1211 ] Ladder2 (C++) (0) | 2019.02.20 |