Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int n;
- int dx[] = { -1,1,0,0 };
- int dy[] = { 0,0,-1,1 };
- vector<vector<int>> d;//d[i][j]: (i,j)에서 시작하는 최대한 살 수 있는 일수 저장
- vector<vector<int>> arr;
- //d[x][y] 구하고 반환
- int dfs(int x, int y)
- {
- int now = arr[x][y];
- int &ret = d[x][y];
- if (ret != -1) return ret;//이미 탐색한 곳이면 d[i][j] 반환
- ret = 1;//1 = (x,y) 먹는 것이 현재 최대로 살 수 있는 일수)
- //상하좌우
- 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) continue; //배열 범위 벗어나는 경우
- if (arr[nx][ny] <= now) continue;//현재 칸 보다 먹이 수가 적거나 같은 경우
- ret = max(ret, dfs(nx, ny) + 1);
- }
- return ret;
- }
- int main()
- {
- cin >> n;
- arr.resize(n, vector<int>(n));
- d.resize(n, vector<int>(n, -1));
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- scanf("%d", &arr[i][j]);
- int ans = 0;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- ans = max(ans, dfs(i, j));
- cout << ans;
- return 0;
- }
Add Comment
Please, Sign In to add comment