1048

#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 
****************************************************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值