https://www.acmicpc.net/problem/1697
1697번: 숨바꼭질
수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일
www.acmicpc.net
중요포인트가 동일한 경우 0
그리고 방문여부 중복 회피하는게 중요하네요 흐음~
#include <iostream>
#include <vector>
#include <queue>
#include<string>
#include<algorithm>
using namespace std;
struct Point {
int n, cnt=0;
};
void Prints(vector<vector<int>>maps, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[i][j];
}
cout << endl;
}
cout << endl;
}
void Prints(vector<vector<bool>>maps, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << maps[i][j];
}
cout << endl;
}
cout << endl;
}
int main() {
int n;
int k;
cin >> n >> k;
//5 12
int gCount = 0;
int dir[] = { 1,-1,2 };
queue<Point>q;
vector<bool>bVisted(1000000, false);
Point p;
p.n = n;
p.cnt = 0;
q.push(p);
bVisted[p.n] = true;
bool bEnd = false;
if (n == k) {
cout << 0;
return 0;
}
while (!q.empty()) {
Point start = q.front();
q.pop();
if (bEnd == true) {
break;
}
for (int i = 0; i < 3; i++) {
int tmpQ = -1;
if (i < 2) {
tmpQ = start.n + dir[i];
}
else {
tmpQ = start.n * dir[i];
}
if (tmpQ <= -1 || tmpQ >= 100001||bVisted[tmpQ]==true) {
continue;
}
if(tmpQ==k){
//cout << k;
cout << start.cnt + 1;
bEnd = true;
i = 4;
}
//cout << tmpQ<<",";
Point tmpP;
tmpP.n = tmpQ;
bVisted[tmpQ] = true;
tmpP.cnt = start.cnt + 1;
q.push(tmpP);
}
}
return 0;
}
'알고리즘 공부' 카테고리의 다른 글
백준 안전 영역 (0) | 2023.07.09 |
---|---|
백준 알파벳 (0) | 2023.07.09 |
백준 단지번호붙이기 (0) | 2023.07.07 |
백준 벽 부수고 이동하기 (0) | 2023.07.07 |
백준 탈출 (0) | 2023.07.05 |