提交时间:2024-04-07 20:56:15
运行 ID: 142403
#include <iostream> #include <queue> using namespace std; int a[110][110], n, x, y; bool flag[110]; struct point{int id, step;}; queue <point> q; void bfs() { while(!q.empty()) { point t = q.front(); q.pop(); int tid = t.id, tstep = t.step; if(tid == y) { cout << tstep - 1 << endl; return; } for(int i = 1; i <= n; i++) { if(a[tid][i] && !flag[i]) { flag[i] = true; q.push({i, tstep + 1}); } } } } int main() { cin >> n >> x >> y; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) cin >> a[i][j]; q.push({x, 0}); flag[x] = true; bfs(); return 0; }