UVA816 三状态BFS
UVA816
题目是一个标准的求最短路问题,但是多了一个朝向。也就是说不同的朝向进入一个节点,转换方向的方法各不相同。所以我们对于每个节点出了横纵坐标以外,还要多一个维度记录朝向。
定义节点数组p[y][x][dir]记录(y,x)坐标,朝向为dir的节点的前继节点。
#include<iostream>//不需要判断边界、移动方向已经确定
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<sstream>
using namespace std;
int r0, c0, r1, c1, r2, c2;//r0、c0表示出发坐标 r2、c2是终点坐标
char dir0;//出发方向
const int maxn = 11;
int has_edge[maxn][maxn][4][3], d[maxn][maxn][4];
struct node
{
int r, c, dir;//r行 c列 dir方向
node(int tr, int tc, int tdir)
{
r = tr; c = tc; dir = tdir;
}
node()
{
}
};
node p[maxn][maxn][4];
const char *dirs = "NESW";
const char *turns = "FLR";
int dir_id(char c)
{
return strchr(dirs, c) - dirs;
}
int turn_id(char c)
{
return strchr(turns, c) - turns;
}
const int dr[] = { -1, 0, 1, 0 };
const int dc[] = { 0, 1, 0, -1 };
node walk(const node &u, int turn) //求当前节点的后继节点
{
int dir = u.dir;
if (turn == 1) dir = (dir + 3) % 4;
if (turn == 2) dir = (dir + 1) % 4;
return node(u.r + dr[dir], u.c + dc[dir], dir);
}
void print_ans(node u) //递归打印路径
{
vector<node>nodes;
for (;;)
{
nodes.push_back(u);
if (d[u.r][u.c][u.dir] == 0) break;
u = p[u.r][u.c][u.dir];
}
//打印解
int cnt = 0;
for (int i = nodes.size() - 1; i >= 0; i--)
{
if (cnt % 10 == 0) printf(" ");
printf(" (%d,%d)", nodes[i].r, nodes[i].c);
if (++cnt % 10 == 0) printf("n");
}
大专栏 UVA816 三状态BFS if (nodes.size() % 10 != 0) printf("n");
}
void bfs()
{
int t = dir_id(dir0);
d[r0][c0][t] = 0;
node temp(r0, c0, t);
d[r1][c1][t] = 1;
node v(r1, c1, t);
p[r1][c1][t] = temp;
queue<node>q;
q.push(v);
while (!q.empty())
{
node u = q.front();
q.pop();
if (u.r == r2&&u.c == c2)
{
print_ans(u);
return;
}
for (int i = 0; i<3; i++)
{
if (has_edge[u.r][u.c][u.dir][i])
{
v = walk(u, i);
if (d[v.r][v.c][v.dir]<0)
{
d[v.r][v.c][v.dir] = d[u.r][u.c][u.dir] + 1;
p[v.r][v.c][v.dir] = u;
q.push(v);
}
}
}
}
printf(" No Solution Possiblen");
}
int main(void)
{
string name, pos;
int r, c;
string str;
stringstream ss;
while (cin >> name&&name != "END")
{
cin >> r0 >> c0 >> dir0 >> r2 >> c2;
//计算进入迷宫的坐标
if (dir0 == 'N') { r1 = r0 - 1; c1 = c0; }
else if (dir0 == 'E') { r1 = r0; c1 = c0 + 1; }
else if (dir0 == 'W') { r1 = r0; c1 = c0 - 1; }
else { r1 = r0 + 1; c1 = c0; }
/*初始化*/
memset(d, -1, sizeof(d));
memset(has_edge, 0, sizeof(has_edge));
memset(p, 0, sizeof(p));
getchar();
while (getline(cin, pos) && pos != "0")
{
ss << pos;
ss >> r >> c;
while (ss >> str)
{
if (str[0] != '*')
{
int dir = dir_id(str[0]);
for (int i = 1; i<str.size(); i++)
{
int turn = turn_id(str[i]);
has_edge[r][c][dir][turn] = 1;
}
}
}
ss.clear();
}
cout << name << "n";
bfs();
}
return 0;
}
原文地址:https://www.cnblogs.com/liuzhongrong/p/12289310.html
时间: 2024-10-09 19:57:06