eolymp
bolt
Try our new interface for solving problems
Problems

Apocalyptic Alignment

published at 3/7/24, 1:24:47 pm

include <iostream>

include <vector>

using namespace std; int main() { int n,s,d,t; cin>>n>>s>>d; int f[n+1][n+1], ruh[n+1]; for (int i=1; i<=n; i++) { ruh[i]=-1; for (int j=1; j<=n; j++) { cin>>f[i][j]; } } vector <int> bfs; bfs.push_back(s); ruh[bfs[0]]=0; while (bfs.size()>0) { if (bfs[0]==d) { t++; cout<<ruh[bfs[0]]; return 0; } for (int i=0; i<n+1; i++) { if (f[bfs[0]][i]==1 and ruh[i]==-1) { bfs.push_back(i); ruh[i]=ruh[bfs[0]]+1; } } bfs.erase(bfs.begin()); } if (t==0) { cout<<0; } }