首页 > 代码库 > hdu 2262 高斯消元求期望

hdu 2262 高斯消元求期望

Where is the canteen

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


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 #include <iostream>  2 #include <cstring>  3 #include <cstdio>  4 #include <cmath>  5 #include <queue>  6 using namespace std;  7   8 char map[20][20];  9 int vis[20][20],d[20][20];//vis记录访问的标号,d记录改点的出度 10 double A[230][230]; 11 int dir[4][2]={1,0,-1,0,0,1,0,-1}; 12 int n,m,cnt,stx,sty; 13 const double eps=1e-8; 14 int dcmp(double x) 15 { 16     if(fabs(x)<eps) return 0; 17     if(x-0>eps) return 1; 18     return -1; 19 } 20 void swap(int &a,int &b){int t=a;a=b;b=t;} 21 struct point 22 { 23     int x,y; 24 }p,t; 25  26 void init() 27 { 28     int i,j; 29     memset(vis,-1,sizeof(vis)); 30     memset(A,0,sizeof(A)); 31     memset(d,0,sizeof(d)); 32     for(i=0;i<n;i++) 33     { 34         getchar(); 35         for(j=0;j<m;j++) 36         { 37             scanf("%c",&map[i][j]); 38             if(map[i][j]==@) 39                 stx=i,sty=j; 40         } 41     } 42 } 43  44 bool judge(point p) 45 { 46     if(0<=p.x&&p.x<n&&0<=p.y&&p.y<m&&map[p.x][p.y]!=#) 47         return true; 48     return false; 49 } 50  51 bool bfs() 52 { 53     queue<point> Q; 54     p.x=stx;p.y=sty;cnt=0; 55     Q.push(p); 56     vis[p.x][p.y]=cnt++; 57     bool flag=0; 58     while(!Q.empty()) 59     { 60         p=Q.front();Q.pop(); 61         for(int i=0;i<4;i++) 62         { 63             t.x=p.x+dir[i][0];t.y=p.y+dir[i][1]; 64             if(judge(t)) 65             { 66                 if(map[t.x][t.y]==$) flag=1; 67                 d[p.x][p.y]++; 68                 if(vis[t.x][t.y]!=-1) continue; 69                 vis[t.x][t.y]=cnt++; 70                 Q.push(t);         71             } 72         } 73     } 74     return flag; 75 } 76  77 void build_matrix() 78 { 79     for(int i=0;i<n;i++) 80     { 81         for(int j=0;j<m;j++) 82         { 83             if(vis[i][j]==-1) continue; 84             int u=vis[i][j]; 85             A[u][u]=1; 86             if(map[i][j]==$){A[u][cnt]=0;continue;} 87             double p=1.0/d[i][j]; 88             for(int k=0;k<4;k++) 89             { 90                 point temp; 91                 temp.x=i+dir[k][0];temp.y=j+dir[k][1]; 92                 if(judge(temp) && vis[temp.x][temp.y]!=-1) 93                 { 94                     int v=vis[temp.x][temp.y]; 95                     A[u][v]-=p; 96                     A[u][cnt]+=p; 97                 } 98             } 99         }100     }101     A[0][cnt]=1;102 }103 104 void gauss(int n)105 {106     int i,j,k,r;107     for(i=0;i<n;i++)108     {109         r=i;110         for(j=i+1;j<n;j++)111             if(fabs(A[j][i])>fabs(A[r][i])) r=j;112         if(dcmp(A[r][i])==0) continue;113         if(r!=i) for(j=0;j<=n;j++) swap(A[r][j],A[i][j]);114         for(k=0;k<n;k++) if(k!=i)115             for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];116     }117     for(i=n-1;i>=0;i--)118     {119          for(j=i+1;j<cnt;j++)120             A[i][cnt]-=A[j][cnt]*A[i][j];121          A[i][cnt]/=A[i][i];122     }123 }124 125 int main()126 {127     while(~scanf("%d%d",&n,&m))128     {129         init();130         if(!bfs()){puts("-1");continue;}131         build_matrix();132         gauss(cnt);133         printf("%.6lf\n",A[0][cnt]);134     }135     return 0;136 }