View Code of Problem 17

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
int n,m;
int mapp[100][100];
int book[100][100];
int maxx=0;
int vist[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
void dfs(int x,int y,int bu){
        if(x<0||x>=n||y<0||y>=m)
            return;
        if(bu>maxx)
            maxx=bu;
        for(int i=0;i<4;i++)
        {
            int tx=x+vist[i][0];
            int ty=y+vist[i][1];
            if(tx<0||ty<0||tx>=n||ty>=m||book[tx][ty]||mapp[tx][ty]<mapp[x][y])
                continue;
            else {
                book[tx][ty]=1;
                dfs(tx,ty,bu+1);
                book[tx][ty]=0;
            }
        }
}
int main()
{
    while(cin>>n>>m)
    {
        maxx=0;
        memset(book,0,sizeof(book));
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            cin>>mapp[i][j];
            book[0][0]=1;
        dfs(0,0,0);
        cout<<maxx<<endl;
    }
   return 0;
}

Double click to view unformatted code.


Back to problem 17