์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
- GCP
- error
- ๋ถํ ์ ๋ณต
- CVE
- ๋ฐฑํธ๋ํน
- ๊ตฌํ
- BFS
- ์ฌ๊ท
- ๋ฌธ์์ด
- ๋งต
- ์ฐ์ ์์ ํ
- ๋์ ํฉ
- ์คํ
- thymeleaf
- java
- Spring
- JPA
- ์ด๋ถ ํ์
- ์๋ฎฌ๋ ์ด์
- dynamic debugging
- Reversing
- ๊ทธ๋ฆฌ๋
- DP
- web
- dfs
- OS
- ์์ ์ ๋ ฌ
- ๋ฐ์ดํฌ์คํธ๋ผ
- c++
- ์ต๋จ ๊ฒฝ๋ก
- Today
- Total
hades
[Baekjoon] 14500๋ฒ: ํ ํธ๋ก๋ฏธ๋ ธ ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/14500
๐ ์ค๊ณ
์ฒ์์๋ ์์ ์ขํ๋ฅผ ์ก๊ณ , ๊ทธ ์ขํ์์ ์์ํ๋ ํ ํธ๋ก๋ฏธ๋ ธ ์ผ์ด์ค๋ฅผ ๋ชจ๋ ๋ง๋ค์ด์ ๊ฐ์ ๋์ถํ๋ ค ํ์ผ๋, ๋์นญ๊ณผ ํ์ ๊น์ง ๊ณ ๋ คํ์ ๋, ์ผ์ด์ค๊ฐ ๋๋ฌด ๋ง์์ง๋ค.
ํ ํธ๋ก๋ฏธ๋ ธ์ ํน์ง์ ์ธ์ ํ ์นธ์ผ๋ก ์ด๋ฃจ์ด์ ธ ์๋ค๋ ๊ฒ์ด๋ค. ๋ฐ๋ผ์, DFS๋ฅผ ์ด์ฉํ์ฌ 4๊ฐ์ ์นธ์ผ๋ก ์ด๋ฃจ์ด์ง ํ ํธ๋ก๋ฏธ๋ ธ๋ฅผ ๋ง๋ค ์ ์๋ค. (poli)
ํ์ง๋ง, ใ ๋ชจ์์ ํ ํธ๋ก๋ฏธ๋ ธ๋ ์ธ์ ํ ์นธ์ผ๋ก ํ์ฅํด๋๊ฐ๋ DFS๋ก ๊ตฌํ ์ ์์ผ๋ฏ๋ก, ๋ฐ๋ก ๋ง๋ค์ด์ผ ํ๋ค. ใ ๋ชจ์์ ๋ง๋๋ ๊ฐ์ฅ ์ฌ์ด ๋ฐฉ๋ฒ์ ๅ ๋ชจ์์ ํ ํธ๋ก๋ฏธ๋ ธ๋ฅผ ๋ง๋ค๊ณ , ์ํ์ข์ฐ์์ ํ๋์ฉ ๋นผ๋ฉด ๋๋ค. ๅ ๋ชจ์์ ๊ตฌ์ฑํ๋๋ฐ, ์นธ์ด ์กด์ฌํ ์ ์๋ ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ๊ฒฝ์ฐ์๋ ์ ์ธ๋์๋ค๊ณ ์๊ฐํ๋ฉด ๋๊ณ , ์ ์ธ๋ ๋ชจ์์ด ใ ๋ชจ์์ ์ด๋ฃฐ ๋๋ฅผ ๋๋นํ์ฌ ๊ทธ๋๋ก ๊ฐฑ์ ์๋๋ฅผ ํ๊ณ , ใ ๋ชจ์์ ๋ชป ์ด๋ฃจ์๋ค๋ฉด poli์์ ๊ฐฑ์ ํ ์ ์์ผ๋ฏ๋ก ๊ด์ฐฎ๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m, result = 0;
vector<vector<int>> paper(500, vector<int>(500));
vector<vector<bool>> visited(500, vector<bool>(500));
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,1,-1 };
void poli(int x, int y, int count, int sum) {
if (count == 4) {
result = max(result, sum);
return;
}
for (int i = 0; i < 4; i++) {
int new_x = x + dx[i];
int new_y = y + dy[i];
if (!(new_x >= 0 && new_x < n && new_y >= 0 && new_y < m)) {
continue;
}
if (!visited[new_x][new_y]) {
visited[new_x][new_y] = true;
poli(new_x, new_y, count + 1, sum + paper[new_x][new_y]);
visited[new_x][new_y] = false;
}
}
}
void poli2(int x, int y) {
int temp_result = paper[x][y];
for (int i = 0; i < 4; i++) {
int new_x = x + dx[i];
int new_y = y + dy[i];
if (!(new_x >= 0 && new_x < n && new_y >= 0 && new_y < m)) {
continue;
}
temp_result += paper[new_x][new_y];
}
for (int i = 0; i < 4; i++) {
int new_x = x + dx[i];
int new_y = y + dy[i];
if (!(new_x >= 0 && new_x < n && new_y >= 0 && new_y < m)) {
result = max(result, temp_result);
continue;
}
result = max(result, temp_result - paper[new_x][new_y]);
}
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> paper[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
visited[i][j] = true;
poli(i, j, 1, paper[i][j]);
visited[i][j] = false;
poli2(i, j);
}
}
cout << result << "\n";
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 30804๋ฒ: ๊ณผ์ผ ํํ๋ฃจ (0) | 2024.07.30 |
---|---|
[Baekjoon] 17626๋ฒ: Four Squares (0) | 2024.07.29 |
[Baekjoon] 11726๋ฒ: 2xn ํ์ผ๋ง (0) | 2024.07.26 |
[Baekjoon] 11724๋ฒ: ์ฐ๊ฒฐ ์์์ ๊ฐ์ (0) | 2024.07.25 |
[Baekjoon] 11659๋ฒ: ๊ตฌ๊ฐ ํฉ ๊ตฌํ๊ธฐ (0) | 2024.07.25 |