#include<iostream> using namespace std; int n,m,k; #define max 100 char mmap[max][max]; int mmin; #define MIN(a,b) ((a)<(b)?(a):(b)); int x1,x2,y1,y2; int mmde[4][2]={{0,-1},{1,0},{0,1},{-1,0}}; int visited[max][max]; void dfs(int x,int y,int sum){ if(mmap[x][y]=='3'){ mmin=MIN(sum,mmin); return; } else { int i; int mx,my; for(i=0;i<4;i++){ mx=x+mmde[i][0]; my=y+mmde[i][1]; if(visited[mx][my]==1||mx<1||mx>n||my<1||my>m||(sum%k!=0&&mmap[mx][my]=='0')) {continue;} else if(abs(mx-x2)+abs(my-y2)+sum>mmin)continue; else { visited[mx][my]=1; dfs(mx,my,sum+1); visited[mx][my]=0; } } } } int main(){ //scanf("%d%d%d",&n,&m,&k); while(cin>>n>>m>>k){ int i,j; memset(visited,0,sizeof(visited)); mmin=10000; for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ cin>>mmap[i][j]; if(mmap[i][j]=='1') {x1=i;y1=j;} else if(mmap[i][j]=='3') {x2=i;y2=j;} } } visited[x1][y1]=1; dfs(x1,y1,0); printf("%d\n",mmin); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
today lazy . tomorrow die .