1048

来源:互联网 发布:windows phone 微信 编辑:程序博客网 时间:2024/06/07 17:04
#include<stdio.h> #include<iostream> #define DEBUG_IO (0) #define DEBUG (0) using namespace std; const int N = 52;   struct Node {     int val;     int step; };   int n; int step; int temp; Node map[N][N]; bool vis[N][N];   void dfs(int x, int y, int prev); int main() { #if DEBUG      freopen("input.txt", "r", stdin);      setbuf(stdout, NULL);  #endif     int i, j, k;     scanf("%d", &n);     for(i = 0; i < n; i++)     {         for(j = 0; j < n; j++)         {             scanf("%d", &map[i][j].val);             vis[i][j] = 0;             map[i][j].step = -1;         }     }       for(i = 0; i < n; i++)     {         for(j = 0; j < n; j++)         {             if(map[i][j].step != -1)             {                 continue;             }             step = 0;             temp = 0;               vis[i][j] = 1; #if DEBUG_IO             cout<<"("<<i<<","<<j<<","<<step<<")"; #endif             dfs(i+1, j, map[i][j].val);             dfs(i-1, j, map[i][j].val);             dfs(i, j+1, map[i][j].val);             dfs(i, j-1, map[i][j].val); #if DEBUG_IO             cout<<"step = "<<step<<endl; #endif             map[i][j].step = step;             vis[i][j] = 0;         }     }     for(i = 0; i < n; i++)     {         for(j = 0; j < n; j++)         {             if(step < map[i][j].step)             {                 step = map[i][j].step;             }         }     }     printf("%d", step+1);     return 0; } void dfs(int x, int y, int prev) {     if(x >= 0 && x < n && y >= 0 && y < n && vis[x][y] == 0)     {         vis[x][y] = 1;         if(prev > map[x][y].val)         {             temp++;             if(map[x][y].step != -1)             {                 if(step < temp + map[x][y].step)                 {                     step = temp + map[x][y].step;                 }                 //RETURN 之前记得重置                 vis[x][y] = 0;                 temp--; #if DEBUG_IO                 cout<<"("<<x<<","<<y<<","<<step<<")"; #endif                 return;             }             if(step < temp)             {                 step = temp;             } #if DEBUG_IO             cout<<"("<<x<<","<<y<<","<<step<<")"; #endif             dfs(x+1, y, map[x][y].val);             dfs(x-1, y, map[x][y].val);             dfs(x, y-1, map[x][y].val);             dfs(x, y+1, map[x][y].val);             temp--;         }         vis[x][y] = 0;     } } /**************************************************************     Problem: 1048     User: xxxx     Language: C++     Result: 正确     Time:10 ms     Memory:484 kb ****************************************************************/

0 0