求助 dfs #3#4 TLE!!!!!
查看原帖
求助 dfs #3#4 TLE!!!!!
882793
666wula666楼主2023/7/29 23:10

求助如何优化,还是说这题dfs肯定要tle吗

#include<bits/stdc++.h>
using namespace std;
#define int long long
int cnt;
int fx,fy;int a,b;
bool st[100][100];

void dfs(int x,int y){

    if(x==fx&&y==fy){cnt++;return;}
    
    if(!st[x+1][y]&&x+1<=fx&&y<=fy)dfs(x+1,y);
    if(!st[x][y+1]&&x<=fx&&y+1<=fy)dfs(x,y+1);
  
}
signed main(){
    cin>>fx>>fy>>a>>b;
    st[a][b]=true;
    st[a+2][b+1]=true;
   if(b-1>=0) st[a+2][b-1]=true;
   if(a-2>=0){
       st[a-2][b+1]=true;
       if(b-1>=0)st[a-2][b-1]=true;
   }
   if(b-2>=0){
       st[a+1][b-2]=true;
       if(a-1>=0)st[a-1][b-2]=true;
   }
   if(a-1>=0)st[a-1][b+2]=true;
   st[a+1][b+2]=true;
    dfs(0,0);
    cout<<cnt;
    
    return 0;
}
2023/7/29 23:10
加载中...