__szh_DNCB__ @ 2024-10-01 16:43:56
bfs 死循环
#include<bits/stdc++.h>
using namespace std;
int n;
pair<int,int> beginning,ending;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
bool s[1005][1005];
bool isgo[1005][1005];
bool check(int x,int y){
if (x <= 0 || x >= n || y <= 0 || y >= n || !(s[x][y]) || isgo[x][y]){
return false;
}return true;
}
int bfs(){
int cnt = 0;
queue<pair<int,int>> q;
q.push(make_pair(beginning.first,beginning.second));
while (q.size() != 0){
pair<int,int> f = q.front();
q.pop();
if (f == ending)return cnt;
isgo[f.first][f.second] = true;
for (int i=0;i<4;i++){
if (check(f.first + dx[i],f.second + dy[i])){
q.push({f.first+dx[i],f.second + dy[i]});
}
}
cnt++;
}
}
int main(){
cin >> n;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
int v;cin >> v;
if (v == 1)s[i][j] = false;
else s[i][j] = true;
}
}
cin >> beginning.first >> beginning.second;
cin >> ending.first >> ending.second;
cout << bfs();
return 0;
}
by xyx404 @ 2024-10-01 16:51:41
@szh_DNCB 你没有死循环,输入格式错了不信你在复制完样例后在输入任意东西看看是不是可以输入。
by xyx404 @ 2024-10-01 16:52:39
注意输入时矩阵是连在一起的。 @szh_DNCB
by __szh_DNCB__ @ 2024-10-01 16:56:16
@xyx404 好的看到了。。。但是已经重构过了,谢谢。已关