题意:已知一个机器人从(0,0)出发,每次移动到上下左右以及原地不动的概率都相等,但是不会朝有障碍或者边界位置移动,现在放入了k个障碍物,问停留在(i,j)并且满足(i+j)>=N-1的概率是多少
思路:由于时间是无限长的,所以
假设到每个点的概率是相等的,那么经过无限时间之后机器人停留在每个点的概率都是相等的
现在到每个点的概率不相等,到每个点的概率的权重如下图
可以看到落在四个角上的概率的权重为3,落在边上的权重为4,落在中间的权重为5,那么就可以用权重计算出落在满足(i+j)>=N-1的概率是(3+5+3+4+4+3)/(3+4+3+4+5+4+3+4+3)=22/33=2/3
样例2:中间放置了一个障碍物,周围四个格子,以及中间权重随之改变,如图
可以用权重计算出落在满足(i+j)>=N-1的概率是(3*5)/(3*8)=15/24=5/8
现在只需要预处理出所有权重,如果遍历N*N的地图会超时,因为有多组样例T 时间复杂度O(T*N*N),只需要把所有输入的障碍位置用map存下来,然后权重减去由于障碍物减少的权重就可以得到答案
代码实现
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define pp pop_back()
#define int long long
#define laile cout<<"laile"<<endl
#define lowbit(x) ((x)&(-x))
#define double long double
#define sf(x) scanf("%lld",&x)
#define sff(x,y) scanf("%lld %lld",&x,&y)
#define _for(i,n) for(int i=0;i<(n);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b);++i)
#define all(x) (x).begin(), (x).end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef unsigned long long ULL;
typedef pair<int,int>PII;
const int N=10010,INF=4e18;
int n,m,k;
map<PII,int> ma;
int get(int i,int j){
if(i==1&&j==1) return 3;
else if(i==1&&j==n) return 3;
else if(i==n&&j==n) return 3;
else if(i==n&&j==1) return 3;
else if(i<1||i>n||j<1||j>n)return 0;
else if(i==1||j==n||i==n||j==1)return 4;
else return 5;
}
void solve(int tt)
{
ma.clear();
cin>>n>>k;
while(k--)
{
int a,b;
cin>>a>>b;
a++;b++;
ma[{a,b}]=get(a,b);
ma[{a-1,b}]++;
ma[{a,b-1}]++;
ma[{a+1,b}]++;
ma[{a,b+1}]++;
}
int mu=n*n*5-4*n;
int zi=(mu-(5*n-4))/2+5*n-4;
for(auto e:ma){
int i=e.fi.fi,j=e.fi.se;
int t=min(e.se,get(i,j));
mu-=t;
if(i+j>=n+1) zi-=t;
}
cout<<"Case #"<<tt<<": ";
if(mu==0)cout<<"0\n";
else
{
int gc=__gcd(mu,zi);
cout<<zi/gc<<"/"<<mu/gc<<'\n';
}
return ;
}
signed main()
{
IOS;
int T=1;
cin>>T;
_rep(i,1,T)
solve(i);
return 0;
}