목록Algorithm/C++ (148)
cgy12306
// https://www.acmicpc.net/problem/2636 // 치즈 #include #include #include #include using namespace std; int N, M, cnt = 0, time = 0; int map[102][102]; int visited[102][102]; int dx[] = { 1,-1,0,0 }; int dy[] = { 0,0,1,-1 }; queue q; vector cv; void bfs() { while (cnt) { memset(visited, 0, sizeof(visited)); visited[0][0] = true; q.push({ 0,0 }); cv.push_back(cnt); while (!q.empty()) { int x = q.f..
// https://www.acmicpc.net/problem/3055 // 탈출 #include #include #include using namespace std; int R, C; char map[52][52]; int visited[52][52]; int dx[] = { 1,-1,0,0 }; int dy[] = { 0,0,1,-1 }; int goalx, goaly; queue w; queue q; int bfs() { visited[q.front().first][q.front().second] = 1; while (!q.empty()) { int wSize = w.size(); for (int i = 0; i < wSize; i++) { int x = w.front().first; int y =..
// https://www.acmicpc.net/problem/2206 // 벽 부수고 이동하기 #include #include #include using namespace std; int N, M; int map[1002][1002]; int visited[1002][1002][2]; int dx[] = { 1,-1,0,0 }; int dy[] = { 0,0,1,-1 }; queue q; int bfs() { visited[1][1][1] = 1; q.push({{ 1,1 }, 1}); while (!q.empty()) { int x = q.front().first.first; int y = q.front().first.second; int b = q.front().second; q.pop(); if ..
홀수의 개수에 따라 결과 값이 정해져 있는 문제이다. 최선을 다한다고 하지만 최악의 경우로 갔을 때도 최선을 다했을 때와 결과는 일치한다. // https://www.acmicpc.net/problem/24678 // 돌무더기 게임 1 #include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T; cin >> T; while (T--) { int x, y, z, cnt = 0; cin >> x >> y >> z; if (x % 2) cnt++; if (y % 2) cnt++; if (z % 2) cnt++; if (cnt == 3)cout
겉에 물이 빠져나갈 수 있는 테두리를 하나 만들어주고, 이후 bfs를 돌려 한 칸씩 바깥의 물을 채워나감과 동시에 안쪽에 채워지지 않는 부분을 체크하여 높이를 구함 // https://www.acmicpc.net/problem/1113 // 수영장 만들기 #include #include #include using namespace std; int N, M; int map[52][52]; int Max = 0; int dx[] = { -1, 1, 0, 0 }; int dy[] = { 0, 0, -1, 1 }; int sum = 0; queue q; void bfs(int h) { map[0][0] = h; q.push({ 0, 0 }); while (!q.empty()) { int x = q.front()..
// https://www.acmicpc.net/problem/12852 // 1로 만들기 2 #include #include using namespace std; int dp[1000001], path[1000001]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int X; cin >> X; dp[1] = 0; path[1] = -1; for (int i = 2; i dp[i / 2] + 1) { dp[i] = dp[i / 2] + 1; path[i] = i / 2; } if (i % 3 == 0 && dp[i] > dp[i / 3] + 1) { dp[i] = dp[i / 3] + 1; path[i] = i / 3;..
dp[4]=11까지는 생각을 했지만 그 이후에 답이 안나와서 다른사람의 블로그를 참고했다. #include using namespace std; int dp[32]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; cin >> N; dp[0] = 1; dp[1] = 0; dp[2] = 3; for (int i = 4; i = 0; j -= 2) { dp[i] += dp[j] * 2; } } cout
// https://www.acmicpc.net/problem/2357 // 최솟값과 최댓값 #include #include using namespace std; int INF = 1000000001; int N, M; int arr[100002]; int max_tree[4 * 100002 + 4]; int min_tree[4 * 100002 + 4]; int init_min(int start, int end, int node) { if (start == end) return min_tree[node] = arr[start]; int mid = (start + end) / 2; return min_tree[node] = min(init_min(start, mid, node * 2), init_min(m..