HDU-6229 ICPC-沈阳M- Wandering Robots 概率

HDU - 6229 

题意:

  在一个n*n的地图中,有一个初始在(0,0)位子的机器人,每次等概率的向相邻的格子移动或者留在原地。问最后留在格子(x,y)(x+y>=n-1)的地方的概率。

思路:

  这道题由于每个格子的贡献是不同的,在四个角格子的贡献是3分(留下来,两个边来的),中间的5分,有一条边与边相连的4分。如果这个点是障碍物,则把这个点的贡献抹为0,再把其四周的格子贡献-1.

  由于开不下数组,可以用map

// #pragma GCC optimize(3)
// #pragma comment(linker, "/STACK:102400000,102400000")  //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
 
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>
#include        <map>
 
using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue
 
 
 
typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;
 
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'
 
#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;
 
const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x7f7f7f7f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e8+7;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;
 
 
template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}
 
 
/*-----------------------showtime----------------------*/
            map<pii,int>mp;
            int nx[4][4] = {
                {1,0}, {0,1} ,{-1,0},{0,-1}
            };
            int T,n,k;
            int get(int x,int y){
                if(x>0&&x<n-1 && y>0 && y<n-1)
                    return 5;
                if(x==0&&y==0)return 3;
                if(x==n-1&&y==0)return 3;
                if(x==0&&y==n-1)return 3;
                if(x==n-1&&y==n-1)return 3;
                return 4;
            }
            ll gcd(ll a, ll b){
                if(b == 0)return a;
                return gcd(b, a % b);
            }
int main(){
            scanf("%d", &T);
           for(int tt=1; tt<=T; tt++){
                mp.clear();
                scanf("%d%d", &n, &k);
                if(n == 1){
                    printf("Case #%d: 1/1\n", tt);
                    continue;
                }

                ll sum = 4*3+(n-2)*4*4+(n-2)*(n-2)*5;
                ll dec = 0;
                ll up = 3*3+(n-2)*2*4+(n-1)*(n-2)/2*5;
                
                for(int i=1; i<=k; i++){
                    int x,y;
                    scanf("%d%d", &x, &y);

                    for(int i=0; i<4; i++){
                        int tx = x + nx[i][0];
                        int ty = y + nx[i][1];
                        if(tx <0 || tx >= n||ty<0||ty>=n)continue;
                        if(mp.count(pii(tx,ty))){
                            if(mp[pii(tx,ty)] == 0)continue;
                            mp[pii(tx,ty)] --;
                            if(tx + ty >= n-1)up--;
                            sum--;
                        }
                        else {
                            mp[pii(tx,ty)] = get(tx,ty) - 1;
                            if(tx + ty >= n-1)up--;
                            sum--;
                        }
                    }
                    if(mp.count(pii(x,y))) {
                        if(mp[pii(x,y)] == 0)continue;
                        if(x + y >= n-1)up -= mp[pii(x,y)];
                        sum -= mp[pii(x,y)];
                        mp[pii(x,y)] = 0;
                    }
                    else{
                        mp[pii(x,y)] = 0;
                        if(x + y >= n-1)up -= get(x,y);
                        sum -= get(x,y);
                    }
                }
                ll gg = gcd(up, sum);
                printf("Case #%d: %lld/%lld\n", tt, up/gg, sum/gg);

                // for(int i=0; i<n; i++){
                //     for(int j=0; j<n; j++){
                //         if(mp.count(pii(i,j)))
                //             cout<<i<<" , "<<j<<"="<<mp[pii(i,j)]<<endl;
                //     }
                //     cout<<endl;
                // }
           }

            return 0;
}
HDU - 6229

猜你喜欢

转载自www.cnblogs.com/ckxkexing/p/9944097.html