题目:有一个n*m的字符矩阵,你要从初始位置到商店去,你每次都选择去一个可达的点(上下左右4个方向),求期望步数。
'@'初始位置,有且只有一个
'#'不能走的点
'$'商店,可能有多个
'.'可以走的点
思路:广搜以便给可达的点编号,列方程高斯消元即可
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<ctime>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define mm(a,b) memset(a,b,sizeof(a))
#define PP puts("*********************");
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
// 0x3f3f3f3f3f3f3f3f
int dr[]={-1,1,0,0};
int dc[]={0,0,-1,1};
const double _eps = 1e-12;
const int MAX = 300;
int equ, var;
double mat[MAX][MAX], x[MAX];
int sign(double x) {return (x > _eps) - (x < -_eps);}
int Gauss() {
int i, j, k, col, max_r, free_num, free_index;
double ta, tmp;
for(i = 0; i <= var; ++i) {
x[i] = 0;
}
for(k = col = 0; k < equ && col < var; ++k, ++col) {
max_r = k;
for(i = k + 1; i < equ; ++i) {
if(sign(fabs(mat[i][col]) - fabs(mat[max_r][col])) > 0)
max_r = i;
}
if(max_r != k) {
for(j = k; j < var + 1; ++j)
swap(mat[max_r][j], mat[k][j]);
}
if(sign(mat[k][col]) == 0)
return 0;
for(i = k + 1; i < equ; ++i) {
if(sign(mat[i][col]) == 0) continue;
ta = mat[i][col] / mat[k][col];
for(j = col; j < var + 1; ++j)
mat[i][j] -= mat[k][j] * ta;
}
}
for(i = k; i < equ; ++i) {
if(sign(mat[i][col]))
return 0;
}
if(k < var)
return 0;
for(i = var - 1; i >= 0; --i) {
tmp = mat[i][var];
for(j = i + 1; j < var; ++j)
if(sign(mat[i][j]) != 0)
tmp -= mat[i][j] * x[j];
x[i] = tmp / mat[i][i];
}
return 1;
}
const int maxn=300;
struct Node{
int r,c;
Node(int _r=0,int _c=0):r(_r),c(_c){}
};
char str[20][20];
int vis[20][20],num[20][20],cnt;
int n,m;
bool judge(int r,int c){
if(r<0||r>=n||c<0||c>=m||str[r][c]=='#')
return false;
return true;
}
int bfs(int r,int c){
queue<Node> Q;
mm(vis,-1);
mm(num,0);
cnt=0;
vis[r][c]=cnt++;
Q.push(Node(r,c));
int flag=0;
while(!Q.empty()){
Node now=Q.front();
Q.pop();
if(str[now.r][now.c]=='$')
flag=1;
for(int i=0;i<4;i++){
r=now.r+dr[i];
c=now.c+dc[i];
if(!judge(r,c)) continue;
num[now.r][now.c]++;
if(vis[r][c]!=-1) continue;
vis[r][c]=cnt++;
Q.push(Node(r,c));
}
}
return flag;
}
void build(){
for(int i=0;i<n;i++)
for(int j=0;j<m;j++){
if(vis[i][j]==-1)
continue;
// printf("%d\n",num[i][j]);
int pos=vis[i][j];
if(str[i][j]=='$'){
mat[pos][pos]=1;
mat[pos][cnt]=0;
continue;
}
mat[pos][pos]=1;
for(int k=0;k<4;k++){
int r=i+dr[k];
int c=j+dc[k];
if(!judge(r,c)) continue;
int t=vis[r][c];
mat[pos][t]=-1.0/num[i][j];
mat[pos][cnt]+=1.0/num[i][j];
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;i++)
scanf("%s",str[i]);
int r,c;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(str[i][j]=='@')
r=i,c=j;
if(!bfs(r,c)){
printf("-1\n");
continue;
}
mm(mat,0);
equ=var=cnt;
build();
if(Gauss()){
printf("%.6f\n",x[0]);
}
else{
printf("-1\n");
}
}
return 0;
}