Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 다익스트라
- 트리
- 다이나믹 프로그래밍
- 우선순위 큐
- 백트래킹
- DFS
- 유니온 파인드
- ue5
- 그래프
- XR Interaction Toolkit
- Unreal Engine 5
- Team Fortress 2
- 투 포인터
- 브루트포스
- 그리디 알고리즘
- VR
- 백준
- 자료구조
- 스택
- 누적 합
- 수학
- 알고리즘
- 문자열
- 유니티
- BFS
- 정렬
- 구현
- 재귀
- 시뮬레이션
- c++
Archives
- Today
- Total
1일1알
백준 1730번 판화 C++ 본문
수직으로만 지난 곳은 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";
}
};
'알고리즘' 카테고리의 다른 글
백준 3258번 컴포트 C++ (0) | 2022.02.12 |
---|---|
백준 2082번 시계 C++ (0) | 2022.02.11 |
백준 2374번 같은 수로 만들기 C++ (0) | 2022.02.09 |
백준 14600번 샤워실 바닥 깔기 (Small) C++ (0) | 2022.02.08 |
백준 1275번 커피숍2 C++ (0) | 2022.02.07 |