题意:走迷宫,但是加入了传送门,传送门分为入口和出口
输入:多case
迷宫大小n,m
输入无空格间隔n行01串(0代表可以走,1代表是墙)
输入q
q行传送门的入口和出口的坐标
终点和起点的坐标
思路:bfs,传送门无非就是换个地方继续bfs
代码:
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
using namespace std;
int n,m;
const int maxn=110;
char a[maxn][maxn];//用来作为地图
int b[maxn][maxn][2];
int c[maxn][maxn];//用来记录地图上到每一个格子的最短路径
int div1[4]= {0,-1,0,1},div2[4]= {1,0,-1,0};//行方向 纵方向
typedef pair <int,int> P;//这个相当于struct结构体里面存着两个int型的变量
int bfs(int x,int y,int x1,int y1)//广度优先搜索
{
queue<P>que;//定义一个队列
que.push(P(x,y));//放入类似于有两个int变量的结构体
c[x][y]=0;//初始的位置步数为0
int i;
int nx,ny;
if(x==x1&&y==y1)
{
printf("0\n");
return 0;
}
while(que.size())
{
P p=que.front();
que.pop();
if(a[p.first][p.second]=='2')//如果这一步是传送门
{
nx=b[p.first][p.second][0];
ny=b[p.first][p.second][1];
que.push(P(nx,ny));
c[nx][ny]=c[p.first][p.second]+1;
}
else
{
for(i=0; i<4; i++)
{
nx=p.first+div1[i];
ny=p.second+div2[i];
if(0<=nx&&nx<n&&ny>=0&&ny<m&&a[nx][ny]!='1'&&c[nx][ny]==99999999)//不超过边界,且不是墙
{
if(c[nx][ny]>c[p.first][p.second]+1)
{
que.push(P(nx,ny));
c[nx][ny]=c[p.first][p.second]+1;
if(nx==x1&&ny==y1)
break;
}
}
}
if(i!=4)
break;
}
}
if(nx==x1&&ny==y1)
printf("%d\n",c[x1][y1]);
else
printf("die\n");
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)//因为输入之间的数值没有空格,当成字符串处理
{
scanf("%s",a[i]);
}
for(int i=0; i<n; i++)//对记录步数的地图,和记录传送门的数组清理上一次的值
{
for(int j=0; j<m; j++)
{
c[i][j]=99999999;
b[i][j][0]=0;
b[i][j][1]=0;
}
}
int q;
scanf("%d",&q);
while(q--)
{
int x,y,x1,y1;
scanf("%d%d%d%d",&x,&y,&x1,&y1);
a[x][y]='2';
b[x][y][0]=x1;
b[x][y][1]=y1;
}
int x,y,x1,y1;
scanf("%d%d%d%d",&x,&y,&x1,&y1);
bfs(x,y,x1,y1);
}
return 0;
}
总结:1,对pair有了初步的了解