Sicily 1155 Can I Post the letter (图的遍历 BFS)

题目:http://soj.me/1155

   n座城市 m条道路 问能否从第一座城市走到最后一座城市

思路:广度优先搜索

#include <iostream>
#include <queue>
#include <memory.h>
using namespace std;

bool map[205][205];
bool visit[205];
int n,m;

bool bfs()
{
    queue<int> q;
    q.push(0);
    
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
        for(int i=0;i<n;i++)
        {
            if(map[temp][i]==1&&visit[i]==0)
            {
                if(i==n-1) return 1;
                q.push(i);
                visit[i]=1;
                
            }
        }
    }
    return 0;

}

int main()
{
    while(1)
    {
        memset(map,0,sizeof(map));
        memset(visit,0,sizeof(visit));
        cin>>n>>m;
        if(!n) break;
        for (int i = 0; i < m; i++)
        {
            int a,b;
            cin>>a>>b;
            map[a][b]=1;
        }
        bool ans=bfs();
        if(ans) cout<<"I can post the letter"<<endl;
        else cout<<"I can't post the letter"<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/danielqiu/archive/2013/01/30/2882675.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值