典型的图遍历题,任何相关算法都可以解决,我使用的是最基本的宽搜。
Run Time: 0sec
Run Memory: 312KB
Code length: 965Bytes
SubmitTime: 2011-12-24 11:19:47
// Problem#: 1155
// Submission#: 1120671
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int main()
{
int N, M;
int i, j;
bool visited[ 200 ];
bool road[ 200 ][ 200 ];
while ( cin >> N && N ) {
memset( visited, false, sizeof( visited ) );
memset( road, false, sizeof( road ) );
cin >> M;
while ( M-- ) {
cin >> i >> j;
road[ i ][ j ] = true;
}
queue<int> q;
q.push( 0 );
visited[ 0 ] = true;
while ( !q.empty() && !visited[ N - 1 ] ) {
i = q.front();
for ( j = 0; j < N; j++ ) {
if ( road[ i ][ j ] && !visited[ j ] ) {
q.push( j );
visited[ j ] = true;
}
}
q.pop();
}
visited[ N - 1 ] ? cout << "I can post the letter\n": cout << "I can't post the letter\n";
}
return 0;
}