山再高,往上爬,总能登顶;
路再长,走下去,定能到达。
UPC-走迷宫
题目描述
邪恶的Pcf把KeineDuck关在了一个迷宫中!KeineDuck必须找到一条通往出口的道路。
方便起见,我们将迷宫描述成了一个n行m列的网格,网格中每个格子都有一个特殊的字符:
• #表示这是一堵墙,不能通过。
• @:KeineDuck当前位置。
• =:迷宫的出口。
• 字母A-Z:传送装置,它们总是成对出现的。若KeineDuck走到其中一个传送装置上,她会立马传送到另一个传送装置。
假设KeineDuck每次移动都会消耗一单位的时间,现在,她想知道,最少要多少时间才能走到出口。
输入
第一行两个整数n,m,表示行数和列数。
接下来n行,每行m个字符,具体内容参考题目描述。
输出
一个整数,表示最少的时间。
特别地,若没有任何一条到达出口的路径,输出-1。
Sample Iutput
5 6
###=##
#.W.##
#.####
#.@W##
######
Sample Output
3
Sample Input
6 3
#=#
#A#
#@#
#.#
#A#
###
Sample Output
3
思路分析
和正常的bfs迷宫一样,只不过这里需要记录一下传送门,然后呢要知道你从这面的传送门如和另一面传送门如是不一样的,所以说judge函数只能记录入门点,入队列的是出门点,按照此思路进行就可以AC了。
AC时刻到
#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<utility>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#pragma warning(disable:4244)
#define PI 3.1415926536
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll read() {
ll c = getchar(), Nig = 1, x = 0;
while (!isdigit(c) && c != '-')c = getchar();
if (c == '-')Nig = -1, c = getchar();
while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
return Nig * x;
}
inline void out(ll a) {
if (a < 0)putchar('-'), a = -a;
if (a >= 10)out(a / 10);
putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
#define Floyd for(int k = 1; k <= n; k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
#define read read()
struct Node {
bool mark;
bool AB, BA;
int x[2], y[2];
}portal[100];
struct node {
int x, y, step;
};
int tx, ty;
char save[400][400];
bool judge[400][400];
queue<node>q;
node temp;
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
void bfs()
{
while (1)
{
if (q.empty()) {
cout << -1 << endl;
return;
}
temp = q.front();
q.pop();
if (save[temp.x][temp.y] == '=') {
cout << temp.step << endl;
return;
}
for (int i = 0; i < 4; i++)
{
tx = temp.x + dx[i];
ty = temp.y + dy[i];
if (!judge[tx][ty])
{
judge[tx][ty] = 1;
if (save[tx][ty] >= 'A' && save[tx][ty] <= 'Z')
{
if (portal[save[tx][ty]].x[0] == tx && portal[save[tx][ty]].y[0] == ty)
q.push(node{ portal[save[tx][ty]].x[1],portal[save[tx][ty]].y[1],temp.step + 1 });
else
q.push(node{ portal[save[tx][ty]].x[0],portal[save[tx][ty]].y[0],temp.step + 1 });
}
else q.push(node{ tx,ty,temp.step + 1 });
}
}
}
}
int main()
{
int n = read, m = read;
for (int i = 0; i <= m; i++)
judge[0][i] = judge[n + 1][i] = 1;
for (int i = 0; i <= n; i++)
judge[i][0] = judge[i][m + 1] = 1;
for (int i = 1; i <= n; i++) {
scanf("%s", save[i] + 1);
for (int j = 1; j <= m; j++)
{
if (save[i][j] == '#')judge[i][j] = 1;
else if (save[i][j] >= 'A' && save[i][j] <= 'Z')
{
if (portal[save[i][j]].mark)
{
portal[save[i][j]].x[0] = i;
portal[save[i][j]].y[0] = j;
}
else
{
portal[save[i][j]].mark = 1;
portal[save[i][j]].x[1] = i;
portal[save[i][j]].y[1] = j;
}
}
else if (save[i][j] == '@')
{
q.push(node{ i,j,0 });
judge[i][j] = 1;
}
}
}
bfs();
}
By-轮月