끄적끄적 코딩
article thumbnail
[C++] 백준 3055번 탈출
알고리즘 2019. 9. 13. 19:54

BFS로 문제를 풀었습니다. 고슴도치가 한칸씩 이동해서 비버의 굴로 도착할 수 있는지 있다면 몇번만에 도착하는지와 없다면 KAKTUS를 출력해야합니다. 고슴도치가 한칸 이동할때마다 물이 상하좌우로 한칸씩 늘어나게 됩니다. 물을 한칸씩 증가시킨후 BFS를 통해서 갈 수 있는곳을 탐색하는 것을 반복하여서 문제를 풀었습니다. #include #include #include #include #include using namespace std; int moveX[4] = { 0, 1, 0, -1}; int moveY[4] = { 1, 0, -1, 0}; int main(int argc, char* argv[]) { int n, m; int x, y; int a, b, c; int turn; int waterA, ..

article thumbnail
[C++] 백준 2589번 보물섬
알고리즘 2019. 9. 13. 18:52

DFS, BFS 문제입니다. 육지에서 가장 멀리 떨어져있는 두곳의 거리를 출력해야합니다. 육지에 대해서 모두 BFS를 해서 count가 가장 높게 나오는 곳을 찾으면 됩니다. #include #include #include #include #include using namespace std; int moveX[4] = { 0, 1, 0, -1}; int moveY[4] = { 1, 0, -1, 0}; int main(int argc, char* argv[]) { int n, m; int x, y; int a, b, c; int count = 0; int dist = 0; int map[60][60]; int visit[60][60]; string s; queue land; queueq; cin >> n ..

article thumbnail
[C++] 백준 7562번 나이트의 이동
알고리즘 2019. 9. 13. 18:33

나이트가 a, b에서 x, y까지 가는데 걸리는 최소 이동횟수를 구하는 문제입니다. 나이트의 이동을 배열로 선언했습니다. int moveX[8] = { 2, 2, -2, -2, 1, 1, -1, -1 }; int moveY[8] = { 1, -1, 1, -1, 2, -2, 2, -2 }; BFS를 통해서 모든 경로를 찾고 x, y에 도착하면 이동횟수를 출력했습니다. #include #include #include #include #include using namespace std; int moveX[8] = { 2, 2, -2, -2, 1, 1, -1, -1 }; int moveY[8] = { 1, -1, 1, -1, 2, -2, 2, -2 }; int main(int argc, char* argv[])..

article thumbnail
[C++] 백준 2468번 안전 영역
알고리즘 2019. 9. 13. 17:46

DFS, BFS 문제입니다. 각 지역의 높이가 주어졌을 때 물의 높이가 몇일 때 가장 많은 영역이 생성 되는지 구하는 문제입니다. 1~100까지 모든 경우를 구해봅니다. x일때 x이하의 값들을 전부 0으로 한 후에 0이 아닌 값들을 DFS해서 방문처리 해주었습니다. DFS가 종료되었을 때 count를 증가해주었고 이를 통해 높이가 x일때 count를 구할 수 있었습니다. 높이가 x일때 count중 가장 큰 값을 출력하여 문제를 해결하였습니다. #include #include #include #include using namespace std; int n; int maxHeight = 0; int maxCount = 1; int map[110][110]; int visit[110][110]; int mov..

article thumbnail
[C++] 백준 1057번 토너먼트
알고리즘 2019. 9. 13. 17:16

N명이 토너먼트를 할 때 A와 B는 몇번째에 만날 수 있는지 출력하는 문제입니다. 문제는 A와 B는 항상 이기는 경우에 대해서만 생각합니다. x/2 + x%2를 하여서 매 경기마다의 위치를 파악할 수 있었습니다. #include #include #include #include #include using namespace std; int main(int argc, char* argv[]) { int n; int a, b; int count = 0; cin >> n; cin >> a >> b; while (a != b) { a = a / 2 + a % 2; b = b / 2 + b % 2; ++count; } cout

article thumbnail
[C++] 백준 1977번 완전제곱수
알고리즘 2019. 9. 13. 17:05

M~N사이의 완전제곱수를 구하는 문제입니다. 0부터 차례대로 제곱을 하여 M~N사이면 벡터에 넣어주고 sum에 더해줍니다. N을 넘으면 반복문을 나가고, 벡터의 사이즈가 0이면 -1을 출력하고 그렇지 않으면 sum과 vector의 첫번째 값을 출력합니다. #include #include #include #include #include #include using namespace std; int main(int argc, char* argv[]) { int m, n; int i = 0; int num = 0; int sum = 0; vector v; cin >> m >> n; while (1) { num = pow(i, 2); if (num > n) { break; } if (num >= m) { v.pu..

article thumbnail
[C++] 백준 1076번 저항
알고리즘 2019. 9. 13. 15:52

첫번째 문자와 두번째 문자는 값으로 만들고 세번째 문자는 값을 곱해주면 됩니다. int형의 범위를 넘어가기 때문에 long long으로 해주었습니다. #include #include #include #include #include using namespace std; long long result = 0; void mul(string s) { if (s == "black") { result *= 1; } else if (s == "brown") { result *= 10; } else if (s == "red") { result *= 100; } else if (s == "orange") { result *= 1000; } else if (s == "yellow") { result *= 10000; } ..

article thumbnail
[C++] 백준 2010번 플러그
알고리즘 2019. 9. 13. 15:33

플러그가 하나 뿐일 때 N개의 멀티탭으로 몇개의 컴퓨터를 켤 수 있는지 찾는 문제입니다. 멀티탭을 꼽기위해서 다른 멀티탭의 플러그를 사용해야하므로 멀티탭안의 플러그 개수 - 1을 sum에 다 더해줍니다. 그리고 마지막 멀티탭은 다른 멀티탭이 연결하지 않으므로 +1을 해서 개수를 출력해줍니다. #include #include #include #include #include using namespace std; int main(int argc, char* argv[]) { int n; int num; int sum = 0; cin >> n; while (n--) { cin >> num; sum += num - 1; } ++sum; cout

검색 태그