You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 310;
//地图
char a[N][N];
int x, y; //Bessie的起始点
//是不是访问过了
int st[N][N];
//放入队列的结构体
struct node {
int x, y, step;
};
queue<node> q; //队列
int dx[] = {0, 0, -1, 1}; //左右上下
int dy[] = {-1, 1, 0, 0};
//找到相对点
void getDui(int x, int y, int &x1, int &y1) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
//注意不能与原点相同
if (!(i == x && j == y) && a[i][j] == a[x][y]) x1 = i, y1 = j;
}
}
void bfs() {
/**
被填充为玉米的格子用“#”表示,草地用“.”表示每一对装置的结点由相同的大写字母组成“A-Z”
且没有两个不同装置的结点用同一个字母表示,出口用“=”表示。
*/
st[x][y] = 1; //Bessie的起始点要设置为已使用
q.push({x, y, 0});//初始化队列
//开始广度优先搜索
while (!q.empty()) {
node p = q.front();
q.pop();
//四个方向
for (int i = 0; i < 4; i++) {
//下一步的坐标
int x1 = p.x + dx[i], y1 = p.y + dy[i];
//如果下一跳是出口
if (a[x1][y1] == '=') {
cout << p.step + 1 << endl;
return;
}
//每一对装置的结点由相同的大写字母组成“A-Z”
if (a[x1][y1] >= 'A' && a[x1][y1] <= 'Z') {
//坐上直达飞行设备
//找对应的点
getDui(x1, y1, x, y);
x1 = x, y1 = y;
}
//草地用“.”表示,玉米用“#”表示
if (x1 >= 1 && y1 >= 1 && a[x1][y1] != '#' && !st[x1][y1]) {
q.push({x1, y1, p.step + 1});
//标识走过了
st[x1][y1] = 1;
}
}
}
}
int main() {
//输入
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == '@') x = i, y = j;//出发点
}
//广度优先搜索
bfs();
return 0;
}