这一题用的三维数组hash表,但这一题坑的是一个窗口点击之后,它就成了第一层,原来的第1成了第2,依次类推,所以我是用一个临时temp数组去存每一位的层数,保证从N-1的层号,按第一层、第二层依次放置。
一开始hhash[temp[linshi]][x][y] == true 这个判断条件写成了hhash[linshi][x][y] == true,这是错的,因为最后temp数组里存放的才是层号,linshi不过是个递减的变量罢了,导致前几次提交只有40分,真坑啊= =(考试估计发现不了这个错误,因为测试样例也是对的。。。)
AC代码:
#include <stdio.h>
#include <queue>
#include <algorithm>
using namespace std;
bool hhash[11][3000][3000];
int main(){
for(int i = 1;i <=10;i++){
for(int j = 0;j <=2600;j++){
for(int p = 0; p<=1500;p++){
hhash[i][j][p] = false;
}
}
}
int N,M;
int ans[15];
int x1,y1,x2,y2,x,y;
scanf("%d %d",&N,&M);
for(int i = 0;i < M;i++){
//初始化结果数组
ans[i] = -1;
}
for(int i = 1;i <=N;i++){
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
for(int j = x1;j <= x2;j++){
for(int p = y1;p <= y2;p++){
hhash[i][j][p] = true;
}
}
}
int temp[12];
for(int i = 1;i <=N;i++){
//一个临时的数组
temp[i] = i;
}
for(int a = 0;a <M;a++){
scanf("%d %d",&x,&y);
int linshi = N;
int signal = 1;
while(signal>0 && linshi>= 1){
if(hhash[temp[linshi]][x][y] == true){
//是temp[linshi]中存放的层数使它成功,而不是hhash[linshi][x][y],注意!!
ans[a] = temp[linshi];
for(int s = linshi;s <N;s++){
//点击之后这一层就变为第一层,要用数组换序
temp[s] = temp[s+1];
}
temp[N] = ans[a];
signal = -1; //找到一个不再继续找
}
else{
linshi--;
}
}
}
for(int i = 0;i <M;i++){
if(ans[i] == -1){
printf("IGNORED\n");
}
else{
printf("%d\n",ans[i]);
}
}
return 0;
}