CS/๋ฐฑ์ค
[๋ฐฑ์ค c++] 1937 ์์ฌ์์ด ํ๋ค (๊ณจ3)
๊ฐ์์
2023. 11. 15. 15:35
์ ํ์ ์ธ ๊ตฌํ ํ์ DFS ์์ต๋๋ค.
// ์์ฌ์์ด ํ๋ค - ๊ตฌํ
#include <iostream>
#include <algorithm>
using namespace std;
int N, result;
int board[501][501];
long long score[501][501];
// ์, ํ, ์ข, ์ฐ ์ค ํ ๊ณณ์ผ๋ก ์ด๋
int dx[4] = {0,0,-1,1};
int dy[4] = {1,-1,0,0};
int DFS(int x, int y){
if(score[x][y] == 0){
score[x][y] =1;
int cnt = 0;
for(int i = 0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 0 && nx < N && ny >= 0 && ny < N){
if(board[nx][ny] > board[x][y]){
cnt = max(cnt, DFS(nx,ny));
}
}
}
score[x][y] += cnt;
}
return score[x][y];
}
int main(){
//n × n์ ํฌ๊ธฐ์ ๋๋๋ฌด ์ฒ
//๋๋๋ฌด๋ฅผ ๋จน๊ณ ์๋ฆฌ๋ฅผ ์ฎ๊ธฐ๋ฉด ๊ทธ ์ฎ๊ธด ์ง์ญ์ ๊ทธ ์ ์ง์ญ๋ณด๋ค ๋๋๋ฌด๊ฐ ๋ง์ด ์์ด์ผํจ
// ์ด๋ค ์ง์ ์ ์ฒ์์ ํ์ด ๋์์ผ ํ๊ณ , ์ด๋ค ๊ณณ์ผ๋ก ์ด๋์ ์์ผ์ผ ํ๋ค๊ฐ ์ต๋ํ ๋ง์ ์นธ์ ๋ฐฉ๋ฌธํ ์ ์๋์ง
// ์ด ํ๋ค๊ฐ ์ต๋ํ ๋ง์ ์นธ์ ์ด๋ํ๋ ค๋ฉด ์ด๋ค ๊ฒฝ๋ก๋ฅผ ํตํ์ฌ ์์ง์ฌ์ผ ํ๋์ง
cin >> N;
result = -1;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
cin >> board[i][j];
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
int temp = DFS(i,j);
if(temp > result) result = temp;
}
}
cout << result << endl;
return 0;
}