1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| const int INF=0x3f3f3f3f; const int MAX=100+10; int n,m,fx,fy; bool vis[MAX][MAX]; char a[MAX][MAX]; bool judge(int x,int y){ if(x<1 || x>n || y<1 ||y>m) return false; return true; } struct node{ int x,y,f,step; node(int _x,int _y,int _f,int _step){ x=_x;y=_y;f=_f;step=_step; } }; int bfs(int x,int y,int f){ queue<node> q; q.push(node(x,y,f,0)); mem(vis,0); vis[x][y]=1; int cnt =0 ,tx,ty; while(!q.empty()){ node t= q.front(); q.pop(); if(!(t.x == fx && t.y == fy) && a[t.x][t.y]=='C') return t.step; if(t.f==1){ tx = t.x -1 ,ty = t.y; while(judge(tx,ty) && !vis[tx][ty] && a[tx][ty]!='*'){ q.push(node(tx,ty,2,t.step+1)); vis[tx][ty]=1; tx--; } tx = t.x +1 ; while(judge(tx,ty)&& !vis[tx][ty] && a[tx][ty]!='*'){ q.push(node(tx,ty,2,t.step+1)); vis[tx][ty]=1; tx++; } }else { tx = t.x ,ty = t.y - 1; while(judge(tx,ty) && !vis[tx][ty] && a[tx][ty]!='*'){ q.push(node(tx,ty,1,t.step+1)); vis[tx][ty]=1; ty--; } ty = t.y +1 ; while(judge(tx,ty) && !vis[tx][ty] && a[tx][ty]!='*'){ q.push(node(tx,ty,1,t.step+1)); vis[tx][ty]=1; ty++; } } } return INF; } int main(){ freopen("in","r",stdin); sf(m);sf(n); fi(i,1,n+1) scanf("%s",a[i]+1); fi(i,1,n+1) fi(j,1,m+1) if(a[i][j]=='C') { fx = i; fy = j; } pf(min(bfs(fx,fy,1),bfs(fx,fy,2))-1); return 0; }
|