思路一:暴力求解 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;
}