Description
Paul is at the orchestra. The string section is arranged in an r × c
rectangular grid and is filled with violinists with the exception of
nviolists. Paul really likes violas, so he would like to take a
picture including at least k of them. Paul can take a picture of any
axis-parallel rectangle in the orchestra. Count the number of possible
pictures that Paul can take. Two pictures are considered to be
different if the coordinates of corresponding rectangles are
different.
Input
The first line of input contains four space-separated integers r, c,
n, k (1 ≤ r, c, n ≤ 10, 1 ≤ k ≤ n) — the number of rows and columns of
the string section, the total number of violas, and the minimum number
of violas Paul would like in his photograph, respectively. The next n
lines each contain two integers xi and yi (1 ≤ xi ≤ r, 1 ≤ yi ≤ c):
the position of the i-th viola. It is guaranteed that no location
appears more than once in the input.
Output
Print a single integer — the number of photographs Paul can take which
include at least k violas. Sample Input
Input
2 2 1 1
1 2
Output
4
Input
3 2 3 3
1 1
3 1
2 2
Output
1
Input
3 2 3 2
1 1
3 1
2 2
Output
4
先选定起点,再在起点右下选定终点,计算中间有多少个提琴手,若符合条件就加一种情况。
数据不大直接暴力搜索。
#include<iostream>
#include<cstdio>
using namespace std;
int r,c,n,k;
int map[12][12];
int main()
{
int x,y;
while(cin>>r>>c>>n>>k)
{
memset(map,0,sizeof(map));
for(int i=0;i<n;i++)
{
cin>>x>>y;
map[x][y]=1;
}
int k1,g=0,x1,y1,x2,y2;
for(x1=1;x1<=r;x1++)
{
for(y1=1;y1<=c;y1++)
{
for(x2=x1;x2<=r;x2++)
{
for(y2=y1;y2<=c;y2++)
{
k1=0;
for(int b=x1;b<=x2;b++)
for(int e=y1;e<=y2;e++)
k1+=map[b][e];
if(k1>=k)
g++;
}
}
}
}
cout<<g<<endl;
}
return 0;
}