递推0pts(不用暴搜了)悬棺求调!
查看原帖
递推0pts(不用暴搜了)悬棺求调!
1323415
wky_wsy楼主2024/11/22 20:00
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;
int cnt=0;
int n,m,xb,yb;
int a[30][30];
/*inline void dfs(int x,int y){
    if(x==n&&y==m) 
    {
        ++cnt;
        return;
    }
    if(x+1<=n&&a[x+1][y]==0)
        dfs(x+1,y);
    if(y+1<=m&&a[x][y+1]==0)
        dfs(x,y+1);
}*/
signed main(){
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>xb>>yb; a[xb][yb]=1;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=m;j++) a[i][j]=1;
    }
    if(xb-2>=0&&yb-1>=0) a[xb-2][yb-1]=0;
    if(xb-2>=0&&yb+1<=m) a[xb-2][yb+1]=0;
    if(yb+2<=m&&xb-1>=0) a[xb-1][yb+2]=0;
    if(yb+2<=m&&xb+1<=n) a[xb+1][yb+2]=0;
    if(xb+2<=n&&yb+1<=m) a[xb+2][yb+1]=0;
    if(xb+2<=n&&yb-1>=0) a[xb+2][yb-1]=0;
    if(yb-2>=0&&xb-1>=0) a[xb-1][yb-2]=0;
    if(yb-2>=0&&xb+1<=n) a[xb+1][yb-2]=0;
    //dfs(0,0);
    a[0][0]=1;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=m;j++){
            if(a[i][j]==0) continue;
            if(i!=0&&j!=0){
                if(a[i-1][j]==0||a[i][j-1]==0) a[i][j]=0;
                else a[i][j]=a[i-1][j]+a[i][j-1];
            }
            else if(i!=0){
                if(a[i-1][j]==0) a[i][j]=0;
                else{
                    a[i][j]+=a[i-1][j];
                }
                
            }
            else if(j!=0){
                if(a[i][j-1]==0) a[i][j-1]=0;
                else a[i][j]+=a[i][j-1];
            }
        }
    }
    cout<<a[n][m];
}
2024/11/22 20:00
加载中...