Zoj 3478 Binary Land BFS

Source: Internet
Author: User

Links: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3478


Binary Land

Time limit: 2 Seconds Memory Limit: 65536 KB

Binary Land is a very Simple action title for the , Famicom  with an interesting twist. You control, not one, but the Penguins, Gurin (male) and Malon (female), who is in Love with and all other. While your is in direct control of one penguin, you indirectly control the other penguin, who moves in a mirror image fash Ion from the original Penguin. When you move your penguin left, the other moves right, and vice versa. The goal of the game is to move both penguins from their own origins to the unique caged heart at the same time.

At the beginning for each stage, you can choose which penguin to control directly. There is many walls which stop penguins from moving into them. If a penguin trys to does so, it'll remain in the original grid. Besides the caged heart and walls, spider webs is strewn about in the 10x15 maze. If one of the Penguins hits the web, it'll become stuck in place until the other penguin moves to its neighbour and help s The stuck penguin move from the spider web to its grid. This process costs one seconds. Generally, it costs 5 seconds to move both penguins simultaneously. But when one of the penguins are stuck, moving the penguin which you directly control takes 2 seconds and while moving the oth Er penguin takes 3 seconds.

What ' s the earliest time, Gurin and Malon can meet at the caged heart?

Binary Land Sample 1 Sample 2
Sample 3 Sample 4 Sample 5
Input

There is multiple test cases. The first line of input was an integer T ≈100 indicating the number of test cases.

Each test case contains lines. The first lines was the 10x15 maze, the 11th line was the origin of Gurin, the 12th line is the origin of Malon, and the 13th line was always a blank line. The maze is made up of the unique caged heart (' H '), spider webs (' O '), Walls (' X '), and empty grids ('. '). The origins of penguins cannot be walls.

Output

For each test case, the output of the earliest time, or-1 when it ' s impossible.

Sample Input
5.......H ..... XXX.XXX.XXX. Xxxx. X.x.xxx. x.x ... O.... X........ Xxxxx. X.xxxxx ..... X.... O.. Xx. X.x.x.x.x.xx ..... X........ Xxxxxxxxxxxxx ..... X....... 1 151 1.......H ..... X.x.xxxxx. x.x. X.x.x.x.xxxxx .... X.. OX ... X.... Xxx. Xxxxx. X.xx ..... X....... Xx.xxx. X.xxxxx. O.. X... X........ X.x.x.xxx. X.xx ..... x.x ..... Ten 910 7...XO.. H....... OX. X.x.x.x.x.xxoxox. XOX. X.x.x.ox ... X.xo. X.. O.xxxxxxx.xxx. Xx. O.xo. X.. O..... X.x.x.xxx.xxx. OX. X.xoxo. X.O. XOX. x.x.x.x.x. X... x.x ..... x.10 910 7.......H ..... X.. Xxx. Xxxxx. x.x.xx. x.x.x.x.x.x ..... Xo.. X.... XXXXX.XXX.XXX ..... x.x ... X.x.xxx.xxx. Xx....... X... X.. Oxxxxxx. x.x.x.x ..... x.x ..... 1 101 4.......H ..... x.x.x.x.x.x.x. x.x.x.x.x.x.x ..... X........ x.x.x.x.x.x.x ..... X........ x.x.x.x.x.x.x ..... X........ x.x.x.x.x.x.x ..... OX ..... 10 110 7
Sample Output
35140-14099

Test instructions: give you the map ' X ' is the wall, '. ' Is the road, and the coordinates of two male and female penguins, starting two penguins will not be in the wall. If they are not trapped, or do not go to the edge of the map, or not blocked by the wall, then are synchronized to go, up and down with the same phase, left and right to go backwards. The minimum time to go to the H point. It takes 5s to walk at the same time. There is a spider's Web ' O ', if one is trapped, then the other may be a stupid penguin, may be a smart penguin, smart Step-by Time of 2s, stupid 3s. If a penguin walks next to another penguin, it can save the other one and put him in his place. Time consuming 11s.

If all two penguins are trapped, they fail.



Practice:

