알고리즘/백준
15685 드래곤커브
아메숑
2019. 5. 8. 02:10
1. 문제
https://www.acmicpc.net/problem/15685
15685번: 드래곤 커브
첫째 줄에 드래곤 커브의 개수 N(1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 드래곤 커브의 정보가 주어진다. 드래곤 커브의 정보는 네 정수 x, y, d, g로 이루어져 있다. x와 y는 드래곤 커브의 시작 점, d는 시작 방향, g는 세대이다. (0 ≤ x, y ≤ 100, 0 ≤ d ≤ 3, 0 ≤ g ≤ 10) 입력으로 주어지는 드래곤 커브는 격자 밖으로 벗어나지 않는다. 드래곤 커브는 서로 겹칠 수 있다. 방향은 0, 1, 2,
www.acmicpc.net
2. 코드
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
int T, x, y, d, g;
int drag[101][101];
void dragon();
int dir[4][2] = { {0,1},{-1,0},{0,-1},{1,0} };
vector<pair<int, int>> c;
struct node {
int x, y; //행,열
};
node fin; //끝점
int main() {
cin >> T;
while (T--) {
c.clear();
cin >> y >> x >> d >> g;
//x, y는 항상 고정(시작점)
drag[x][y] = 1;
c.push_back(make_pair(x, y));
fin.x = x + dir[d][0]; fin.y = y + dir[d][1];
drag[fin.x][fin.y] = 1;
while (g--) {
dragon();
}
}
int cnt = 0;
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 100; j++) {
if (drag[i][j] == 1 && drag[i][j+1] == 1 && drag[i+1][j] == 1 && drag[i+1][j+1] == 1) {
cnt++;
}
}
}
cout << cnt << endl;
}
void dragon() {
int csize = c.size();
for (int i = 0; i < csize; i++) {
//cout << "befor : " << c[i].first << " " << c[i].second << endl;
int nx = c[i].first - fin.x;
int ny = c[i].second - fin.y;
int cx = ny + fin.x; int cy = -nx + fin.y;
drag[cx][cy] = 1;
c.push_back(make_pair(cx, cy));
//cout << "after : " << cx << " " << cy << endl;
}
//시작점은 마지막에 회전(회전한 점이 끝점)
int nx = c[0].first - fin.x;
int ny = c[0].second - fin.y;
int cx = ny + fin.x; int cy = -nx + fin.y;
drag[cx][cy] = 1;
c.push_back(make_pair(fin.x, fin.y));
fin.x = cx; fin.y = cy;
}
3. 후기
10개월 전에 c언어로 풀었던 문제를 c++로 다시 풀어봤다. 회전하고 난 좌표를 구하는게 가장 어려웠다.