Guest User

Untitled

a guest
Oct 16th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include<iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int n;
  8. int dx[] = { -1,1,0,0 };
  9. int dy[] = { 0,0,-1,1 };
  10. vector<vector<int>> d;//d[i][j]: (i,j)에서 시작하는 최대한 살 수 있는 일수 저장
  11. vector<vector<int>> arr;
  12.  
  13. //d[x][y] 구하고 반환
  14. int dfs(int x, int y)
  15. {
  16. int now = arr[x][y];
  17.  
  18. int &ret = d[x][y];
  19. if (ret != -1) return ret;//이미 탐색한 곳이면 d[i][j] 반환
  20. ret = 1;//1 = (x,y) 먹는 것이 현재 최대로 살 수 있는 일수)
  21.  
  22. //상하좌우
  23. for (int i = 0; i < 4; i++)
  24. {
  25. int nx = x + dx[i];
  26. int ny = y + dy[i];
  27.  
  28. if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue; //배열 범위 벗어나는 경우
  29. if (arr[nx][ny] <= now) continue;//현재 칸 보다 먹이 수가 적거나 같은 경우
  30.  
  31. ret = max(ret, dfs(nx, ny) + 1);
  32. }
  33. return ret;
  34. }
  35.  
  36. int main()
  37. {
  38. cin >> n;
  39. arr.resize(n, vector<int>(n));
  40. d.resize(n, vector<int>(n, -1));
  41.  
  42. for (int i = 0; i < n; i++)
  43. for (int j = 0; j < n; j++)
  44. scanf("%d", &arr[i][j]);
  45.  
  46. int ans = 0;
  47. for (int i = 0; i < n; i++)
  48. for (int j = 0; j < n; j++)
  49. ans = max(ans, dfs(i, j));
  50.  
  51. cout << ans;
  52. return 0;
  53. }
Add Comment
Please, Sign In to add comment