Time Limit: 1000MS | Memory Limit: 65535KB | 64bit IO Format: %lld & %llu |
Description
小Q被邪恶的大魔王困在了迷宫里,love8909决定去解救她。
迷宫里面有一些陷阱,一旦走到陷阱里,就会被困身亡:(,迷宫
里还有一些古老的传送阵,一旦走到传送阵上,会强制被传送到
传送阵的另一头。
现在请你帮助love8909算一算,他至少需要走多少步才能解
救到小Q?
Input
第一行为一个整数$T$,表示测试数据组数。
每组测试数据第一行为两个整数$N$,$M$,($1\leq N, M\leq 50$)表示
迷宫的长和宽。
接下来有$N$行,每行$M$个字符,是迷宫的具体描述。
.
表示安全的位置#
表示陷阱,Q
表示小Q的位置L
表示love8909所在位置,
数据保证love8909只有一个,数据也保证小Q只有一个。
小写字母a
-z
表示分别表示不同的传送阵,数据保证传送阵
两两配对。
Output
每组数据输出一行,解救小Q所需的最少步数,如果无论如何都
无法救小Q,输出-1
。
Sample Input
2 5 5 ....L .###. b#b#a ##.## ...Qa 5 5 ....L .###. .#.#. ##.## ...Q.
Sample Output
3
-1
Source
广搜思想在处理传送门的时候要用一个结构体来实现转换,思维比较巧妙
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char str[100][100];
int visit[100][100];
struct node
{
int x1, y1, x2, y2, date;
}men[200];
struct cur
{
int x, y, step;
};
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int sx, sy, m, n;
int bfs();
int judge(int x,int y)
{
if(x>=0&&x<m&&y>=0&&y<n&&visit[x][y]==0&&str[x][y]!='#')
return 1;
else
return -1;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &m, &n);
memset(visit,0,sizeof(visit));
memset(men,0,sizeof(men));
for(int i=0;i<m;i++)
{
scanf(" %s",str[i]);
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(str[i][j]<='z'&&str[i][j]>='a')
{
int k=str[i][j]-'0';
if(men[k].date==0)
{
men[k].x1=i,men[k].y1=j;
men[k].date=1;
}
else
{
men[k].x2=i,men[k].y2=j;
}
}
else if(str[i][j]=='L')
{
sx=i,sy=j;
}
}
}
if(bfs()==1)
{
printf("-1\n");
}
}
return 0;
}
int bfs()
{
struct cur e;
e.x=sx, e.y=sy, e.step=0;
queue<cur>q;
q.push(e);
while(!q.empty())
{
e=q.front();
q.pop();
struct cur p;
if(str[e.x][e.y]=='Q')
{
printf("%d\n",e.step);
return -1;
}
else
{
for(int i=0;i<4;i++)
{
int x=e.x+dir[i][0],y=e.y+dir[i][1];
if(judge(x,y)==1)
{
if(str[x][y]>='a'&&str[x][y]<='z')
{
int k=str[x][y]-'0';
if(men[k].x1==x&&men[k].y1==y)
{
p.x=men[k].x2,p.y=men[k].y2;
p.step=e.step+1;
q.push(p);
}
else
{
p.x=men[k].x1,p.y=men[k].y1;
p.step=e.step+1;
q.push(p);
}
}
else
{
p.x=x,p.y=y,p.step=e.step+1;
q.push(p);
}
visit[x][y]=1;
}
}
}
}
return 1;
}
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char str[100][100];
int visit[100][100];
struct node
{
int x1, y1, x2, y2, date;
}men[200];
struct cur
{
int x, y, step;
};
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int sx, sy, m, n;
int bfs();
int judge(int x,int y)
{
if(x>=0&&x<m&&y>=0&&y<n&&visit[x][y]==0&&str[x][y]!='#')
return 1;
else
return -1;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d %d", &m, &n);
memset(visit,0,sizeof(visit));
memset(men,0,sizeof(men));
for(int i=0;i<m;i++)
{
scanf(" %s",str[i]);
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(str[i][j]<='z'&&str[i][j]>='a')
{
int k=str[i][j]-'0';
if(men[k].date==0)
{
men[k].x1=i,men[k].y1=j;
men[k].date=1;
}
else
{
men[k].x2=i,men[k].y2=j;
}
}
else if(str[i][j]=='L')
{
sx=i,sy=j;
}
}
}
if(bfs()==1)
{
printf("-1\n");
}
}
return 0;
}
int bfs()
{
struct cur e;
e.x=sx, e.y=sy, e.step=0;
queue<cur>q;
q.push(e);
while(!q.empty())
{
e=q.front();
q.pop();
struct cur p;
if(str[e.x][e.y]=='Q')
{
printf("%d\n",e.step);
return -1;
}
else
{
for(int i=0;i<4;i++)
{
int x=e.x+dir[i][0],y=e.y+dir[i][1];
if(judge(x,y)==1)
{
if(str[x][y]>='a'&&str[x][y]<='z')
{
int k=str[x][y]-'0';
if(men[k].x1==x&&men[k].y1==y)
{
p.x=men[k].x2,p.y=men[k].y2;
p.step=e.step+1;
q.push(p);
}
else
{
p.x=men[k].x1,p.y=men[k].y1;
p.step=e.step+1;
q.push(p);
}
}
else
{
p.x=x,p.y=y,p.step=e.step+1;
q.push(p);
}
visit[x][y]=1;
}
}
}
}
return 1;
}