202206-2 寻宝大冒险

思路一:暴力求解 70分

#include<iostream>
using namespace std;
bool check(bool **,bool**,int,int,int);
int main(){
    int n,L,S;
    cin>>n>>L>>S;
    bool ** A = new bool*[L+1];
    for(int i=0;i<=L;i++){
        A[i] = new bool[L+1]{0};
    }
    bool **B = new bool*[S+1];
    for(int i=0;i<=S;i++){
        B[i] = new bool[S+1]{0};
    }
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        A[x][y] = true;
    }
    for(int i=S;i>=0;i--){
        for(int j=0;j<=S;j++){
            cin>>B[i][j];
        }
    }
    //输入完成
    int count = 0;
    for(int i=0;i<=L;i++){
        if(L-i+1<=S) break;
        for(int j=0;j<=L;j++){
            if(L-j+1<= S) continue;
            if(check(A,B,i,j,S)) count++;
        }
    } 
    cout<<count<<endl;
    
    return 0;
}
bool check(bool **A,bool **B,int x,int y,int S){
    for(int i=0;i<=S;i++){
        for(int j=0;j<=S;j++){
            if(A[x+i][y+j] != B[i][j]) return false;
        }
    }
    return true;
}

思路二:利用条件4,时间复杂度O(n*S^2)

#include<iostream>
using namespace std;
bool check(bool **,bool**,int,int,int);
int main(){
    int n,S;
    long long L;
    cin>>n>>L>>S;
    bool ** A = new bool*[L+1];
    for(int i=0;i<=L;i++){
        A[i] = new bool[L+1]{0};
    }
    bool **B = new bool*[S+1];
    for(int i=0;i<=S;i++){
        B[i] = new bool[S+1]{0};
    }
    int ** note = new int*[n]; 
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        A[x][y] = true;
        note[i] = new int[2];
        note[i][0] = x;
        note[i][1] = y;
    }
    for(int i=S;i>=0;i--){
        for(int j=0;j<=S;j++){
            cin>>B[i][j];
        }
    }
    //输入完成
    
    // 运行错误 70 分,没说时间的问题,可能是L太大了,占内存好几个G 
    int count = 0;
    for(int i=0;i<n;i++){
        if(note[i][0]+S>L||note[i][1]+S>L) continue;
        if(check(A,B,note[i][0],note[i][1],S)) count++;
    }
    cout<<count<<endl;
    
    return 0;
}
bool check(bool **A,bool **B,int x,int y,int S){
    for(int i=0;i<=S;i++){
        for(int j=0;j<=S;j++){
            if(A[x+i][y+j] != B[i][j]) return false;
        }
    }
    return true;
}

思路三:改进空间复杂度,不使用表,直接使用坐标判断

#include<iostream>
using namespace std;
bool check(int **,bool**,int,int,int,int);
int main(){
    int n,S;
    long long L;
    cin>>n>>L>>S;
//    bool ** A = new bool*[L+1];
//    for(int i=0;i<=L;i++){
//        A[i] = new bool[L+1]{0};
//    }
    bool **B = new bool*[S+1];
    for(int i=0;i<=S;i++){
        B[i] = new bool[S+1]{0};
    }
    int ** note = new int*[n]; 
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
//        A[x][y] = true;
        note[i] = new int[2];
        note[i][0] = x;
        note[i][1] = y;
    }
    for(int i=S;i>=0;i--){
        for(int j=0;j<=S;j++){
            cin>>B[i][j];
        }
    }
    //输入完成
    
    // 运行错误 70 分,没说时间的问题,可能是L太大了,占内存好几个G 
    /*
    “改进”基础上改进 
    漂亮 100分! 
    */
    int count = 0;
    for(int i=0;i<n;i++){
        if(note[i][0]+S>L||note[i][1]+S>L) continue;
        if(check(note,B,note[i][0],note[i][1],S,n)) count++;
    }
    cout<<count<<endl;
    
    return 0;
}
bool exist(int **note,int n,int x,int y){
    for(int i=0;i<n;i++){
        if(note[i][0] == x && note[i][1] == y){
//            cout<<x<<" "<<y<<endl;
            return true;    
        }
    } 
    return false;
}
bool check(int **note,bool **B,int x,int y,int S,int n){
//    cout<<"x:"<<x<<",y:"<<y<<endl;
    for(int i=0;i<=S;i++){
        for(int j=0;j<=S;j++){
            if(B[i][j]^exist(note,n,x+i,y+j)){
//                cout<<B[i][j]<<"!="<<exist(note,n,i,j)<<endl;
                return false;
            }
        }
    }
    return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值