HDU 2262 Where is the canteen 期望 + 高斯消元

题意:LL从起点’@’出发,在每个点等概率的向可以走的点走。’$’是目的地,可能有多个。问从起点到目的地走的步数的期望?
思路:每个点
E1E2...EkEi
Ei=(E1+E2+...+Ek)/k+
E1+E2+...+EkkEi=k
注意:要用BFS或者DFS的方法去找点,因为如果你的高斯消元是要求出每个 Ei 的话,
@..#...#$
上面这个例子,右上角的点是解不出来的,就会导致答案出现错误。

http://acm.hdu.edu.cn/showproblem.php?pid=2262

/*********************************************
    Problem : HDU 2262
    Author  : NMfloat
    InkTime (c) NM . All Rights Reserved .
********************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#define rep(i,a,b)  for(int i = (a) ; i <= (b) ; i ++) //遍历
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --) //反向遍历
#define repS(it,p) for(auto it = p.begin() ; it != p.end() ; it ++) //遍历一个STL容器
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next) //遍历u所连接的点
#define cls(a,x)   memset(a,x,sizeof(a))
#define eps 1e-8

using namespace std;

const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 255;
const int MAXE = 2e5+5;

typedef long long LL;
typedef unsigned long long ULL;

int T,n,m;

int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};

double a[MAXN][MAXN],x[MAXN]; // 矩阵a存的是系数矩阵 矩阵x存的是右边的值
int equ ; // 方程数 和 未知数个数

char Map[20][20];
bool vis[20][20];
int idx[20][20];

struct qnode {
    int x,y;
    qnode(int _x,int _y) {x = _x ; y = _y;}
};
queue<qnode>que;

void debug() {
    rep(i,1,equ) {
        rep(j,1,equ) {
            printf("%f ",a[i][j]);
        }
        printf("= %f\n",x[i]);
    }
    puts("");
}

bool gauss() {// 0代表无解,1代表有解
    //debug();
    rep(i,1,equ) { // 代表equ次矩阵变换
        //用第i行第i个数消除第i列。
        //在第i列中找到最大的一个数 从第i行到第equ行
        int maxd = i; //maxd代表第几行 
        rep(j,i+1,equ) if(fabs(a[maxd][i])<fabs(a[j][i])) maxd = j;
        if(fabs(a[maxd][i])<eps) return false; //无解
        //如果最大的数在i行,就不用交换,否则就要交换行和列。
        if(maxd != i) {
            //交换 maxd 行和 i 行
            rep(j,i,equ) swap(a[maxd][j],a[i][j]);
            swap(x[maxd],x[i]);
        }

        //用i行i列的数去消除其他行
        rep(j,i+1,equ) { //代表现在消除的是哪一行
            double rate = a[j][i] / a[i][i];
            rep(k,i,equ) {//代表第j行第k个数 
                a[j][k] -= rate * a[i][k];
            }
            x[j] -= rate * x[i];
        }
        //debug();
    }
    //debug();
    //消元结束,现在开始找出结果
    rrep(i,1,equ) {
        rep(j,i+1,equ) {
            x[i] -= a[i][j] * x[j];
        }
        x[i] /= a[i][i]; 
    }
    return true;
    //x数组存的就是最后结果。
}

void input() {
    rep(i,1,n) scanf("%s",&Map[i][1]);
}

// E(x) = E(1) + ... + E[k] / k + 1;
// E[1] + E[2] + E[k] - kE[x] = -k; 

int tot ;
int index ;
int sx,sy;

int BFS() {
    while(!que.empty()) que.pop();
    que.push(qnode(sx,sy));
    cls(vis,0);
    index = 0;
    vis[sx][sy] = 1; idx[sx][sy] = ++index;
    tot = 0; //代表第tot个方程
    while(!que.empty()) {
        qnode tmp = que.front();
        que.pop();
        ++ tot;
        int cnt = 0;
        if(Map[tmp.x][tmp.y] == '$') {
            a[tot][idx[tmp.x][tmp.y]] = 1;
            x[tot] = 0;
            continue;
        }
        rep(i,1,4) {
            int tmpx = tmp.x + fx[i];
            int tmpy = tmp.y + fy[i];
            if(tmpx >= 1 && tmpx <= n && tmpy >= 1 && tmpy <= m && Map[tmpx][tmpy] != '#') {
                if(!vis[tmpx][tmpy]) {
                    vis[tmpx][tmpy] = 1;
                    idx[tmpx][tmpy] = ++index;
                    que.push(qnode(tmpx,tmpy));
                }
                cnt ++;
                a[tot][idx[tmpx][tmpy]] = 1;
            }
        }
        a[tot][idx[tmp.x][tmp.y]] = -cnt;
        x[tot] = - cnt;
    }
    return tot;
}

void solve() {
    cls(a,0);
    rep(i,1,n) rep(j,1,m) {
        if(Map[i][j] == '@') {
            sx = i; sy = j; 
        }
    }
    BFS(); // 得到系数矩阵
    equ = tot;
    if(!gauss()) puts("-1"); // 解方程
    else printf("%.6f\n",x[1]);
}

int main(void) {
    //freopen("a.in","r",stdin);
    while(~scanf("%d %d",&n,&m)) {
        input();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值