HDU6229 Wandering Robots 【概率】

传送门


Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 308    Accepted Submission(s): 172


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。这时计算右下部分的总贡献比上全部的总贡献即为答案。(不要问我为什么,我也不知道,等我弄清楚啦。再来补上)

ACcode
#include
using namespace std;
const int N=10007;
int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
setst;
int main()
{
    int TA,sum,res,sum2,x,y,n,k,xx,yy,cas=1,s;
    scanf("%d",&TA);
    while(TA--)
    {
        st.clear();
        scanf("%d%d",&n,&k);
        for(int i=0;i::iterator it=st.begin();
        while(it!=st.end())
        {
            s=*it;
            x=s/N;
            y=s%N;
            for(int i=0;i<4;i++)
            {
                xx=x+dx[i];
                yy=y+dy[i];
                if(xx<0||yy<0||xx>=n||yy>=n||st.count(xx*N+yy))continue;
                sum--;
                if(xx+yy>=n-1)
                sum2--;

            }
           
            if(x+y>=n-1)
            {
                sum2-=5;
            if(x==0||x==n-1)
                sum2++;
            if(y==0||y==n-1)
                sum2++;
            } 
               sum-=5;
            if(x==0||x==n-1)
                sum++;
            if(y==0||y==n-1)
                sum++;
            it++;
        }
        int res=__gcd(sum,sum2);
        printf("Case #%d: %d/%d\n",cas++,sum2/res,sum/res);
    }
}



你可能感兴趣的:(【数学】)