본문 바로가기

알고리즘/BFS18

1697 숨바꼭질 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#include #include #include using namespace std; int dx[3] = { -1,1 }; int bfs(int n, int k) { queue q; q.push(make_pair(0, n)); int chk[200001] = { 0 }; while (!q.empty()) { int len = q.front().first; int x = q.front().second; q.pop(); if (x == k) { return len; } for (int i = 0; i 2020. 1. 14.
7562 나이트의 이동 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354#include #include #include using namespace std; int dx[8] = { 1,2,2,1,-1,-2,-2,-1 };int dy[8] = {-2,-1,1,2,2,1,-1,-2};int bfs(int stX, int stY, int endX, int endY, int width) { queue q; q.push(make_pair(0, make_pair(stY, stX))); int chk[305][305] = { 0 }; chk[stY][stX] = 1; while (!q.empty()) { int.. 2020. 1. 14.
7576 토마토 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061#include #include #include using namespace std; queue q;int arr[1005][1005] = { 0 };int chk[1005][1005] = { 0 };int dx[4] = { 1,-1, 0,0 };int dy[4] = { 0,0,1,-1 }; int bfs(int sum, int endY, int endX) { int rst = 0, len; while (!q.empty()) { len = q.front().first; int y = q.front().se.. 2020. 1. 14.
2178 미로 탐색 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566#include #include #include using namespace std; char arr[101][101] = { 0 };int chk[101][101] = { 0 };int minDist = 1000000000; void bfs(int endY, int endX) { queue q; q.push(make_pair(1, make_pair(0, 0))); chk[0][0] = 1; while (!q.empty()) { int len = q.front().first; int y .. 2020. 1. 13.
말과 기사 더블릿 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816.. 2019. 2. 25.
댐 더블릿 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768#include #include #include using namespace std; int arr[105][105] = { 0 };queue que; int main() { int n; scanf("%d", &n); for (int i = 1; i 2019. 2. 23.