zoukankan      html  css  js  c++  java
  • hdu2262 Where is the canteen

    Where is the canteen

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 1313    Accepted Submission(s): 384

    Problem Description
    After a long drastic struggle with himself, LL decide to go for some snack at last. But when steping out of the dormitory, he found a serious problem : he can't remember where is the canteen... Even worse is the campus is very dark at night. So, each time he move, he check front, back, left and right to see which of those four adjacent squares are free, and randomly walk to one of the free squares until landing on a canteen.
     
    Input
    Each case begin with two integers n and m ( n<=15,m<=15 ), which indicate the size of the campus. Then n line follow, each contain m characters to describe the map. There are 4 different type of area in the map:

    '@' is the start location. There is exactly one in each case.
    '#' is an impassible square.
    '$' is a canteen. There may be more than one in the campus.
    '.' is a free square.
     
    Output
    Output the expected number of moves required to reach a canteen, which accurate to 6 fractional digits. If it is impossible , output -1.
     
    Sample Input
    1 2 @$ 2 2 @. .$ 1 3 @#$
     
    Sample Output
    1.000000 4.000000 -1
     

    /*
    将点所有点令为1至n*m-1那么建立n*m个方程
    对于一个任一个点En=(E1+E2+E3)/cnt+1; cnt为可以走的方向数 [0,4]
    最后用高斯消元模版求解(用kuangbin的模版不知为什么这么慢)
    而且餐厅有很多个
    */
    #include<iostream>
    #include<cmath>
    #include<queue>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    
    #define eps 1e-12
    const int MAXN = 250;
    char mp[20][20];
    bool vis[20][20];
    int n,m;
    int sx,sy;
    int dx[4]={-1,1,0,0};
    int dy[4]={0,0,1,-1};
    struct Node{
        int x,y;
    }aa,bb;
    queue<Node>q;
    int temp[MAXN];
    double a[MAXN][MAXN];
    double x[MAXN];
    int equ,var;
    
    inline int C(int x,int y){
        return x*m+y;
    }
    bool Ok(int x,int y,int d){
        if(d==0){
            if(x>=0 && x<n && y>=0 && y<m && mp[x][y]!='#' && !vis[x][y]) return true;
        }else {
            if(x>=0 && x<n && y>=0 && y<m && mp[x][y]!='#' && vis[x][y]) return true;
        }
        return false;
    }
    void Bfs(){
        int i,j,k;
        while(!q.empty()){
            bb=q.front(); q.pop();
            for(i=0;i<4;i++){
                aa.x=bb.x+dx[i];
                aa.y=bb.y+dy[i];
                if(Ok(aa.x,aa.y,0)){
                    vis[aa.x][aa.y]=1;
                    q.push(aa);
                }
            }
        }
    }
    
    void Makefunction(){
        int i,j,k;
        for(i=0;i<n;i++)
            for(j=0;j<m;j++){
                int cnt=0;
                if(mp[i][j]=='#') continue;
                if(mp[i][j]=='$'){
                    a[C(i,j)][C(i,j)]=1; continue;
                }
                for(k=0;k<4;k++){
                    int xx=i+dx[k];
                    int yy=j+dy[k];
                    if(Ok(xx,yy,1)){
                        cnt++; a[C(i,j)][C(xx,yy)]=1;
                    }
                }
                a[C(i,j)][C(i,j)]=-1*cnt;
                x[C(i,j)]=-1*cnt;
            }
    }
    int Gauss(){
        int i,j,k,col,max_r; //max_r 指现在对哪一行操作 equ 方程数 var 未知数个数
        for(k=0,col=0;k<equ && col<var;k++,col++){
            max_r=k;
            for(i=k+1;i<equ;i++)
                if(fabs(a[i][col])>fabs(a[max_r][col])) max_r=k; //寻找这个未知数最大的一个
            if(fabs(a[max_r][col])<eps) {
                if(col==C(sx,sy)) return 0;
                else continue;
            }
            if(k!=max_r){
                for(j=col;j<var;j++) swap(a[k][j],a[max_r][j]);
                swap(x[k],x[max_r]);
            }
            x[k]/=a[k][col];
            for(j=col+1;j<var;j++) a[k][j]/=a[k][col];
            a[k][col]=1;
            for(i=0;i<equ;i++)
                if(i!=k){
                    x[i]-=x[k]*a[i][col];
                    for(j=col+1;j<var;j++) a[i][j]-=a[k][j]*a[i][col];
                    a[i][col]=0;
                }
        }
        return 1;
    }
    
    int main(){
        //freopen("in.txt","r",stdin);
        int i,j,k;
        while(~scanf("%d %d",&n,&m)){
            q.empty();
            memset(vis,0,sizeof(vis));
            for(i=0;i<n;i++) scanf("%s",mp[i]);
            for(i=0;i<n;i++)
                for(j=0;j<m;j++){
                    if(mp[i][j]=='@'){ sx=i; sy=j; }
                    if(mp[i][j]=='$'){ aa.x=i; aa.y=j; q.push(aa); vis[i][j]=1; }
                }
            Bfs();
            memset(a,0,sizeof(a));
            memset(x,0,sizeof(x));
            Makefunction();
            var=n*m; equ=n*m;
            ;
            if(vis[sx][sy] && Gauss() ) printf("%lf
    ",x[C(sx,sy)]);
            else printf("-1
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    OpenCV---直方图反向投影
    OpenCV---直方图的应用(均衡化和图像比较)
    ASP.NET WEB SERVICE 创建、部署与使用
    DataSet和DataTable有用的方法
    黑客网站
    局域网中工作组和域之间的差别
    索引使用原则(精)
    C# Web Service 初级教学
    Extjs下拉多选框
    如何在UltraEdit中高亮显示PB代码
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8433772.html
Copyright © 2011-2022 走看看