首页 > 其他分享 >HDU 2216 Game III

HDU 2216 Game III

时间:2022-11-09 20:32:37浏览次数:45  
标签:HDU 2216 int Sara YY XX position Zjt III


Problem Description


Zjt and Sara will take part in a game, named Game III. Zjt and Sara will be in a maze, and Zjt must find Sara. There are some strang rules in this maze. If Zjt move a step, Sara will move a step in opposite direction.
Now give you the map , you shold find out the minimum steps, Zjt have to move. We say Zjt meet Sara, if they are in the same position or they are adjacent . 
Zjt can only move to a empty position int four diraction (up, left, right, down). At the same time, Sara will move to a position in opposite direction, if there is empty. Otherwise , she will not move to any position.
The map is a N*M two-dimensional array. The position Zjt stays now is 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 minimum steps they meet each other.


 



Input


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


 



Output


For each test case, you should print the minimum steps. “Bad Luck!” will be print, if they can't meet each other.


 



Sample Input


4 4
XXXX
.Z..
.XS.
XXXX
4 4
XXXX
.Z..
.X.S
XXXX
4 4
XXXX
.ZX.
.XS.
XXXX



Sample Output


1
1
Bad Luck!

bfs标记全部的可能

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=25;
int n,m,bx,by,ex,ey;
char s[N];
int mp[N][N];
int f[N][N][N][N];

struct point
{
int a,b,c,d;
point(int a=0,int b=0,int c=0,int d=0):a(a),b(b),c(c),d(d){};
};

int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

int main()
{
while (~scanf("%d%d",&n,&m))
{
memset(mp,0,sizeof(mp));
memset(f,-1,sizeof(f));
for (int i=1;i<=n;i++)
{
scanf("%s",s+1);
for (int j=1;j<=m;j++)
{
if (s[j]=='Z') {s[j]='.'; bx=i; by=j; }
if (s[j]=='S') {s[j]='.'; ex=i; ey=j; }
mp[i][j]=s[j]=='.';
}
}
queue<point> p;
p.push(point(bx,by,ex,ey));
f[bx][by][ex][ey]=0;
int flag=0;
while (!p.empty())
{
point q=p.front(); p.pop();
if (abs(q.a-q.c)+abs(q.b-q.d)<=1)
{
printf("%d\n",f[q.a][q.b][q.c][q.d]);
flag=1; break;
}
for (int i=0;i<4;i++)
{
int X=q.a+d[i][0],Y=q.b+d[i][1];
if (!mp[X][Y]) continue;
int XX=q.c+d[i^1][0],YY=q.d+d[i^1][1];
if (mp[XX][YY])
{
if (f[X][Y][XX][YY]==-1)
{
p.push(point(X,Y,XX,YY));
f[X][Y][XX][YY]=f[q.a][q.b][q.c][q.d]+1;
}
}
else
{
XX=q.c; YY=q.d;
if (f[X][Y][XX][YY]==-1)
{
p.push(point(X,Y,XX,YY));
f[X][Y][XX][YY]=f[q.a][q.b][q.c][q.d]+1;
}
}
}
}
if (!flag) printf("Bad Luck!\n");
}
return 0;
}



标签:HDU,2216,int,Sara,YY,XX,position,Zjt,III
From: https://blog.51cto.com/u_15870896/5838737

相关文章

  • HDU 1010 Tempter of the Bone
    DescriptionThedoggiefoundaboneinanancientmaze,whichfascinatedhimalot.However,whenhepickeditup,themazebegantoshake,andthedoggie......
  • HDU 1017 A Mathematical Curiosity
    DescriptionGiventwointegersnandm,countthenumberofpairsofintegers(a,b)suchthat0<a<b<nand(a^2+b^2+m)/(ab)isaninteger. Thi......
  • HDU 5605 geometry
    ProblemDescriptionThereisapoint  atcoordinate .Alinegoesthroughthepoint,andintersectswiththepostivepartof  axesatpoint .Plea......
  • HDU 1329 Hanoi Tower Troubles Again!
    DescriptionPeoplestoppedmovingdiscsfrompegtopegaftertheyknowthenumberofstepsneededtocompletetheentiretask.Butontheotherhand,they......
  • HDU 2475 Box
    DescriptionThereareNboxesontheground,whicharelabeledbynumbersfrom1toN.Theboxesaremagical,thesizeofeachonecanbeenlargedorreduc......
  • HDU 5432 Pyramid Split
    ProblemDescriptionXiaoMingisacitizenwho'sgoodatplaying,hehaslot'sofgoldconeswhichhavesquareundersides,let'scallthempyramids.Anyo......
  • HDU 5496 Beauty of Sequence
    ProblemDescriptionSequenceisbeautifulandthebeautyofanintegersequenceisdefinedasfollows:removesallbutthefirstelementfromeveryconse......
  • HDU 5433 Xiao Ming climbing
    ProblemDescriptionDuetothecursemadebythedevil,XiaoMingisstrandedonamountainandcanhardlyescape.Thismountainisprettystrangethat......
  • HDU 1542 Atlantis
    ProblemDescriptionThereareseveralancientGreektextsthatcontaindescriptionsofthefabledislandAtlantis.Someofthesetextsevenincludemaps......
  • HDU 3308 LCIS
    ProblemDescriptionGivennintegers.Youhavetwooperations:UAB:replacetheAthnumberbyB.(indexcountingfrom0)QAB:outputthelength......