C++:
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1010;
struct Point{
int x, y;
}p[N];
int main(){
int n, k, t, xl, xr, yl, yh;
scanf("%d%d%d%d%d%d%d", &n, &k, &t, &xl, &yl, &xr, &yh);
int res1 = 0, res2 = 0;
while(n--){
int cnt = 0, cnt1 = 0;
for(int i = 1; i <= t; i++){
//cout << "cnt1:" << cnt1 << endl;
scanf("%d%d", &p[i].x, &p[i].y);
}
for(int i = 1; i <= t; i++){
if(p[i].x >= xl && p[i].x <= xr && p[i].y >= yl && p[i].y <= yh){
cnt++;
cnt1++;
if(cnt1 >= k){
res2++;
break;
}
}else{
cnt1 = 0;
}
}
if(cnt) res1++;
}
cout << res1 << endl;
cout << res2 << endl;
}
错误代码:
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1010;
struct Point{
int x, y;
bool flag = false;
}p[N];
int main(){
int n, k, t, xl, xr, yl, yh;
scanf("%d%d%d%d%d%d%d", &n, &k, &t, &xl, &yl, &xr, &yh);
int res1 = 0, res2 = 0;
while(n--){
int cnt = 0, cnt1 = 0;
for(int i = 1; i <= t; i++){
//cout << "cnt1:" << cnt1 << endl;
scanf("%d%d", &p[i].x, &p[i].y);
}
for(int i = 1; i <= t; i++){
if(p[i].x >= xl && p[i].x <= xr && p[i].y >= yl && p[i].y <= yh){
p[i].flag = true;
cnt++;
if(p[i - 1].flag) cnt1++;
else cnt1 = 1;
if(cnt1 >= k){
res2++;
break;
}
}
}
if(cnt) res1++;
}
cout << res1 << endl;
cout << res2 << endl;
}
错误原因:用flag记录是否经过风险区,但是没有把没经过风险区的时刻设为false,这样当遍历到下一个人时,可能用的是前一个人前一时刻的flag。
改正代码:
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1010;
struct Point{
int x, y;
bool flag = false;
}p[N];
int main(){
int n, k, t, xl, xr, yl, yh;
scanf("%d%d%d%d%d%d%d", &n, &k, &t, &xl, &yl, &xr, &yh);
int res1 = 0, res2 = 0;
while(n--){
int cnt = 0, cnt1 = 0;
for(int i = 1; i <= t; i++){
//cout << "cnt1:" << cnt1 << endl;
scanf("%d%d", &p[i].x, &p[i].y);
}
for(int i = 1; i <= t; i++){
if(p[i].x >= xl && p[i].x <= xr && p[i].y >= yl && p[i].y <= yh){
p[i].flag = true;
cnt++;
if(p[i - 1].flag) cnt1++;
else cnt1 = 1;
if(cnt1 >= k){
res2++;
break;
}
}else{
p[i].flag = false;
}
}
if(cnt) res1++;
}
cout << res1 << endl;
cout << res2 << endl;
}