hdu 2262 高斯消元求期望

时间:2023-03-09 00:20:13
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
题目大意:一个图,一个起点多个终点,求到达终点的步数期望。
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std; char map[][];
int vis[][],d[][];//vis记录访问的标号,d记录改点的出度
double A[][];
int dir[][]={,,-,,,,,-};
int n,m,cnt,stx,sty;
const double eps=1e-;
int dcmp(double x)
{
if(fabs(x)<eps) return ;
if(x->eps) return ;
return -;
}
void swap(int &a,int &b){int t=a;a=b;b=t;}
struct point
{
int x,y;
}p,t; void init()
{
int i,j;
memset(vis,-,sizeof(vis));
memset(A,,sizeof(A));
memset(d,,sizeof(d));
for(i=;i<n;i++)
{
getchar();
for(j=;j<m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='@')
stx=i,sty=j;
}
}
} bool judge(point p)
{
if(<=p.x&&p.x<n&&<=p.y&&p.y<m&&map[p.x][p.y]!='#')
return true;
return false;
} bool bfs()
{
queue<point> Q;
p.x=stx;p.y=sty;cnt=;
Q.push(p);
vis[p.x][p.y]=cnt++;
bool flag=;
while(!Q.empty())
{
p=Q.front();Q.pop();
for(int i=;i<;i++)
{
t.x=p.x+dir[i][];t.y=p.y+dir[i][];
if(judge(t))
{
if(map[t.x][t.y]=='$') flag=;
d[p.x][p.y]++;
if(vis[t.x][t.y]!=-) continue;
vis[t.x][t.y]=cnt++;
Q.push(t);
}
}
}
return flag;
} void build_matrix()
{
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(vis[i][j]==-) continue;
int u=vis[i][j];
A[u][u]=;
if(map[i][j]=='$'){A[u][cnt]=;continue;}
double p=1.0/d[i][j];
for(int k=;k<;k++)
{
point temp;
temp.x=i+dir[k][];temp.y=j+dir[k][];
if(judge(temp) && vis[temp.x][temp.y]!=-)
{
int v=vis[temp.x][temp.y];
A[u][v]-=p;
A[u][cnt]+=p;
}
}
}
}
A[][cnt]=;
} void gauss(int n)
{
int i,j,k,r;
for(i=;i<n;i++)
{
r=i;
for(j=i+;j<n;j++)
if(fabs(A[j][i])>fabs(A[r][i])) r=j;
if(dcmp(A[r][i])==) continue;
if(r!=i) for(j=;j<=n;j++) swap(A[r][j],A[i][j]);
for(k=;k<n;k++) if(k!=i)
for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
for(i=n-;i>=;i--)
{
for(j=i+;j<cnt;j++)
A[i][cnt]-=A[j][cnt]*A[i][j];
A[i][cnt]/=A[i][i];
}
} int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
if(!bfs()){puts("-1");continue;}
build_matrix();
gauss(cnt);
printf("%.6lf\n",A[][cnt]);
}
return ;
}