BFS, because do not know which is only stupid, so to BFS two times. Play more trouble, pay attention to all kinds of situation is good.

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> #define INF 999999999#define EPS 0.00001#define LL __ Int64d#define Pi ACOs ( -1.0)//10x15 maze, int dir[4][2][2]={1,0,1,0,-1,0,-1,0,0,1,0,-1,0,-1,0,1};//One-dimensional four directions two-dimensional which penguin Three-dimensional Xychar mp[10][15];int vis[10][15][10][15];//-1 represents not traversed, not-1 represents the minimum number of steps to reach this state struct point{int x1,y1;int x2,y2;int bu; BOOL operator < (const point &b) Const{return bu>b.bu;}}; int ok (int x,int y) {if (x>=0&&x<10&&y>=0&&y<15&&mp[x][y]!= ' x ') return 1; return 0;} Point Walk (Point nw,int t,int Zou)//walk, {int xx1,xx2,yy1,yy2;if (zou&1) {xx1=nw.x1+dir[t][0][0];yy1=nw.y1+dir[t][0] [1];if (OK (xx1,yy1)) {nW.x1=xx1;nw.y1=yy1;}} if (zou&2) {xx2=nw.x2+dir[t][1][0];yy2=nw.y2+dir[t][1][1];if (ok (xx2,yy2)) {nw.x2=xx2;nw.y2=yy2;}} return NW;} int low (point gg) {if (vis[gg.x1][gg.y1][gg.x2][gg.y2]==-1| | VIS[GG.X1][GG.Y1][GG.X2][GG.Y2]&GT;GG.BU)//Determine whether to update vis, you can go, return 1{vis[gg.x1][gg.y1][gg.x2][gg.y2]=gg.bu; return 1;} return 0;} int near (point GG)//Determine if two penguins are adjacent {if (ABS (GG.X1-GG.X2) +abs (gg.y2-gg.y1) ==1) return 1;return 0;} int BFS (point gg) {priority_queue<point> Q;gg.bu=0;q.push (GG);p oint nw;point Tem;low (GG), while (!q.empty ()) {nw= Q.top (); Q.pop (); if (nw.x1==nw.x2&&nw.y1==nw.y2&&mp[nw.x1][nw.y1]== ' H ')//End return Nw.bu;if (mp[ nw.x1][nw.y1]== ' o ' &&mp[nw.x2][nw.y2]== ' o ')//are trapped continue;int xx1,xx2,yy1,yy2;if (mp[nw.x1][nw.y1]== ' o ')// 1 Trapped {if (near (NW))//adjacent, 2 save 1{tem=nw;tem.bu=nw.bu+11;tem.x1=tem.x2;tem.y1=tem.y2, if (Low (TEM)) Q.push (TEM);} for (int i=0;i<4;i++) {tem=walk (nw,i,2), tem.bu=nw.bu+3;if (Low (TEM)) Q.push (TEM);}} else if (mp[nw.x2][nw.y2]== ' O ')//1hao 2s{if (near (NW)) {tem=nw;tem.bu=Nw.bu+11;tem.x2=tem.x1;tem.y2=tem.y1;if (Low (TEM)) Q.push (TEM);} for (int i=0;i<4;i++) {tem=walk (nw,i,1), tem.bu=nw.bu+2;if (Low (TEM)) Q.push (TEM);}}  else {for (int i=0;i<4;i++) {tem=walk (nw,i,3), tem.bu=nw.bu+5;if (Low (TEM)) Q.push (TEM);} }}return INF;} int main () {int T;cin>>t;while (t--) {for (int i=0;i<10;i++) {scanf ("%s", Mp[i]);} memset (vis,-1,sizeof Vis);p oint gg;cin>>gg.x1;gg.x1--; cin>>gg.y1;gg.y1--;cin>>gg.x2;gg.x2--; Cin>>gg.y2;gg.y2--;int ANS=BFS (GG), swap (GG.X1,GG.X2), swap (Gg.y1,gg.y2), Ans=min (ANS,BFS (GG)), if (Ans==inf) cout<< "-1" &LT;&LT;ENDL;ELSECOUT&LT;&LT;ANS&LT;&LT;ENDL;} return 0;}



Zoj 3478 Binary Land 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.