HDU 2216 Game III (BFS)

Source: Internet
Author: User

Problem Descriptionzjt and Sara'll take part in a game, named game Iii. Zjt and Sara'll be in a maze, and ZJT must find Sara. There is some Strang rules in this maze. If ZJT Move a step, Sara would move a step in opposite direction.
Now give your the map, you shold find out the minimum steps, ZJT has to move. We say ZJT meet Sara, if they is in the same position or they is adjacent.
ZJT can only move to a empty position int four diraction (up, left, right, down). At the same time, Sara would move to a position in opposite direction, if there is empty. Otherwise, she'll not move to any position.
The map is a n*m two-dimensional array. The position zjt stays are marked Z, and the position, where Sara stays, is marked E.

>. : Empty position
> X:the Wall
> z:the position ZJT now stay
> s:the position Sara now stay

Your task is to find out the the minimum steps they meet each other.

Inputthe input contains several test cases. Each test case starts with a line contains three number N, M (2<= N <=, 2 <= M <=) indicate the size of The map. Then N lines follows, each line contains M character. A Z and a S would be in the map as the discription above.

Outputfor Each test case, you should print the minimum steps. "Bad luck!" would be the print, if they can ' t meet each of the other.

Sample Input4 4XXXX. Z... Xs. XXXX4 4XXXX. Z... X.sxxxx4 4XXXX. Zx.. Xs. Xxxx

Sample Output11bad luck!

The problem: This BFS is very interesting, different from the typical topic, its target point is moving. When Z is moving, S moves in the opposite direction (if active). That is, led to WA two times, vis array only open two-dimensional to record Z has not traversed, and then read the puzzle is to open four-dimensional array, save Z, S. Then the third set of examples of a run out, found to determine whether the access statement to determine whether the s move after the line.

#include <cstdio>#include<iostream>#include<string>#include<sstream>#include<cstring>#include<stack>#include<queue>#include<algorithm>#include<cmath>#include<map>#definePI ACOs (-1.0)#defineMS (a) memset (A,0,sizeof (a))#defineMSP Memset (Mp,0,sizeof (MP))#defineMSV memset (vis,0,sizeof (VIS))using namespacestd;//#define LOCALintn,m;Charmp[ A][ A];BOOLvis[ A][ A][ A][ A];intdir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};structnode{intZx,zy; intSx,sy; intStep;} T,nn;intBFs () {Queue<Node>Q;  while(!q.empty ()) Q.pop (); Vis[t.zx][t.zy][t.sx][t.sy]=1; T.step=0;    Q.push (t);  while(!Q.empty ()) {T=Q.front (), Q.pop (); if(T.sx==t.zx&&abs (t.sy-t.zy) = =1)returnT.step; if(T.sy==t.zy&&abs (T.SX-T.ZX) = =1)returnT.step; if(T.sx==t.zx&&t.sy==t.zy)returnT.step;  for(intI=0;i<4; i++) {NN.ZX=t.zx+dir[i][0]; Nn.zy=t.zy+dir[i][1]; if(mp[nn.zx][nn.zy]=='X')Continue; if(nn.zx<0|| nn.zx>=n| | nn.zy<0|| NN.ZY&GT;=M)Continue; NN.SX=t.sx-dir[i][0]; Nn.sy=t.sy-dir[i][1]; if(nn.sx<0|| nn.sx>=n| | nn.sy<0|| NN.SY&GT;=M) nn.sx=t.sx,nn.sy=T.sy; if(mp[nn.sx][nn.sy]=='X') nn.sx=t.sx,nn.sy=T.sy; Nn.step=t.step+1; if(Vis[nn.zx][nn.zy][nn.sx][nn.sy])Continue; Vis[nn.zx][nn.zy][nn.sx][nn.sy]=1;        Q.push (NN); }    }    return-1;}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif //LOCALIos::sync_with_stdio (false);  while(cin>>n>>m) {msv;  for(intI=0; i<n;i++) cin>>Mp[i];  for(intI=0; i<n;i++)         for(intj=0; j<m;j++)        {            if(mp[i][j]=='Z') t.zx=i,t.zy=J; Else if(mp[i][j]=='S') t.sx=i,t.sy=J; }        intans=BFS (); if(ans==-1) printf ("Bad luck!\n"); Elseprintf"%d\n", ans); }    return 0;}

HDU 2216 Game III (BFS)

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.