Where is the canteen Gaussian elimination

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

Mainly recursive iterations bit more

#include<bits/stdc++.h>
#define ls o<<1
#define rs o<<1|1
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
typedef long long LL; 
template<class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template<class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=20,M=0,Z=1e9+7,W=13,L=2e6;
const double eps=1e-10;
const int dy[4]={-1,0,0,1},dx[4]={0,-1,1,0};
using namespace std;
int casenum,casei;
int id;
int n,m,i,j,h,t;
char a[N][N];
int b[N][N]; //how many connects
int vis[N][N]; //the i st reached,
int qy[L],qx[L];// queue
double A[255+5][255+5]; //matrix
int sty,stx;// start  point
bool flag; //reach or not 

//~(-1)=0
void inq(int y,int x){//y i
	if(y<1||y>n||x<1||x>m||a[y][x]=='#'||~vis[y][x])return;
	vis[y][x]=id++;
	if(a[y][x]=='$'){flag=1;return;}
	qy[t]=y;
	qx[t++]=x;
}

void bfs(){
	MS(vis,-1);flag=0;
	h=t=0;inq(sty,stx);
	while(h<t){
		int y=qy[h];
		int x=qx[h++];
		b[y][x]=4;//minus later
		for(int i=0;i<4;i++) {
			if(a[y+dy[i]][x+dx[i]]=='#')b[y][x]--;
			else inq(y+dy[i],x+dx[i]);
		}
	}
}
void build_matrix(){
	MS(A,0);//starts with 0,since id starts with 0
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++)if(~vis[i][j]){//not 0
			int u=vis[i][j];//every point
			A[u][u]=1;// coeffi
			if(a[i][j]=='$')continue;//means 0;
			A[u][id]=1;//every equ same
			double p=1.0/b[i][j];
			for(int k=0;k<4;k++){
				int y=i+dy[k];
				int x=j+dx[k];
				if(~vis[y][x]){
					int v=vis[y][x];
					A[u][v]=-p;
				}
			}
		}
	}
}
void gauss_jordan(int equ,int val){
	int i,j,r,c,maxr;
	//0,0 1,1 2,2
	for(r=c=0;r<equ&&c<val;c++){//down
		maxr=r;
		for(i=r+1;i<equ;i++)if(fabs(A[i][c])>fabs(A[maxr][c]))maxr=i;
		if(fabs(A[maxr][c])<eps)continue;
		if(maxr!=r){
			for(j=c;j<=val;j++)swap(A[maxr][j],A[r][j]);
		}
		for(i=r+1;i<equ;i++)if(fabs(A[i][c])>eps){
			double k=A[i][c]/A[r][c];
			for(j=c;j<=val;j++)A[i][j]-=k*A[r][j];
		}
		r++;
	}
	for(c=val-1;c>=1;c--){
		for(r=c-1;r>=0;r--)if(fabs(A[r][c])>eps){
			A[r][id]-=A[r][c]/A[c][c]*A[c][id];
			//delete get the value for every x
			//you need to get the val by divide
			A[r][c]=0;//upper row
		}
	}
}

int main(){
//	freopen("in.txt","r",stdin);
	while(~scanf("%d%d",&n,&m)) {
		id=0;
		for(i=1;i<=n;i++){
			scanf("%s",a[i]+1);
			for(j=1;j<=m;j++)if(a[i][j]=='@'){
				sty=i;
				stx=j;
			}
		}
		for(i=0;i<=n+1;i++)a[i][0]=a[i][m+1]='#';
		for(j=0;j<=m+1;j++)a[0][j]=a[n+1][j]='#';
		bfs();
		if(flag==0){printf("-1\n");continue;}
		build_matrix();
		gauss_jordan(id,id);
		printf("%.06lf\n",A[0][id]/A[0][0]);
	}
	return 0;
}

 https://blog.csdn.net/snowy_smile/article/details/49452699

Note points

1. fabs () <eps must continue

2. A [] [] represents a coefficient matrix, from 0 memory

3. starts from column 0, each taking a maximum value of the current column's row, the row with the current exchange

4. Each row of column equations is that all the parameters, coefficient of the current column shows a state 1 and all the state probabilities are combined 1

5. When the time '$' point desirably 0

6. Finally, the solution is augmented column to get rid coefficient

7. feeling a little big queue open

8. The number is the number of parameters Solution

9. operator feeling each square with

10. This column coefficient as close to 0, the corresponding solution here might be zero, that part is augmented also likely to be small

Published 810 original articles · won praise 19 · views 60000 +

Guess you like

Origin blog.csdn.net/ujn20161222/article/details/103998585