알고리즘/BOJ(C++)
[BOJ] 14226 :: 이모티콘
쿠마쿠마34
2018. 7. 3. 15:22
반응형
https://www.acmicpc.net/problem/14226
pair를 한 정접으로 하는 그래프를 생각하고, bfs탐색을 진행합니다.
pair의 first원소는 현재 화면에 있는 이모티콘의 개수이고 second원소는 클립보드에 있는 이모티콘의 개수입니다.
index관련 예외처리만 잘해주면 됩니다!
요즘 런탐에러 너무많이나네요ㅠ 실수안해야지..
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <iostream> #include <queue> using namespace std; int s; bool check[1003][2003]; int dist[1003][2003]; int main() { cin >> s; queue<pair<int, int> > q; q.push(make_pair(1, 0)); check[1][0] = true; dist[1][0] = 0; int cnt = 0; bool flag = false; while (!q.empty()) { int x = q.front().first; int y = q.front().second; q.pop(); if (x == s) { cout << dist[x][y] << endl; flag = true; break; } if (check[x][x] == false) { q.push(make_pair(x, x)); check[x][x] = true; dist[x][x] = dist[x][y] + 1; } if (y != 0 && (x+y <= s) && check[x + y][y] == false) { q.push(make_pair(x + y, y)); check[x + y][y] = true;; dist[x + y][y] = dist[x][y] + 1; } if (x >= 1 && check[x - 1][y] == false) { q.push(make_pair(x - 1, y)); check[x - 1][y] = true; dist[x - 1][y] = dist[x][y] + 1; } } } | cs |
반응형