Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 79919 | Accepted: 29747 |
Descriptionios
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Input數組
Outputide
Sample Inputspa
5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
Sample Outputxml
25
題解:跑一發DFS,記憶化搜索一下就好
#include<iostream> using namespace std; int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}}; int n,m; int map[102][102]; int dp[102][102]; int dfs(int x,int y) { //cout <<x << " " << y << endl; int tx,ty,i; if(dp[x][y]) //當已經搜索過期就返回 return dp[x][y]; for(i = 0;i < 4;i++) { tx = x + dir[i][0] , ty = y + dir[i][1]; if(tx >= 0 && tx < n && ty >= 0 && ty < m && map[tx][ty] < map[x][y])//搜索+ 狀態轉移方程 { int temp = dfs(tx,ty); //遞歸的出口有兩個1.在11行;2.在16行 if(dp[x][y] <= temp) //i=0時比較dfs(x+1, y);i=1時比較dfs(x, y+1);i=2時比較dfs(x-1, y);i=3時比較dfs(x, y-1) { dp[x][y] = temp+1; } } } return dp[x][y]; } int main() { int i,j,t; while(cin >> n >> m) { for(i =0 ;i < n;i++) { for(j = 0;j < m;j++) { cin >> map[i][j]; dp[i][j] = 0; } } int max = -1; for(i = 0 ;i < n;i++) { for(j = 0;j < m;j++) { t = dfs(i,j); if(max < t) { max = t; } } } cout <<max + 1 << endl; } return 0; }