2017ACM/ICPC亚洲区沈阳站_Wandering Robots(hash)_马尔科夫链_随机游走

来源:互联网 发布:淘宝加热棒 编辑:程序博客网 时间:2024/05/16 05:17
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define INF 0x3f3f3f3f#define rep0(i, n) for (int i = 0; i < n; i++)#define rep1(i, n) for (int i = 1; i <= n; i++)#define rep_0(i, n) for (int i = n - 1; i >= 0; i--)#define rep_1(i, n) for (int i = n; i > 0; i--)#define MAX(x, y) (((x) > (y)) ? (x) : (y))#define MIN(x, y) (((x) < (y)) ? (x) : (y))#define mem(x, y) memset(x, y, sizeof(x))#include <map>#define MAXN (10000 + 10)#define MAXK 1000/**题目大意N * N的区域内,有K个格子不能到达,机器人从(0, 0)出发有均等的该概率留在原地和到达上下左右可到达的区域,问无穷远的时间以后有多大概率到达x + y >= n - 1 的区域思路计算除了不能到达的格子之外的格子能通往多少方向d,则格子的权值为d + 1ans = x + y >= n - 1 的格子的权值之和 / 总权值和*******************************************************马尔科夫链的随机游走模型可建立状态转移矩阵,对n * n 的图中n * n 个点编号为0 ~[ (n - 1) * n + n – 1]  设最大编号为maxP = p(i, j) = [p(0, 0) p(0, 1) … p(0, max)               P(1, 0) p(1, 1) … p(1, max)               …               P(max, 0) p(max, 1) … p(max, max)]π(i) 为i时间各点的概率π(n + 1) = π(n) * P当时间->无穷 π(n + 1)->π可以通过 π * P = π 计算  验证猜测结果正确*******************************************************找规律的答案 有待证明现在能想到的是 整个封闭系统每个格子以出现机器人的概率作为权值 在很长的时间线上是一个熵增的过程(想到元胞自动机),如果要模拟这个概率扩散的过程的话,格子的权值的更新是一个用他所能到达的格子的权值和他自身的权值迭代的过程,这个过程中可以发现他的相邻的格子的权值是在不断同化的,因此,在无穷远后(0, 0)的和他周围的格子的权值不在体现优势,而更加开放的格子则更占优(可根据迭代公式理解)*/using namespace std;typedef long long LL;int n, k;map<LL, int> mp;int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int g[MAXK][2];LL gcd(LL x, LL y){    if (y == 0)        return x;    else return gcd(y, x % y);}int main(){    #ifndef ONLINE_JUDGE        freopen("in.txt", "r", stdin);    #endif // ONLINE_JUDGE    int t, kase = 0;    scanf("%d", &t);    while(t--)    {        mp.clear();        scanf("%d %d", &n, &k);        LL a = 0, b = 0;        if (n > 2)        {            a += (LL)(n - 2) * 4 * 2 + 3 * 3 + (LL)(n - 2) * (n - 1) / 2 * 5;            b += 4 * 3 + (LL)(n - 2) * 4 * 4 + (LL)(n - 2) * (n - 2) * 5;        }        else        {            a += 3 * 3;            b += 4 * 3;        }        //cout << a << "   " << b << endl;        int x, y;        for (int i = 0; i < k; i++)        {            scanf("%d %d", &x, &y);            pair<LL, int> tmp;            tmp.first = (LL)x * MAXN + y;            tmp.second = i;            mp.insert(tmp);        }        map<LL, int> :: iterator itr;        map<LL, int> :: iterator itr1;        for (itr = mp.begin(); itr != mp.end(); itr++)        {            x = (itr->first) / MAXN;            y = (itr->first) % MAXN;            int cnt = 0;            for (int i = 0; i < 4; i++)            {                int nx = x + dir[i][0], ny = y + dir[i][1];                if (nx < 0 || nx >= n || ny < 0 || ny >= n)                    continue;                cnt++;                bool t = (itr1 = mp.find( (LL) nx * MAXN + ny) ) == mp.end();                if (t && nx + ny >=  n - 1)                {                    b--;                    a--;                }                else if (t)                    b--;            }            b -= cnt + 1;            if (x + y >= n - 1)                a -= cnt + 1;        }        LL g = gcd(a, b);        printf("Case #%d: %lld/%lld\n", ++kase, a / g, b / g);    }    return 0;}