알고리즘
백준 1730번 판화 C++
영춘권의달인
2022. 2. 10. 13:38
수직으로만 지난 곳은 1, 수평으로만 지난 곳은 2, 수직 수평 모두 지난 곳은 3으로 저장하고 상황에 맞게 구현하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
vector<vector<int>> board(n, vector<int>(n, 0));
string str;
cin >> str;
int row = 0;
int col = 0;
for (int i = 0; i < str.length(); i++) {
int dir = -1;
switch (str[i]) {
case 'U':
dir = 0;
break;
case 'R':
dir = 1;
break;
case 'D':
dir = 2;
break;
case 'L':
dir = 3;
break;
default:
break;
}
int nextRow = row + dRow[dir];
int nextCol = col + dCol[dir];
if (nextRow < 0 || nextRow >= n) continue;
if (nextCol < 0 || nextCol >= n) continue;
if (dir == 0 || dir == 2) {
if (board[row][col] == 2 || board[row][col] == 3) {
board[row][col] = 3;
}
else {
board[row][col] = 1;
}
if (board[nextRow][nextCol] == 2 || board[nextRow][nextCol] == 3) {
board[nextRow][nextCol] = 3;
}
else {
board[nextRow][nextCol] = 1;
}
}
else if (dir == 1 || dir == 3) {
if (board[row][col] == 1 || board[row][col] == 3) {
board[row][col] = 3;
}
else {
board[row][col] = 2;
}
if (board[nextRow][nextCol] == 1 || board[nextRow][nextCol] == 3) {
board[nextRow][nextCol] = 3;
}
else {
board[nextRow][nextCol] = 2;
}
}
row = nextRow;
col = nextCol;
}
for (auto a : board) {
for (auto b : a) {
if (b == 0) {
cout << '.';
}
else if (b == 1) {
cout << '|';
}
else if (b == 2) {
cout << '-';
}
else if (b == 3) {
cout << '+';
}
}
cout << "\n";
}
};