알고리즘 문제풀이/백준
[백준 2983] 개구리 공주
홍시홍
2020. 3. 4. 21:57
https://www.acmicpc.net/problem/2983
2983번: 개구리 공주
문제 트럭을 타고 이동하던 중에 상근이는 휴식을 취하기 위해서 호수에 잠시 들렸다. 호수에는 개구리가 살고 있고, 개구리는 호수 위에 떠있는 식물 N개를 점프하면서 다닌다. 오래된 전설에 따르면 개구리에게 키스를 하면 개구리는 아름다운 공주로 변한다고 한다. 일단 개구리를 잡아야 전설이 사실인지 아닌지 확인할 수 있다. 개구리를 잡아보자. 호수는 2차원 평면으로 생각할 수 있고, 식물은 그 평면 위의 점으로 나타낼 수 있다. (x, y)위에 있는 개구리는
www.acmicpc.net
요구사항
점프 방향과 꽃의 위치가 주어졌을 때 마지막으로 개구리가 위치하는 좌표 찾기
풀이
그래프로 그려서 보면 현재 좌표 (x,y)에서 방향 A, B, C, D로 갈 수 있는 곳은 x+y가 같은곳 혹은 x-y 값이 같은 곳이다
1. set 자료구조를 활용하여 자료를 저장할 수 있도록 한다.
2. 세개의 자료를 저장하기 위해서 tuple사용
3. 각 조건에 따라 구현
바킹독님 풀이 참조하였다 어려워...
#include <map>
#include <set>
#include <tuple>
#include <iostream>
using namespace std;
typedef tuple<int, int, int> ti3;
typedef pair<int, pair<int, int> > p;
set<ti3> splus;
set<ti3> sminus;
char dir[100010];
int n, m;
int xpos;
int ypos;
int main() {
scanf("%d%d", &n, &m);
scanf("%s", dir);
for (int i = 0; i < n; i++) {
//cout << i << endl;
int x, y;
scanf("%d%d", &x, &y);
if (i == 0) {
xpos = x;
ypos = y;
}
splus.insert({x+y,x,y} );
sminus.insert( {x-y,x,y } );
}
//set<ti3>::iterator iter;
for (int i = 0; dir[i] != 0; i++) {
int nowx = xpos;
int nowy = ypos;
if (dir[i] == 'A') {
auto iter = sminus.find({ nowx - nowy,nowx,nowy });
if (iter == prev(sminus.end()) ) {
continue;
}
int minus, nextx, nexty;
tie(minus, nextx,nexty) = *next(iter);
if (minus != nowx - nowy) {
continue;
}
sminus.erase(iter);
splus.erase({ nowx + nowy,nowx,nowy });
xpos = nextx;
ypos = nexty;
}
else if (dir[i] == 'B') {
auto iter = splus.find({ nowx + nowy,nowx,nowy });
if (iter == prev(splus.end() )) {
continue;
}
int plus, nextx, nexty;
tie(plus, nextx,nexty) = *next(iter);
if (plus != nowx + nowy) {
continue;
}
sminus.erase({ nowx - nowy,nowx,nowy });
splus.erase({ nowx + nowy,nowx,nowy });
xpos = nextx;
ypos = nexty;
}
else if (dir[i] == 'C') {
auto iter = splus.find({ nowx + nowy,nowx,nowy});
if (iter == splus.begin()) {
continue;
}
int plus, nextx, nexty;
tie(plus, nextx,nexty) = *prev(iter);
if (plus != nowx + nowy) {
continue;
}
sminus.erase({ nowx - nowy,nowx,nowy });
splus.erase({ nowx + nowy,nowx,nowy });
xpos = nextx;
ypos = nexty;
}
else if (dir[i] == 'D') {
auto iter = sminus.find({ nowx - nowy,nowx,nowy });
if (iter == sminus.begin()) {
continue;
}
int minus, nextx, nexty;
tie(minus, nextx,nexty) = *prev(iter);
if (minus != nowx - nowy) {
continue;
}
sminus.erase({ nowx - nowy,nowx,nowy });
splus.erase({ nowx + nowy,nowx,nowy });
xpos = nextx;
ypos = nexty;
}
}
printf("%d %d", xpos, ypos);
}