본문 바로가기

알고리즘268

1057 토너먼트 1234567891011121314151617181920212223242526272829303132#include #include int main() { int n, a, b; scanf("%d %d %d", &n, &a, &b); if (a > b) { int tmp = a; a = b; b = tmp; } int two = 1, cnt = 1; while (1) { int tmpA = a / two; if (a % two > 0) tmpA++; int tmpB = b / two; if (b % two > 0) tmpB++; if ((tmpA + 1 == tmpB) && (tmpB % 2 == 0)) { printf("%d", cnt); break; } two *= 2; cnt++; }}Colored .. 2020. 2. 22.
2250 트리의 높이와 너비 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103#include #include #include #include using namespace std; vector heightNd;vector child;int par[10001] = { 0 };int ndX[10001] = { 0 };int maxWidth = -1 , mostLevel; void levelWidth(int h) { for (int .. 2020. 1. 22.
9205 맥주 마시면서 걸어가기 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172#include #include #include #include #include #include using namespace std; int dist[105][105] = { 0 }; void bfs(int n) { queue q; int cur = 0; int chk[105] = { 0 }; chk[cur] = 1; q.push(cur); while (!q.empty()) { cur = q.front(); q.pop(); if (cur == n + 1) { prin.. 2020. 1. 21.
1953 팀배분 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172#include #include #include #include using namespace std; vector diff;int team[105] = { 0 }; //-1, 1 두 팀으로 구분 void bfs(int idx) { queue q; team[idx] = 1; q.push(make_pair(idx, 1)); while (!q.empty()) { int cur = q.front().first; int t = q.front().second; q.pop(); .. 2020. 1. 20.
3184 양 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374#include #include #include using namespace std; char arr[255][255] = { 0 };int chk[255][255] = { 0 }; int lamb = 0, wolf = 0, row, col; void bfs(int y, int x) { queue q; int tmpLamb = 0; int tmpWolf = 0; q.push(make_pair(y, x)); chk[y][x] = 1; int dx[4] = { 1.. 2020. 1. 20.
2251 물통 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816.. 2020. 1. 20.
12761 돌다리 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788#include #include #include using namespace std; int a, b, n, m;int bfs(int cur) { queue q; q.push(make_pair(cur, 0)); int dx[2] = { 1,-1 }; int dxA[2] = { a,-a }; int dxB[2] = { b,-b }; int mulAB[2] = { a, b }; int chk[100001] = { .. 2020. 1. 20.
9372 상근이의 여행 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151#include #include #include #include #include #include.. 2020. 1. 20.
5014 스타트링크 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162#include #include #include #include using namespace std; int f, s, g, u, d ; int bfs() { vector chk(1000005,0); queue q; int cur = s; int cnt = 0; chk[cur] = 1; q.push(make_pair(cnt, cur)); int flag = 0; while (!q.empty()) { cur = q.front().second; cnt = q.front().first; // printf(".. 2020. 1. 20.
2589 보물섬 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374#include #include #include using namespace std; char arr[55][55] = {0};char chk[55][55] = { 0 };int dx[4] = { 1,-1,0,0 };int dy[4] = { 0,0,1,-1 };int row, col; typedef struct _dot { int len; int y; int x;}dot; int bfs(int y, int x) { queue q; memcpy(chk, arr,.. 2020. 1. 19.
2644 촌수계산 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899#include #include #include using namespace std; int par[105] = { 0 };int depth[105] = { 0 };int man; void depthSearch(int root) { queue q; int deep = 1; q.push(make_pair(deep, root)); while (!q.empty()) { int .. 2020. 1. 18.
2146 다리만들기 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990#include #include #include #include using namespace std; int dx[4] = { 1,-1, 0,0 };int dy[4] = { 0,0,1,-1 };int arr[105][105] = { 0 };int n; int bridge(int isNum) { int chk[105][105]; memcpy(chk, arr, sizeof(arr)); queue q; for.. 2020. 1. 18.