50,779
社区成员
发帖
与我相关
我的任务
分享
用bfs直接写没写对,还是看了题解,这题跟不同bfs不太一样,不能直接对走过的点进行标记,当再次走过一个点时,如果转弯次数反倒小或者方向不同,也要加入队列
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int N = 150, INF = 0x3f3f3f3f;
char mp[N][N];
int st[N][N];//标记转弯次数是否确定
int ax, ay, bx, by;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int d[N][N];
//转弯是根据当前点前一个点的上一个点的坐标变化来判断的
//因此还要记录每个点的上一个点的坐标
//走过的点不能直接进行标记
//当再次经过该点时转弯次数更少或方向改变时也要加入队列
//使用优先队列,使得出队的点都是转弯次数最小的
struct Node {
int x, y;
int pre_x, pre_y;//上一个点的坐标
int dis;//转弯次数
bool operator<(const Node &w) const { // 优先队列中按照转弯次数从小到大排列
return dis > w.dis;
}
};
int n;
bool check(int x, int y) {
if (x < 1 || y < 1 || x > n || y > n) return 1; //越界
return 0;
}
int bfs(int x, int y) {
priority_queue<Node>q;//优先队列
memset(d, -1, sizeof d);
q.push({x, y, x, y, 0});
st[x][y] = 1;
while (q.size()) {
Node t = q.top();//优先队列出队
q.pop();
if (t.x == bx && t.y == by) {
return t.dis;//转弯次数
}
st[t.x][t.y] = 1; //每次出队时转弯次数确定
for (int i = 0; i < 4; i++) {
int fx = t.x + dx[i], fy = t.y + dy[i];
if (check(fx, fy)) continue;//越界
if (st[fx][fy]) continue;//转弯次数确定与否
if (mp[fx][fy] == 'x') continue;
int cnt = 0; //转弯
if (fx != t.pre_x && fy != t.pre_y) cnt++;
q.push({fx, fy, t.x, t.y, t.dis + cnt});
}
}
return -1;//注意不能达到时返回-1
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> mp[i][j];
if (mp[i][j] == 'A') ax = i, ay = j;
if (mp[i][j] == 'B') bx = i, by = j;
}
}
cout << bfs(ax, ay);
return 0;
}