首页 > 其他分享 >HDU 3085 Nightmare Ⅱ

HDU 3085 Nightmare Ⅱ

时间:2022-11-09 20:36:00浏览次数:63  
标签:HDU his return int Nightmare 3085 girl now erriyue


Problem Description

Last 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 are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.
You may 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 are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.


 


Input


The input starts with an integer T, means the number of test cases.
Each test case starts with a line contains two 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 be:
‘.’ denotes an empty place, 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 will contain exactly one letter M, one letter G and two letters Z. 


 



Output


Output a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.


 



Sample Input


3
5 6
XXXXXX
XZ..ZX
XXXXXX
M.G...
......
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G...

10 10
..........
..X.......
..M.X...X.
X.........
.X..X.X.X.
.........X
..XX....X.
X....G...X
...ZX.X...
...Z..X..X

 



Sample Output

1
1
-1


由于数据量大,不能单向,要用双向广搜。

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int sz=805;
const int INF=0x7FFFFFFF;
int T,n,m;
int x[2],y[2],t=0;
char s[sz];
bool mp[sz][sz];
bool f[2][sz][sz];
int d[2][4]={{0,0,1,-1},{1,-1,0,0}};

struct point
{
int x,y;
point(int x=0,int y=0):x(x),y(y){};
};
queue<point> p[2];

bool check(int X,int Y)
{
return min(abs(X-x[0])+abs(Y-y[0]),abs(X-x[1])+abs(Y-y[1]))>2*t;
}

bool bfs(int now)
{
int tot=p[now].size();
while (tot--)
{
point q=p[now].front(); p[now].pop();
if (!check(q.x,q.y)) continue;
for (int i=0;i<4;i++)
{
int X=q.x+d[0][i],Y=q.y+d[1][i];
if (mp[X][Y]&&check(X,Y)&&!f[now][X][Y])
{
f[now][X][Y]=true;
p[now].push(point(X,Y));
if (f[now^1][X][Y]) return true;
}
}
}
return false;
}

int work()
{
for (;!p[0].empty()||!p[1].empty();t++)
{
if (bfs(0)) return t;
if (bfs(0)) return t;
if (bfs(0)) return t;
if (bfs(1)) return t;
}
return -1;
}

int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
t=-1;
memset(f,0,sizeof(f));
memset(mp,0,sizeof(mp));
for (int i=0;i<=1;i++) while (!p[i].empty()) p[i].pop();
for (int i=1;i<=n;i++)
{
scanf("%s",s+1);
for (int j=1;j<=m;j++)
{
if (s[j]=='M'||s[j]=='G')
{
p[s[j]=='G'].push(point(i,j));
f[s[j]=='G'][i][j]=true;
s[j]='.';
}
if (s[j]=='Z') x[++t]=i,y[t]=j;
mp[i][j]=s[j]=='.';
}
}
printf("%d\n",work());
}
return 0;
}



标签:HDU,his,return,int,Nightmare,3085,girl,now,erriyue
From: https://blog.51cto.com/u_15870896/5838731

相关文章

  • HDU 3313 Key Vertex
    ProblemDescriptionYouneedwalkingfromvertexStovertexTinagraph.IfyouremoveonevertexwhichstopsyoufromwalkingfromStoT,thatverte......
  • HDU 3316 Mine sweeping
    ProblemDescriptionIthinkmostofyouareusingsystemnamedofxporvistaorwin7.Andthesesystemisconsistofafamousgamewhatisminesweeping......
  • HDU 3355 Hop — Don’t Walk!
    ProblemDescriptionKERMITTHEFROGisaclassicvideogamewithasimplecontrolandobjectivebutrequiresagooddealofthinking.Youcontrolanani......
  • HDU 3427 Clickomania
    ProblemDescriptionClickomaniaisapuzzleinwhichonestartswitharectangulargridofcellsofdifferentcolours.Ineachstep,aplayerselects("......
  • HDU 2209 翻纸牌游戏
    ProblemDescription有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌。但是麻烦的是,......
  • HDU 2216 Game III
    ProblemDescriptionZjtandSarawilltakepartinagame,namedGameIII.ZjtandSarawillbeinamaze,andZjtmustfindSara.Therearesomestrang......
  • 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......