sicily 1155 Can I Post the letter

嵇永望
2023-12-01
 

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

 

320 11 2310 10

Sample Output

 

I can post the letterI can't post the letter

 

简单的深搜

#include <iostream>
#include <stack>
using namespace std;

stack<int> st;
int load[200][200];
int isv[200][200];
int N;
bool cal(int a)
{
 for(int i=0;i<N;i++)
 {
   if(load[a][i]==1&&isv[a][i]==0)
   {
    st.push(i);
   }
  
   while(!st.empty())
   {
    int k=st.top();
    if(k==(N-1))
    {
     return true;
    }
    st.pop();
    isv[a][i]=1;
    cal(k);
        
   }
  
 }
 return false;
}
int main()
{
 while(cin>>N&&N!=0)
 {
  while(!st.empty())
  {
   st.pop();
  }
  for(int i=0;i<200;i++)
  {
   for(int j=0;j<200;j++)
   {
    load[i][j]=0;
    isv[i][j]=0;
   }
  }
  int M;
  cin>>M;
  for(int i=0;i<M;i++)
  {
   int a,b;
   cin>>a;
   cin>>b;
   load[a][b]=1;
  }
  if(cal(0))
   cout<<"I can post the letter"<<endl;
  else
   cout<<"I can't post the letter"<<endl;

 }
 return 0;
}

 类似资料:

相关阅读

相关文章

相关问答