HDU 3085 Nightmareⅱ "BFS + Manhattan distance + Comprehensive Strong"

Source: Internet
Author: User

Nightmareⅱtime limit:2000/1000ms (java/other) Memory limit:32768/32768k (Java/other) total submission (s): Acce pted Submission (s): 6Font:Times New Roman|Verdana|GeorgiaFont Size:←→Problem Descriptionlast night, little Erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there is the ghosts in the maze. They'll kill the people. Now little Erriyue wants to know if he could find his girl friend before the ghosts find them.
Suppose that little Erriyue and his girl friend can move in 4 directions. In each second, little Erriyue can move 3 steps and his girl friend can move 1 step. The Ghosts is evil, every second they would divide into several parts to occupy the grids within 2 steps to them until the Y occupy the whole maze. You can suppose in every second the ghosts divide firstly then the little Erriyue and he girl friend start to move, And if little Erriyue or his girl friend arrive in a grid with a ghost, they would die.
Note:the New Ghosts also can devide as the original ghost.
Inputthe input starts with an integer T, means the number of test cases.
Each test case starts with a line contains the integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may:
'. ' Denotes an empty place and all can walk on.
' X ' denotes a wall, only people can ' t walk on.
' M ' denotes little Erriyue
' G ' denotes the girl friend.
' Z ' denotes the ghosts.
It is guaranteed that would contain exactly one letter M, one letter G and one letters Z.
Outputoutput A single integer s on one line, denotes Erriyue and his girlfriend would meet in the minimum time s if they CA N meet successfully, or output-1 denotes they failed to meet. Sample Input
6XXXXXXXZ.. Zxxxxxxxm. G......... 5 6XXXXXXXZZ.. Xxxxxxxm ..... G... 10............X ..... M.x ... x.x ..... X.. x.x.x ..... X.. Xx.... x.x .... G... X... Zx. X...... Z.. X.. X
Sample Output
11-1


#include <stdio.h> #include <queue> #include <math.h> #include <string.h> #include <stdlib.h >using namespace Std;char map[810][810];int step[4][2]= {1,0,-1,0,0,1,0,-1};struct node{int x, y;} M,g,z[2];int num_of_step,n,m;queue<node> Q[3];bool Ghost (Node A) {for (int i=0; i<2; i++) {if (ABS (a) x-z[i].x) +abs (a.y-z[i].y) <= 2*num_of_step)//Return false;//is caught} return true; BOOL Check (Node B) {if (b.x>=0&&b.y>=0&&b.x<n&&b.y<m&&map[b.x][b.y]!= '    X ') return true; return false;}    BOOL BFS (int people,int time,int start,int endd) {node cur,next;    Q[2]=q[people];            for (int i=0, i<time; i++) {while (!q[2].empty ()) {Cur=q[2].front ();            Q[2].pop ();            Q[people].pop ();                     if (Ghost (cur))//If caught {for (int i=0; i<4; i++) {next=cur; next.x+=step[I][0];                    NEXT.Y+=STEP[I][1]; if (Ghost (next) &&check (next) &&map[next.x][next.y]!=start) {if (MA                        P[NEXT.X][NEXT.Y]==ENDD) return true;                        Map[next.x][next.y]=start;                    Q[people].push (next);    }}}} Q[2]=q[people]; } return false;}    int solve () {for (Int. i=0; i<3; i++)////clear {while (!q[i].empty ()) Q[i].pop ();    } num_of_step=0;    Q[0].push (M);    Q[1].push (G);        while (!q[0].empty () &&!q[1].empty ())////{num_of_step++; if (BFS (0,3, ' M ', ' G ') | |    BFS (////, ' G ', ' M ')) return num_of_step; } return-1;}    int main (void) {int N;    scanf ("%d", &n);        while (n--) {int cnt=0;        scanf ("%d%d", &n,&m);        GetChar (); for (int i=0; i<n; i++) {scanf ("%S ", Map[i]);                for (int ii=0; ii<m; ii++) {if (map[i][ii]== ' m ') m.x=i,m.y=ii;                else if (map[i][ii]== ' G ') g.x=i,g.y=ii;            else if (map[i][ii]== ' Z ') z[cnt].x=i,z[cnt].y=ii,++cnt;        }}//getchar ();    printf ("%d\n", Solve ()); } return 0;}


.. X......... M.x ... x.x ..... X.. x.x.x ..... X.. Xx.... x.x .... G... X... Zx. X...... Z.. X.. X
Sample Output
11-1

HDU 3085 Nightmareⅱ "BFS + Manhattan distance + Comprehensive Strong"

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.