본문 바로가기

백준 알고리즘/BFS47

백준 5014번 C++ #include #include #include using namespace std; const int MAX = 1000001; int f, s, g, u, d; int floors[MAX]; int bfs() { queue q; int answer = -1; q.push(s); floors[s] = 0; while (!q.empty()) { int current = q.front(); if (current == g) { answer = floors[current]; break; } q.pop(); if (current + u >= 1 && current + u = 1 && current - d > f >> s >> g >> u >> d; memset(floors, -1, sizeof(floors));.. 2019. 8. 12.
백준 2589번 C++ #include #include #include #include #include using namespace std; const int MAX = 51; int n, m; int map[MAX][MAX]; vector v; int visited[MAX][MAX]; struct Move { int x, y; }; Move mv[4] = { {1,0}, {0,1}, {-1,0}, {0,-1} }; int bfs(int i, int j) { int answer = 0; memset(visited, -1, sizeof(visited)); queue way; way.push(make_pair(i,make_pair(j,0))); visited[i][j] = 1; while (!way.empty()) { int cu.. 2019. 8. 12.
백준 7569번 C++ #include #include #include using namespace std; const int MAX = 101; int tomato[MAX][MAX][MAX]; int m, n, h, tmnum=0,day = 0; queue tm; struct Move { int x, y, z; }; Move mv[6] = { {-1,0,0}, {1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1} }; bool finish(int i) { if (i == tmnum) return true; else return false; } int bfs(){ int finishnum = tm.size(); while (!tm.empty()) { int size = tm.size(); for (.. 2019. 8. 10.
백준 2468번 C++ #include #include #include using namespace std; const int MAX = 101; int n; int height[MAX][MAX]; int visited[MAX][MAX]; struct Move { int x, y; }; Move mv[4] = { {1,0},{-1,0},{0,1},{0,-1} }; void bfs(int i, int j, int k) { queue q; q.push(make_pair(i, j)); visited[i][j] = 1; while (!q.empty()) { int currenti = q.front().first; int currentj = q.front().second; q.pop(); for (int index = 0; index .. 2019. 8. 10.