提交时间:2024-01-21 15:35:12

运行 ID: 121153

#include<iostream> #include<vector> using namespace std; const int N = 110; int n, a, b, t, ans = 1e9; bool vis[N]; vector<int> vec[N]; void dfs(int x, int cnt = 0){ if(vis[x]) return; if(x == b){ ans = min(ans, cnt - 1); return; } vis[x] = true; for(int i = 0; i < vec[x].size(); i++){ dfs(vec[x][i], cnt + 1); } vis[x] = false; } int main(){ cin >> n >> a >> b; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ cin >> t; if(t) vec[i].push_back(j); } } dfs(a); cout << ans; return 0; }