Can I Post the lette

来源:互联网 发布:zbrush mac 破解教程 编辑:程序博客网 时间:2024/05/17 03:32

深搜dfs或广搜bfs都可以.

Description

I am a traveler. I want to post a letter to Merlin. But because there are so many roads I can walk through, and maybe I can’t go to Merlin’s house following these roads, I must judge whether I can post the letter to Merlin before starting my travel. 

Suppose the cities are numbered from 0 to N-1, I am at city 0, and Merlin is at city N-1. And there are M roads I can walk through, each of which connects two cities. Please note that each road is direct, i.e. a road from A to B does not indicate a road from B to A.

Please help me to find out whether I could go to Merlin’s house or not.

Input

There are multiple input cases. For one case, first are two lines of two integers N and M, (N<=200, M<=N*N/2), that means the number of citys and the number of roads. And Merlin stands at city N-1. After that, there are M lines. Each line contains two integers i and j, what means that there is a road from city i to city j.

The input is terminated by N=0.

Output
For each test case, if I can post the letter print “I can post the letter” in one line, otherwise print “I can't post the letter”.
Sample Input
 Copy sample input to clipboard

#include <stack>#include <iostream>#include <memory.h>#include <string>#include <vector>using namespace std; bool visited[202];int main() {     int n,m;        while(cin >> n && n != 0)    {        cin >> m;        int x,y;        vector<int> g[200];        for(int i = 0;i < m;i++)        {            cin >> x >> y;            g[x].push_back(y);        }        memset(visited,false,sizeof(visited));        stack<int> s;        s.push(0);        visited[0] = true;        while(!s.empty())        {            int v = s.top();            s.pop();            for(int i = 0;i < g[v].size();i++)            {                if(!visited[g[v][i]])                {                    visited[g[v][i]] = true;                    s.push(g[v][i]);                    //break;                }            }        }        if(visited[n - 1])            cout << "I can post the letter" << endl;        else            cout << "I can't post the letter" << endl;    }    return 0; }                                 


0 0
原创粉丝点击