C++代码28分,求助大佬们

P1228 地毯填补问题

崔化博 @ 2021-08-27 10:51:10


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

void dfs(int n,int x,int y,int px,int py) {
    if(n==1)
        return ;
    px-=x-1;
    py-=y-1;
    //cout<<n<<' '<<x<<' '<<y<<' '<<px<<' '<<py<<endl;
    if(px<=n/2&&py<=n/2) { //左上
        cout<<x+n/2<<' '<<y+n/2<<' '<<1<<endl;
        dfs(n/2,x,y,px,py);
        dfs(n/2,x,y+n/2,x+n/2-1,y+n/2);
        dfs(n/2,x+n/2,y,x+n/2,y+n/2-1);
        dfs(n/2,x+n/2,y+n/2,x+n/2,y+n/2);
    } else {
        if(px<=n/2&&py>n/2) { //右上
            cout<<x+n/2<<' '<<y+n/2-1<<' '<<2<<endl;
            dfs(n/2,x,y+n/2,px,py);
            dfs(n/2,x,y,x+n/2-1,y+n/2-1);
            dfs(n/2,x+n/2,y,x+n/2,y+n/2-1);
            dfs(n/2,x+n/2,y+n/2,x+n/2,y+n/2);
        } else {
            if(px>n/2&&py<=n/2) { //左下
                cout<<x+n/2-1<<' '<<y+n/2<<' '<<3<<endl;
                dfs(n/2,x+n/2,y,px,py);
                dfs(n/2,x,y,x+n/2-1,y+n/2-1);
                dfs(n/2,x,y+n/2,x+n/2-1,y+n/2);
                dfs(n/2,x+n/2,y+n/2,x+n/2,y+n/2);
            } else { //右下
                cout<<x+n/2-1<<' '<<y+n/2-1<<' '<<4<<endl;
                dfs(n/2,x+n/2,y+n/2,px,py);
                dfs(n/2,x,y,x+n/2-1,y+n/2-1);
                dfs(n/2,x+n/2,y,x+n/2,y+n/2-1);
                dfs(n/2,x,y+n/2,x+n/2-1,y+n/2);
            }
        }
    }
}
int main() {
    int k,x,y;
    cin>>k;
    k=(1<<k);
    cin>>x>>y;
    dfs(k,1,1,x,y);
    return 0;
}

by qq2931451523 @ 2021-12-07 21:28:01

解决了吗, 我的也是只有28分


by legend3366 @ 2022-01-18 21:59:03

@qq2931451523 我也28


by 25219chx @ 2022-05-23 19:25:51

稍微改一下就行了

#include <bits/stdc++.h>
using namespace std;
void dfs(int x1, int y1, int x2, int y2, int gz_x, int gz_y) {
    if (x2 - x1 == 1 && y2 - y1 == 1) {
        if (gz_x == x1 && gz_y == y1) printf("%d %d 1\n", x2, y2); 
        if (gz_x == x1 && gz_y == y2) printf("%d %d 2\n", x2, y1); 
        if (gz_x == x2 && gz_y == y1) printf("%d %d 3\n", x1, y2); 
        if (gz_x == x2 && gz_y == y2) printf("%d %d 4\n", x1, y1); 
        return;
    }
    int mid_x = (x2 - x1) / 2 + x1;
    int mid_y = (y2 - y1) / 2 + y1;
    if (gz_x <= mid_x && gz_y <= mid_y) {                               
        dfs(x1, y1, mid_x, mid_y, gz_x, gz_y);                          

        printf("%d %d 1\n", mid_x + 1, mid_y + 1);              
        dfs(mid_x + 1, y1, x2, mid_y, mid_x + 1, mid_y);                
        dfs(mid_x + 1, mid_y + 1, x2, y2, mid_x + 1, mid_y + 1);
        dfs(x1, mid_y + 1, mid_x, y2, mid_x, mid_y + 1);                
    }
    if (gz_x <= mid_x && gz_y > mid_y) { 
        dfs(x1, mid_y + 1, mid_x, y2, gz_x, gz_y);                   
        printf("%d %d 2\n", mid_x + 1, mid_y);                    
        dfs(x1, y1, mid_x, mid_y, mid_x, mid_y);                                 
        dfs(mid_x + 1, y1, x2, mid_y, mid_x + 1, mid_y);                
        dfs(mid_x + 1, mid_y + 1, x2, y2, mid_x + 1, mid_y + 1);
    }
    if (gz_x > mid_x && gz_y <= mid_y) { 
        dfs(mid_x + 1, y1, x2, mid_y, gz_x, gz_y);                
        printf("%d %d 3\n", mid_x, mid_y + 1);                       
        dfs(mid_x + 1, mid_y + 1, x2, y2, mid_x + 1, mid_y + 1);    
        dfs(x1, y1, mid_x, mid_y, mid_x, mid_y);                                  
        dfs(x1, mid_y + 1, mid_x, y2, mid_x, mid_y + 1);                    
    }
    if (gz_x > mid_x && gz_y > mid_y) { 
        dfs(mid_x + 1, mid_y + 1, x2, y2, gz_x, gz_y);
        printf("%d %d 4\n", mid_x, mid_y);                      
        dfs(x1, y1, mid_x, mid_y, mid_x, mid_y);                               
        dfs(x1, mid_y + 1, mid_x, y2, mid_x, mid_y + 1);              
        dfs(mid_x + 1, y1, x2, mid_y, mid_x + 1, mid_y);              
    }
}

int main() {
    int x, y; 
    int k;    
    cin >> k >> x >> y;
    int n = 1 << k; 
    dfs(1, 1, n, n, x, y);
    return 0;
}

|