hdu 6229

来源:互联网 发布:php 广告发布系统源码 编辑:程序博客网 时间:2024/06/03 13:49

Wandering Robots

Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 252 Accepted Submission(s): 146

Problem Description
In an attempt to colonize Mars, some scientists were tasked with cleaning the planet. A cleaning robot, Marsba,was build with a huge restricted area in the Mars as a massive N × N square grid with K (K ≤ 1000) impassable barriers. This area are numbered from (0, 0) to (N - 1, N - 1) sequentially from left to right, row by row, where N ≤ 10000. The starting point of Marsba is situated on the top left corner lattice (0, 0). Marsba had instructions to program him with equal probability of remaining in the same lattice or travelling to an adjacent one. (Two lattices are said to be adjacent if they share a common edge.) This meant an equal probability being split equally between remaining in the lattice and the number of available routes. Specifically, for the lattice Marsba located in which has d adjacent lattices without impassable barriers, the probability for Marsba of remaining in the lattice or travelling to any adjacent lattice is \frac{1}{d+1} .
Then, those scientists completely forgot about it.
Many millennia ago, a young man realizes the importance of the cleaning robot, Marsba, at the end of the forgotten.
For further research, he asks you to calculate the probability of Marsba’s location (x, y) satisfying x + y ≥ N - 1.
Let the probability be an irreducible fraction of the form p/q, you should output p and q respectively, with a fraction slash as the separator.

Input
The first line of the input contains an integer t (t ≤ 1000) specifying the number of test cases.
For each case, the first line contains two positive integers N and K. Each of the next K lines contains the coordinate of a barrier.
Note that the starting point (0, 0) has no barrier and all test cases guarantee the connectivity of all lattices free of barriers.

Output
For each case output its label first, then output the probability as an irreducible fraction.

Sample Input
5
3 0
3 1
1 1
3 2
1 1
2 2
3 3
1 1
1 2
2 2
5 4
1 1
1 2
2 3
3 2

Sample Output
Case #1: 2/3
Case #2: 5/8
Case #3: 10/19
Case #4: 7/16
Case #5: 43/71

题意:有一个机器人从(0,0)出发,每次等概率的不动或者往上下左右没有障碍的地方走动,问走无限步后停在图的右下部的概率是多少?
做法:找规律?如果一个点没有障碍,那么它对周围和自己的贡献为1,然后计算右下的贡献和除以所有点的贡献和,
反正我不会证明,知道了这一点后就是用map去搞了,具体做法是用map去算贡献,map记录的是一个点被他周围的障碍访问了几次,那么它的贡献就会减少这么多,然后用一个较大的数去标记障碍,然后扫一遍map就可以了。

#include<bits/stdc++.h>#define pii pair<int,int>using namespace std;int T,n,k,kase = 1;map<pii,int> mp;int xx[] = {0,0,1,-1};int yy[] = {1,-1,0,0};int sum,sumr;int cal(int x,int y){    int ret = 5;    if(x == 0) ret --;    if(x == n-1) ret --;    if(y == 0) ret --;    if(y == n-1) ret --;    return ret;}bool judge(int x,int y){    if(x < 0 || x >= n || y < 0 || y >= n) return false;    return true;}void solve(int x,int y){    mp[{x,y}] = 10;    for(int i = 0;i < 4;i ++){        int nx = x+xx[i],ny = y+yy[i];        mp[{nx,ny}] ++;    }}int main(){    cin >> T;    while(T--){        mp.clear();        scanf("%d %d",&n,&k);        for(int i = 1;i<= k;i ++){            int x,y;            scanf("%d %d",&x,&y);            solve(x,y);        }        sum = 5*n*n-4*n;        sumr = (5*n*n+n-4)/2;        for(auto it:mp){            int x = it.first.first,y = it.first.second;            if(judge(x,y)){                int res = 0 ;                if(it.second>= 10){                    res = cal(x,y);                }                else{                    res = it.second;                }                sum -= res;                if(x+y >= n-1){                    sumr -= res;                }            }        }        int gd = __gcd(sum,sumr);        printf("Case #%d: %d/%d\n",kase++,sumr/gd,sum/gd);    }    return 0;}