3 条题解

  • 1
    @ 2024-12-17 13:33:52
    #include <iostream>
    #define int long long
    using namespace std;
    const short dir[8][2] = { {1 , 2} , {1 , -2} , {2 , 1} , {2 , -1} , {-1 , 2} , {-1 , -2} , {-2 , 1} , {-2 , -1} };
    bool d[30][30];
    int dp[30][30] , n , m , x , y;
    signed main(){
        cin >> n >> m >> x >> y;
        d[x][y] = 1;
        for(int i = 0 ; i < 8 ; i++){
            int tx = x + dir[i][0],ty = y + dir[i][1];
            if(tx >=  0  &&  tx <= n  &&  ty >=  0  &&  ty <= m) 
    			d[tx][ty] = 1;
        }
        dp[0][0] = 1;
        for(int i = 0 ; i <= n ; i++)
            for(int j = 0;j <= m;j++)
                if(!d[i][j]){
                    if(i) 
    					dp[i][j] +=  dp[i-1][j];
                    if(j) 
    					dp[i][j] +=  dp[i][j-1];
                }
        cout << dp[n][m] << endl;
        return 0;
    }
    
    • 0
      @ 2025-4-12 10:52:04
      #include<iostream>
      using namespace std;
      const int N=20+10;
      int n,m,x,y;
      bool v[N][N];
      long long a[N][N];
      int dx[]={0,-1,-2,-2,-1,1,2,2,1};
      int dy[]={0,-2,-1,1,2,2,1,-1,-2};
      void init(){
      	for(int i=0;i<=8;i++){
      		int xx=x+dx[i];
      		int yy=y+dy[i];
      		if(xx>=0&&xx<=n&&yy>=0&&yy<=n){
      			v[xx][yy]=1;
      		}
      	}
      	for(int i=0;i<=m;i++){
      		if(v[0][i]==0){
      			a[0][i]=1;
      		}else{
      			break;
      		}
      	}
      	for(int i=0;i<=n;i++){
      		if(v[i][0]==0){
      			a[i][0]=1;
      		}else{
      			break;
      		}
      	}
      }
      int main(){
      	cin>>n>>m>>x>>y;
      	init();
      	for(int i=1;i<=n;i++){
      		for(int j=1;j<=m;j++){
      			if(v[i][j]==0){
      				a[i][j]=a[i-1][j]+a[i][j-1];
      			}
      		}
      	}
      	cout<<a[n][m];
      }
      
      
      
      
      
      
      
      
      • -1
        @ 2023-4-4 22:31:54

        一道比较入门的dp题. 答案可能很大,所以记得开 long long.

        #include<iostream>
        #include<cstring>
        #include<cstdio>
        #include<algorithm>
        using namespace std;
        const int fx[9] = {0, -2, -1, 1, 2, 2, 1, -1, -2};
        const int fy[9] = {0, 1, 2, 2, 1, -1, -2, -2, -1};
        int bx, by, mx, my;
        long long f[40][40];
        bool s[40][40];
        int main()
        {
            scanf("%d%d%d%d",&bx,&by,&mx,&my);
            bx=bx+2; 
            by=by+2; 
            mx=mx+2;
            my=my+2;
            f[2][1]=1;
            s[mx][my]=1;
            for(int i=1;i<=8;i=i+1)s[mx+fx[i]][my+fy[i]]=1;
            for(int i=2;i<=bx;i=i+1)
            {
                for(int j=2;j<=by;j=j+1)
                {
                    if(s[i][j]) continue;
                    f[i][j]=f[i-1][j]+f[i][j-1];
                }
            }
            printf("%lld\n", f[bx][by]);
            return 0;
        } 
        
      • 1

      信息

      ID
      659
      时间
      1000ms
      内存
      128MiB
      难度
      2
      标签
      递交数
      91
      已通过
      54
      上传者