include fstream include iostream using namespace std ifstream cin inpu

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <fstream>
//#include <iostream>
using namespace std;
ifstream cin("input.txt");
ofstream cout("output.txt");
int a[110][110];
int path[110];
int q[110];
int n,x,y,b=0,e=0;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
cin>>a[i][j];
cin>>x>>y;
q[e]=x;
e++;
while(b<=e){
for (int i=1;i<=n;i++)
if (a[q[b]][i]==1){
if ((path[i]==0)&&(i!=x)) q[e++]=i;
if ((path[i]==0)||(path[i]>path[q[b]]+1)) path[i]=path[q[b]]+1;
}
b++;
}
if (x==y) cout<<0;
else
if (path[y]>0) cout<<path[y];
else cout<<-1;
return 0;
}