Your ways (动态规划)ACM-ICPC Asia Phuket Regional Programing Contest 2009

来源:互联网 发布:华西村黑暗真相知乎 编辑:程序博客网 时间:2024/06/05 15:40

F. Your Ways

Time Limit: 3000ms
Memory Limit: 65536KB
64-bit integer IO format: %lld      Java class name:Main
SubmitStatus PID: 4275

 You live in a small well-planned rectangular town in Phuket. The size of the central area of the town is H kilometers x W kilometers. The central area is divided into HW unit blocks, each of size 1 x 1 km2. There are H + 1 streets going in the West to East direction, and there are W + 1 avenue going in the North-South direction. The central area can be seen as a rectangle on the plane, as shown below.

We can identify each intersection by its co-ordinate on the plane. For example, on the Figure above the bottom-left corner is intersection (0,0), and the top-right corner is intersection (6,3).
Your house is at the bottom-left corner (i.e., intersection (0,0)) and you want to go to the university at the top-right corner (i.e., intersection (W,H)). More over, you only want to go to the university with wasting any efforts; therefore, you only want to walk from West-to-East and South-to-North directions. Walking this way, in the example above there are 84 ways to reach the university.
You want to go to the university for K days. Things get more complicated when each morning, the city blocks parts of streets and avenues to do some cleaning. The blocking is done in such a way that it isnot possible to reach parts of the streets or avenues which is blocked from some other part which is blocked as well through any paths containingonly West-to-East and South-to-North walks.
You still want to go to the university using the same West-to-East and South-to-North strategy. You want to find out for each day, how many ways you can reach the university by only walking West-to-East and South-to-North. Since the number can be very big, we only want the resultmodulo 2552.

Input

The first line contains an integer T, the number of test cases (1 <= T <= 5). Each test case is in the following format.

The first line of each test case contains 3 integers: W, H, and K (1 <= W <= 1,000; 1 <= H <= 1,000; 1 <= K <= 10,000). W and H specify the size of the central area. K denotes the number of days you want to go to the university.
The next K lines describe the information on broken parts of streets and avenues. More specifically, line 1 + i, for 1 <= i <= K, starts with an integer Qi(1 <= Qi <= 100) denoting the number of parts which are blocked. Then Qi sets of 4 integers describing the blocked parts follow. Each part is described with 4 integers, A, B, C, and D (0 <= A <= C <= W; 0 <= B <= D <= H) meaning that the parts connecting intersection (A,B) and (C,D) is blocked. It is guaranteed that that part is a valid part of the streets or avenues, also C - A <= 1, and D – B <= 1, i.e., the part is 1 km long.

Output

For each test case, for each day, your program must output the number of ways to go to the universitymodulo 2552 on a separate line. i.e., the output for each test case must contains K lines.

Sample Input

22 2 31 0 0 0 12 1 0 2 0 0 2 1 21 1 1 2 1100 150 21 99 150 100 1502 99 150 100 150 100 149 100 150

Sample Output

34415620

Hint

The amount of I/O for this task is quite large. Therefore, when reading input, you should avoid using java.io.Scanner which is much slower than using java.io.BufferedReader. 

题意: 给你一个 w*h 的矩阵 k天 每天有q条路不通 问你从左下角走到右上角有多少方案

Orz。。。比赛被这题坑到了 

直接暴力找超时 

最后想出来 思路是用 cnt = 最大方案数 - 不通路的方案数

比赛时间结束!55...

#include<bits/stdc++.h>using namespace std;int Map[1003][1003];int main(){    int T;    scanf("%d",&T);    for(int xi=0; xi<=1001; xi++)        Map[xi][0]=Map[0][xi]=1;    for(int xi=1; xi<=1001; xi++)        for(int yi=xi; yi<=1001; yi++)        {            Map[xi][yi]+=Map[xi-1][yi];            Map[xi][yi]+=Map[xi][yi-1];            Map[yi][xi]=Map[xi][yi]%=2552;        }//        for(int i=0;i<10;i++,puts(""))//        for(int j=0;j<10;j++)//        printf("%d ",Map[i][j]);    while(T--)    {        int x,y,t,sum;        scanf("%d%d%d",&x,&y,&t);        for(int i=0; i<t; i++)        {            sum=Map[x][y];            int tt;            scanf("%d",&tt);            for(int j=0; j<tt; j++)            {                int x1,x2,y1,y2;                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);                sum-=Map[x-x2][y-y2]*Map[x1][y1];//                printf("%d  %d  %d  %d\n",x-x2,y-y2,Map[x-x2][y-y2],Map[x1][y1]);                sum+=2552*100000;                sum%=2552;            }            printf("%d\n",sum);        }    }}
0 0
原创粉丝点击