Nyoj 999--Master was taken by the monster —————— "double-wide Search"

Source: Internet
Author: User

The master was taken away by the monster. Time limit:MS | Memory limit:65535 KB Difficulty:3
Describe

Words Tang got Sun Xing, the master and Apprentice are bent on the same body, a total of Western. Since the Baochang saved the princess, Monarch sent out of the West, along the road Shing, Wu empty for the master to Huazai, and so Wu empty back, Wu empty said: "Not good, bad", not so that the net said, Wu empty said: "Master was taken by the Devil", Wu-Net: "no! , Wu empty a face dazed, Wu Net: "Master and two seniors have been taken by the monster." Goku (Halo!) )。 In order to prevent Goku to save, the Devil first put Tang and eight precepts respectively hid, if wukong in T minutes after not found people, it must be eaten by the monster. Suppose Wukong in a N-row m-column of the Matrix, Wukong in every minute can go up, down, left, right one of the places can walk, each can only go one step at a time. We define the discovery as being able to see each other directly, meaning that two people are in the same row or column, and there are no obstructions in the middle or no one else can see each other.

Input
There are several sets of test data, each group is first three positive integers n, m (3<=n,m<=100), T, (0<=t<=100) represent the number of rows, number of columns, the time specified. Next n lines, m characters per line. Where ' S ' stands for Goku's position, ' D ' stands for Master's position, ' E ' represents the position of the eight commandments. And there's only one guarantee. ' X ' stands for the wall, '. ' stands for open space.
Output
each group first outputs a line case C: (c indicates the current number of groups, counting from 1);
The next line, if Wukong can find two people within the specified time, then output the minimum time required, otherwise output-1.
Sample input
5 6 3 XXD .... E..... X..... S ..... 5 6 3 XDX .... E........... S ..... 5 6 8 XXDX. . XEX. ...... .... S .....
Sample output
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.. */

  




Nyoj 999--Master was taken by the monster —————— "double-wide Search"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.