大佬们能帮看看为什么我第一题只A了10%吗,我觉得没什么问题呀。。。
后端,走有障碍物迷宫的那个
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
int n, bx, by, ex, ey;
vector<vector<char> > map;
vector<int> dx = { 0, 0, 1, -1 };
vector<int> dy = { 1, -1, 0, 0 };
bool go(int x, int y, vector<vector<bool> >& memo) {
if (x < 0 || x >= n || y < 0 || y >= n || memo[x][y] == true || map[x][y] == '#' || map[x][y] == '@')
return false;
}
int main() {
cin >> n;
cin >> bx >> by >> ex >> ey;
vector<vector<bool> > memo(n, vector<bool>(n, false));
for (int i = 0; i < n; ++i) {
string str;
cin >> str;
vector<char> tmp;
for (int j = 0; j < n; ++j) {
tmp.push_back(str[j]);
}
map.push_back(tmp);
}
int len = 0;
// 开始遍历最优路径
queue<pair<int, int> > q;
q.push({ bx, by });
while (!q.empty()) {
int sz = q.size();
++len;
for (int i = 0; i < sz; ++i) {
pair<int, int> top = q.front();
q.pop();
int cx = top.first;
int cy = top.second;
memo[cx][cy] == true;
if (top.first == ex && top.second == ey) {
cout << len << endl;
return 0;
}
for (int i = 0; i < 4; ++i) {
int nx = cx + dx[i];
int ny = cy + dy[i];
if (go(nx, ny, memo))
q.push({ nx, ny });
}
}
}
cout << -1 << endl;
return 0;
}
全部评论
(21) 回帖