7-5 愛瞎跑的學長
題目
三維的搜索題,我也不知道為什么會卡記憶體,出的時候沒注意這個,以前題目改的,
vis陣列記錄這個點有沒有走過,fr陣列記錄上一個點的位置,ste陣列是走到當前這一步需要的動作,
每一個點的定義
struct Point
{
int l;
int x;
int y;
int step;
};
記錄位置和到這個位置是第幾步
題目要求的路徑字典序最小解決辦法
int dx[6] = { -1, 0, 1, 0,0,0 };
int dy[6] = { 0, 0, 0,-1,1,0 };
int dz[6] = { 0,-1, 0, 0,0,1 };
char d[7] = "bdflru";
下一步的選擇直接字典序排序,這樣就保證當前走的一定是最符合條件的,
要求的輸出路徑,用了一個遞回函式,依次輸出走到這一步需要的位置(從終點倒推回來),通過遞回的形式實作正序,
然后就是bfs中的判斷邊界條件和是否可以向上向下爬比較復雜
if (i == 1||i==5) {
if ((map[now.l][now.x][now.y] != '+') && (map[next.l][next.x][next.y] != '+'))continue;
}
if ((map[next.l][next.x][next.y] == '*') || (next.x < 0) || (next.x >= l) || (next.y < 0) || (next.y >= r) || (next.l < 0) || (next.l >= n))
continue;
然后源代碼
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int inf = 0xffffff;
char map[52][105][105];
int vis[52][105][105];
char ste[52][105][105];
int n, l, r;
int ans;
struct Point
{
int l;
int x;
int y;
int step;
};
Point s, w,fr[52][105][105];
int dx[6] = { -1, 0, 1, 0,0,0 };
int dy[6] = { 0, 0, 0,-1,1,0 };
int dz[6] = { 0,-1, 0, 0,0,1 };
char d[7] = "bdflru";
bool flag = 0;
void BFS()
{
queue<Point> q;
vis[s.l][s.x][s.y] = 1;
memset(vis, 0, sizeof(vis));
q.push(s);
while (!q.empty())
{
Point now = q.front();
q.pop();
for (int i = 0; i < 6; i++)
{
Point next;
next.x = now.x + dx[i];
next.y = now.y + dy[i];
next.l = now.l + dz[i];
next.step = now.step + 1;
if (i == 1||i==5) {
if ((map[now.l][now.x][now.y] != '+') && (map[next.l][next.x][next.y] != '+'))continue;
}
if ((map[next.l][next.x][next.y] == '*') || (next.x < 0) || (next.x >= l) || (next.y < 0) || (next.y >= r) || (next.l < 0) || (next.l >= n))
continue;
if (map[next.l][next.x][next.y] == 'E')
{
ans = next.step;
flag = 1;
ste[next.l][next.x][next.y] = d[i];
fr[next.l][next.x][next.y].l = now.l;
fr[next.l][next.x][next.y].x = now.x;
fr[next.l][next.x][next.y].y = now.y;
return;
}
if (((map[next.l][next.x][next.y] == '.') || (map[next.l][next.x][next.y] == '+')) && !vis[next.l][next.x][next.y])
{
vis[next.l][next.x][next.y] = 1;
ste[next.l][next.x][next.y] = d[i];
q.push(next);
fr[next.l][next.x][next.y].l = now.l;
fr[next.l][next.x][next.y].x = now.x;
fr[next.l][next.x][next.y].y = now.y;
}
}
}
}
void prin(int x,int y,int l) {
if (x == s.x && y == s.y && l == s.l)return;
prin(fr[l][x][y].x, fr[l][x][y].y, fr[l][x][y].l);
printf("%c",ste[l][x][y]);
}
int main()
{
int T;
cin >> T;
getchar();
while (T--)
{
int wl, wx, wy;
scanf("%d%d%d", &n, &l, &r);
if (n == 0 && l == 0 && r == 0) break;
memset(map, 0, sizeof(map));
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < l; j++)
{
for (int k = 0; k < r; k++)
{
cin >> map[i][j][k];
if (map[i][j][k] == 'P')
{
s.l = i;
s.x = j;
s.y = k;
}
if (map[i][j][k] == 'E')
{
wl = i;
wx = j;
wy = k;
}
}
}
}
s.step = 0;
flag = 0;
BFS();
if (flag) {
printf("%d ", ans);
prin(wx,wy,wl);
printf("\n");
}
else
printf("xie'e\n");
}
return 0;
}
感覺是有點煩,代碼有點長,測的時候一直在糾結資料怎么出
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/241429.html
標籤:其他
