1. 程式人生 > >HDU-6229 ICPC-瀋陽M- Wandering Robots 概率

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