#include<stdio.h>//dfs #include<stdlib.h> #include<string.h> #define INF 9999999 int a[101][101],book[101][101]; int p=4,q=3,mmin=INF,x=1,sy=1,n=5,m=4; void dfs(int x,int y,int step) { int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int tx,ty,k; if(x==p && y==q) { //step=-1; if(step<mmin) mmin=step; return; } for(k=0;k<3;k++) { tx=x+next[k][0]; ty=y+next[k][1]; if(tx<1 || tx>n || ty<1 || ty>m ) continue; if(a[tx][ty]==0&&book[tx][ty]==0) { book[tx][ty]=1; dfs(tx,ty,step+1); book[tx][ty]=0; } } return; } int main() { FILE *fin,*fout; fin=fopen("in.txt","r"); fout=fopen("out.txt","w"); int i,j; //memset(a,0,sizeof(a)); //scanf("%d%d",n,m); for(i=1;i<=5;i++) for(j=1;j<=4;j++) fscanf(fin,"%d",&a[i][j]); book[1][1]=1; dfs(1,1,0); fprintf(fout,"%d",mmin); fclose(fin); fclose(fout); return 0; }