56分

P1228 地毯填补问题

qwertyuuiigfvcgv @ 2024-01-25 17:08:22

递归找不出问题 WA #2 #4 #7 code:

#include<iostream>
#include<math.h>
using namespace std;
bool map[2048][2048];
void place(int x,int y,int c){
    map[x][y] = true;
    switch (c)
    {
        case 1:
            map[x-1][y] = true;
            map[x][y-1] = true;
            break;
        case 2:
            map[x+1][y] = true;
            map[x][y-1] = true;
            break;
        case 3:
            map[x-1][y] = true;
            map[x][y+1] = true;
            break;
        case 4:
            map[x+1][y] = true;
            map[x][y+1] = true;
            break;
        default:
            break;
    }
}
void cover(int size,int x,int y){
    if(size<=2){
        if(map[x][y]){
            place(x+1,y+1,1);
            cout<<y+1<<" "<<x+1<<" "<<1<<endl;
            return;
        }
        if(map[x+1][y]){
            place(x,y+1,2);
            cout<<y+1<<" "<<x<<" "<<2<<endl;
            return;
        }
        if(map[x][y+1]){
            place(x+1,y,3);
            cout<<y<<" "<<x+1<<" "<<3<<endl;
            return;
        }
        if(map[x+1][y+1]){
            place(x,y,4);
            cout<<y<<" "<<x<<" "<<4<<endl;
            return;
        }
    }
    bool zs = false,ys = false,zx = false,yx = false;
    for(int i = x;i<x+size/2;i++){
        for(int j = y;j<y+size/2;j++){
            if(map[i][j]){
                zs = true;
                break;
            }
        }
    }
    for(int i = x+size/2;i<x+size;i++){
        for(int j = y;j<y+size/2;j++){
            if(map[i][j]){
                ys = true;
                break;
            }
        }
    }
    for(int i = x;i<x+size/2;i++){
        for(int j = y+size/2;j<y+size;j++){
            if(map[i][j]){
                zx = true;
                break;
            }
        }
    }
    for(int i = x+size/2;i<x+size;i++){
        for(int j = y+size/2;j<y+size;j++){
            if(map[i][j]){
                yx = true;
                break;
            }
        }
    }
    if(zs){
        place(x+size/2,y+size/2,1);
        cout<<y+size/2<<" "<<x+size/2<<" "<<1<<endl;
    }
    if(ys){
        place(x+size/2-1,y+size/2,2);
        cout<<y+size/2<<" "<<x+size/2-1<<" "<<2<<endl;
    }
    if(zx){
        place(x+size/2,y+size/2-1,3);
        cout<<y+size/2-1<<" "<<x+size/2<<" "<<3<<endl;
    }
    if(yx){
        place(x+size/2-1,y+size/2-1,4);
        cout<<y+size/2-1<<" "<<x+size/2-1<<" "<<4<<endl;
    }
    cover(size/2,x,y);
    cover(size/2,x+size/2,y);
    cover(size/2,x,y+size/2);
    cover(size/2,x+size/2,y+size/2);
}
int main(){
    int x,y,s;
    cin>>s>>x>>y;
    map[x][y] = true;
    cover(pow(2,s),1,1);
}

请问问题出在哪


by youwhenway_second @ 2024-08-07 09:10:50

#include<iostream>
#include<math.h>
using namespace std;
bool map[2048][2048];
void place(int x,int y,int c){
    map[x][y] = true;
    switch (c)
    {
        case 1:
            map[x-1][y] = true;
            map[x][y-1] = true;
            break;
        case 2:
            map[x+1][y] = true;
            map[x][y-1] = true;
            break;
        case 3:
            map[x-1][y] = true;
            map[x][y+1] = true;
            break;
        case 4:
            map[x+1][y] = true;
            map[x][y+1] = true;
            break;
        default:
            break;
    }
}
void cover(int size,int x,int y){
    if(size<=2){
        if(map[x][y]){
            place(x+1,y+1,1);
            cout<<y+1<<" "<<x+1<<" "<<1<<endl;
            return;
        }
        if(map[x+1][y]){
            place(x,y+1,2);
            cout<<y+1<<" "<<x<<" "<<2<<endl;
            return;
        }
        if(map[x][y+1]){
            place(x+1,y,3);
            cout<<y<<" "<<x+1<<" "<<3<<endl;
            return;
        }
        if(map[x+1][y+1]){
            place(x,y,4);
            cout<<y<<" "<<x<<" "<<4<<endl;
            return;
        }
    }
    bool zs = false,ys = false,zx = false,yx = false;
    for(int i = x;i<x+size/2;i++){
        for(int j = y;j<y+size/2;j++){
            if(map[i][j]){
                zs = true;
                break;
            }
        }
    }
    for(int i = x+size/2;i<x+size;i++){
        for(int j = y;j<y+size/2;j++){
            if(map[i][j]){
                ys = true;
                break;
            }
        }
    }
    for(int i = x;i<x+size/2;i++){
        for(int j = y+size/2;j<y+size;j++){
            if(map[i][j]){
                zx = true;
                break;
            }
        }
    }
    for(int i = x+size/2;i<x+size;i++){
        for(int j = y+size/2;j<y+size;j++){
            if(map[i][j]){
                yx = true;
                break;
            }
        }
    }
    if(zs){
        place(x+size/2,y+size/2,1);
        cout<<y+size/2<<" "<<x+size/2<<" "<<1<<endl;
    }
    if(ys){
        place(x+size/2-1,y+size/2,2);
        cout<<y+size/2<<" "<<x+size/2-1<<" "<<2<<endl;
    }
    if(zx){
        place(x+size/2,y+size/2-1,3);
        cout<<y+size/2-1<<" "<<x+size/2<<" "<<3<<endl;
    }
    if(yx){
        place(x+size/2-1,y+size/2-1,4);
        cout<<y+size/2-1<<" "<<x+size/2-1<<" "<<4<<endl;
    }
    cover(size/2,x,y);
    cover(size/2,x+size/2,y);
    cover(size/2,x,y+size/2);
    cover(size/2,x+size/2,y+size/2);
}
int main(){
    int x,y,s;
    cin>>s>>y>>x;//here
    map[x][y] = true;
    cover(pow(2,s),1,1);
}

by qwertyuuiigfvcgv @ 2024-09-21 08:51:50

@youwhenway_second 谢谢啊大佬


|