蒟蒻求助!!TLE#2!
查看原帖
蒟蒻求助!!TLE#2!
668599
BearBig楼主2022/6/21 20:34

闰土

#include<bits/stdc++.h>
using namespace std;
int mp[105][105];int n,m;
bool vis[105][105];int tot(-2147483647);
bool inrange(int x,int y)
{return 1<=x and x<=n and 1<=y and y<=m;}
void dfs(int i,int j,int presum)
{
    tot=max(tot,presum);
    if(inrange(i-1,j) and mp[i-1][j]<mp[i][j] and vis[i-1][j]==false)
        {vis[i-1][j]=true;dfs(i-1,j,presum+1);vis[i-1][j]=false;}
    if(inrange(i,j+1) and mp[i][j+1]<mp[i][j] and vis[i][j+1]==false)
        {vis[i][j+1]=true;dfs(i,j+1,presum+1);vis[i][j+1]=false;}
    if(inrange(i+1,j) and mp[i+1][j]<mp[i][j] and vis[i+1][j]==false)
        {vis[i+1][j]=true;dfs(i+1,j,presum+1);vis[i+1][j]=false;}
    if(inrange(i,j-1) and mp[i][j-1]<mp[i][j] and vis[i][j-1]==false)
        {vis[i][j-1]=true;dfs(i,j-1,presum+1);vis[i][j-1]=false;}
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            cin>>mp[i][j];
    for(int i=1;i<=n;++i)
        for(int j=1;j<=m;++j)
            dfs(i,j,1);
    cout<<tot;
}

有哪位巨佬帮忙看一看?

2022/6/21 20:34
加载中...