【20200321】银牌题[Cloned] B - Wandering Robots

也就是HDU6229

【题目】

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.

---

1.沈阳区域赛是用LaTex写的,吐槽一下,我没看懂frac公式

2.概率+离散化,其实非常简单,

/*考虑到无限情况,走了一定步数之后可能分布于任何一个点,
所以初始点没有用*/
那么就是有一个权值,可以从哪些点走到它。。。杀了一个点之后,他周围的点都少一个,(其实在map里就多记录一个罢,最后减去原始sum和sumr)
---
代码
/*明明是概率统计*/
/*考虑到无限情况,走了一定步数之后可能分布于任何一个点,
所以初始点没有用*/
#pragma GCC diagnostic error "-std=c++11"
#include <bits/stdc++.h>
using namespace std;
const int dx[]={0,0,-1,1};
const int dy[]={-1,1,0,0};
int t;
map <pair<int,int>,int> mp;  
int check(int x,int y,int n){
    if (x<0 || x>=n || y<0 || y>=n) return false;
    return true;
}
int main(){
    scanf("%d",&t);
    for (int kase=1;kase<=t;kase++){
        mp.clear();
        int n,k;
        scanf("%d%d",&n,&k);
        int sum((3*4)+((n-2)*4*4)+(n-2)*(n-2)*5),sumr=((3*3)+((n-2)*2*4)+((n-2)*(n-1)/2*5));
        for (int i=1;i<=k;i++){
            int x,y;
            scanf("%d%d",&x,&y);
            mp[{x,y}]=6;
            for (int i=0;i<4;i++){
                int xx=x+dx[i],yy=y+dy[i];
                mp[{xx,yy}]++;
            }
        }
            for (auto it:mp){
                int xx=it.first.first,yy=it.first.second;
                if (check(xx,yy,n)){
                    int res=5;
                    if (xx==0 && yy==0 || xx==0 && yy==n-1 || xx==n-1 && yy==0 || xx==n-1 && yy==n-1)
                        res=3;
                    else if (xx==0 || xx==n-1 || yy==0 || yy==n-1) res=4;
                    if (xx+yy>=n-1) sumr=sumr-min(res,it.second);
                    sum=sum-min(res,it.second);
                }
            }
        int tmp=__gcd(sum,sumr);
        printf("Case #%d: %d/%d\n",kase,sumr/tmp,sum/tmp);
        
    }
}

  太棒了,学到很多

再找题解的时候发现,啊,这个

#pragma GCC diagnostic error "-std=c++11"
神奇的命令可以auto不报错。
auto it,是个iterator,直接指向元素,而map里的每一个元素又是一个pair。而map里我又设了一个pair 。所以是it.first是一个pair,那么要找it.first.first是xx,it.first.second是yy,it.second是那个值,

猜你喜欢

转载自www.cnblogs.com/asanagiyantia/p/12543532.html