알고리즘 풀이
백준 1937번 욕심쟁이 판다
dokrsky
2017. 12. 11. 23:23
https://www.acmicpc.net/problem/1937
#include <iostream>#include <cmath>using namespace std;int dx[4] = {0, -1, 0, 1};int dy[4] = {-1, 0, 1, 0};int dp[501][501] = {0, };int tree[501][501] = {0, };int n;bool isInner(int i, int j) {if (i >= 0 && i < n && j >= 0 && j < n) {return true;}return false;}int process (int x, int y) {if (dp[x][y] == 1) {for (int idx = 0; idx < 4; ++idx) {int newX = x + dx[idx];int newY = y + dy[idx];if (isInner(newX, newY)) {if (tree[x][y] < tree[newX][newY]) {dp[x][y] = max(dp[x][y], process(newX, newY) + 1);}}}}return dp[x][y];}int main(){int result = -1;cin >> n;for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {cin >> tree[i][j];dp[i][j] = 1;}}for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {result = max(result, process(i, j));}}cout << result << endl;}