1 条题解

  • 0
    @ 2023-12-6 17:16:53

    宽搜

    #include<bits/stdc++.h>
    using namespace std;
    int n,x,y;
    int r;
    bool h[111];
    struct node
    {
    	int x,st;
    };
    vector<int> q[111];
    queue<node> p;
    void bfs()
    {
    	int st;
    	p.push({x,0});
    	h[x]=1;
    	while(!p.empty())
    	{
    		x=p.front().x;
    		st=p.front().st;
    		p.pop();
    		for(int i=0;i<q[x].size();i++)
    		{
    			if(!h[q[x][i]])
    			{
    				h[q[x][i]]=1;
    				if(q[x][i]==y) 
    				{
    					cout<<st<<endl;
    					return;
    				}
    				p.push({q[x][i],st+1});
    			}
    		}
    	}
    }
    int main()
    {
    	//freopen("relationship.in","r",stdin);
    	//freopen("relationship.out","w",stdout);
    	cin>>n>>x>>y;
    	for(int i=1;i<=n;i++)
    	{
    		for(int j=1;j<=n;j++)
    		{
    			cin>>r;
    			if(r)
    			{
    				q[i].push_back(j);
    			}
    		}
    	}
    	if(x==y) 
    	{
    		cout<<0<<endl;
    		return 0;
    	}
    	bfs();
    	return 0;
    }
    
    

    信息

    ID
    697
    时间
    1000ms
    内存
    128MiB
    难度
    2
    标签
    递交数
    72
    已通过
    29
    上传者