Case 1:-1 Case 2:3 case 3:-1
Problem-solving ideas: First, the figure processing, processing can be expressed at some point can find someone or two people. Then, after searching for someone, search for another one or two at the same time. If one of them is found, then the other person is searched. This is double-wide search, first search an intermediate result, and then on this basis to search for another unfinished results. However, it is important to note that the best results are not first searched. So we need to find a successful search of the two paths are searched again. The minimum value is taken.
#include <bits/stdc++.h>using namespace Std;char map[105][105];int mark[105][105];int vis[105][105];int f[4][2] ={{0,1},{1,0},{0,-1},{-1,0}};int n,m,ans,t;const int inf=1e9;struct monkey{int x, y; int step;}; BOOL Cantravle (int ti,int TJ) {//Determine if you can walk if (map[ti][tj]== ' X ' | | map[ti][tj]== ' D ' | | map[ti][tj]== ' E ') {return 0; }else{return 1; }}char Trans (int i,int j,int CAs) {//conversion character if (cas&&map[i][j]== ' E ' | | (!cas&&map[i][j]== ' d ')) {return ' o '; } return CAS? ' d ': ' E ';} void change (int si,int sj,int CAs) {//D, E where the row can go to D and E, the intersection point to O int i,j; For (I=si+1;i<=n&&cantravle (I,SJ); i++) Map[i][sj]=trans (I,sj,cas); For (I=si-1;i>=1&&cantravle (I,SJ); i--) Map[i][sj]=trans (I,sj,cas); For (J=sj+1;j<=m&&cantravle (SI,J); j + +) Map[si][j]=trans (Si,j,cas); For (J=sj-1;j>=1&&cantravle (SI,J); j--) Map[si][j]=trans (Si,j,cas);} Queue<monkey>q;queue<monkey>qde;iNT BFSDE (int CAs) {//Find another Monkey st,tmp after finding the eight commandments or master; while (! Qde.empty ()) {St=qde.front (); Qde.pop (); if (st.step>t) continue; if (cas&&map[st.x][st.y]== ' E ' | | (!cas&&map[st.x][st.y]== ' d ') | | map[st.x][st.y]== ' O ') {return st.step; } int i,j,tx,ty,tsp; for (i=0;i<4;i++) {tx=st.x+f[i][0]; TY=ST.Y+F[I][1]; if (tx<=n&&tx>=1&&ty<=m&&ty>=1&& (!vis[tx][ty]) &&CanTravle ( Tx,ty)) {tsp=st.step+1; Vis[tx][ty]=1; TMP.X=TX,TMP.Y=TY,TMP.STEP=TSP; Qde.push (TMP); }}} return INF;} void BFS () {//Find eight commandments or master or find two people Monkey st,tmp at the same time; Ans=inf; while (! Q.empty ()) {St=q.front (); Q.pop (); if (st.step>t) continue; if (map[st.x][st.y]== ' O ') {ans=min (ans,st.step); }else if (map[st.x][st.y]== ' E ') {while(! Qde.empty ()) Qde.pop (); Qde.push (ST); memset (vis,0,sizeof (VIS)); Vis[st.x][st.y]=1; Ans=min (ANS,BFSDE (0)); }else if (map[st.x][st.y]== ' d ') {while (! Qde.empty ()) Qde.pop (); Qde.push (ST); memset (vis,0,sizeof (VIS)); Vis[st.x][st.y]=1; Ans=min (ANS,BFSDE (1)); } int tx,ty,tsp,i,j; for (i=0;i<4;i++) {tx=st.x+f[i][0]; TY=ST.Y+F[I][1]; if (Tx>=1&&tx<=n&&ty>=1&&ty<=m&&cantravle (tx,ty) && (!mark[tx ][ty]) {mark[tx][ty]=1; tsp=st.step+1; TMP.X=TX,TMP.Y=TY,TMP.STEP=TSP; Q.push (TMP); }}}}int Main () {int i,j,k,cnt=0; Monkey St; while (scanf ("%d%d%d", &n,&m,&t)!=eof) {memset (mark,0,sizeof (Mark)); for (i=1;i<=n;i++) {for (j=1;j<=m;j++) { cin>>map[i][j]; if (map[i][j]== ' S ') {st.x=i; St.y=j; St.step=0; }}} for (i=1;i<=n;i++) {//processing diagram for (j=1;j<=m;j++) {if (map[i][j]== ' D ') {change (i,j,1); }else if (map[i][j]== ' E ') {change (i,j,0); }}} while (! Q.empty ()) Q.pop (); Q.push (ST); Mark[st.x][st.y]=1; BFS (); if (ans>t) {printf ("Case%d:\n-1\n", ++cnt); }else{printf ("Case%d:\n%d\n", ++cnt,ans); }} return 0;} /*5 6 30XDX ..... E.... S ..... ..... 7 6 5.......d.x. x.x ... X..... x.x .... E....... S.. */