City Road

Problem Description

Long long ago, city Old was all around the water and was divided into M*N small square houses. The city Old had only two bridges, in the southwest corner and northeast corner. It’s obvious that the citizens can have C(m+n, n) different ways with shortest length, to go from one bridge to the other. As the city developed, bigger buildings came out and blocked some streets. These new buildings were always large rectangles combining many small houses. Since the new buildings may affect the number of shortest ways a lot, it’s you job now to recalculate the number. The city has M rows and N columns of houses, M+1 horizontal streets, and N+1 vertical streets. Suppose the house in the southwest corner is (1, 1), then the house in the northeast corner can be marked as (m, n). And then each new building can be described by its southwest corner(house) and its size(number of houses) in horizontal and vertical directions.

Input

This problem has multiple test cases. In each test case, the first line is two integer m, n(m*n<=1,000,000)which indicate the size of city Old, in vertical and horizontal directions respectively. And the second line has one integer B(B<=1000) what is the number of the new buildings. After that there are B lines. Each line has four integers x, y, a, b, describing the southwest corner (x, y) , the vertical size a, and the horizontal size b of the building. The buildings may be overlapped with each other. The input is terminated by a case with m*n=0, which should not be processed.

Output

For each test case output one integer representing the number of shortest ways in a single line. This number is guaranteed to be less than 2^63.

Sample Input

5 6
1
2 4 3 2
0 0

Sample Output

192
#include<stdio.h>
#include<string.h>
__int64 p[2000005],m,n,x1,y1,h,w;
int visit[2000005];
int main()
{
    __int64 i,cnt,j,k;
 while(scanf("%I64d%I64d",&m,&n)&&m+n)
 {
       scanf("%I64d",&cnt);
    m++;
    n++;
    memset(p,0,sizeof(__int64));
    memset(visit,0,sizeof(visit));
    for(i=0;i<n;i++)  p[i]=1;
    for(i=0;i<m;i++) p[i*n]=1;
    for(i=0;i<cnt;i++)
    {
     scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&h,&w);
     for(j=x1;j<x1+h-1;j++)
     {
      for(k=y1;k<y1+w-1;k++)
      {
       p[j*n+k]=0;
       visit[j*n+k]=1;
      }
     }
    }
    for(i=1;i<m;i++)
    {
     for(j=1;j<n;j++)
     {
     if(visit[i*n+j]!=1) p[i*n+j]=p[i*n+j-1]+p[n*(i-1)+j];
     }
    }
    printf("%I64d\n",p[m*n-1]);
 }
 return 0;
}
posted @ 2013-04-15 22:41  forevermemory  阅读(244)  评论(0编辑  收藏  举报