Click me to HDU1181
so soon river goes them got moon begin big 0
Yes.Harry 可以念这个咒语:"big-got-them".HintHint
#include<iostream> #include<algorithm> #include<string> #include<queue> using namespace std; const int MAX_INT = 0x7f7f7f7f; const int beg = (int)('b'-'a'),end = (int)('m'-'a'); int map[27][27]; bool visited[27]; /* void CreateMap() { string str; memset(map,0x7f,sizeof(map)); while( cin >>str && str != "0" ) { map[*str.begin()-'a'][*str.rbegin()-'a'] = 1; } }*/ bool FindThePath()//bfs or dfs { queue<int> q; q.push(beg); int vex,i; while(!q.empty()) { vex = q.front(); q.pop(); //cout << vex + 'a' <<' '; //printf("%c ",vex+'a'); visited[vex] = true; for(i=0; i<26; ++i) { if(!visited[i] && map[vex][i] == 1) { if(i == end) return true; q.push(i); } } } return false; } int main() { //CreateMap(); string str; while( cin >>str ) { memset(visited,false,sizeof(visited)); memset(map,0x7f,sizeof(map)); while(str != "0") { map[*str.begin()-'a'][*str.rbegin()-'a'] = 1; cin >> str; } if(FindThePath()) cout <<"Yes."<< endl; else cout <<"No."<<endl; } return 0; }
#include<iostream> #include<algorithm> #include<string> #include<queue> using namespace std; const int MAX_INT = 0x7f7f7f7f; int end = (int)('m'-'a'); int map[27][27]; bool visited[27]; int NextAdjVex(int vex,int w) { for(int i=w; i<26; ++i) { if( !visited[i] && map[vex][i] == 1 ) return i; } return -1; } bool isDone; void DFS(int vex)//bfs or dfs { if ( isDone ) return; if( vex == end ) { isDone = true; return ; } visited[vex] = true; int w = NextAdjVex(vex,0); while(!isDone && w>=0) { DFS(w); w = NextAdjVex(vex,w); // vex is adj with w } } int main() { string str; int beg; while( cin >>str ) { memset(visited,false,sizeof(visited)); memset(map,0x7f,sizeof(map)); while(str != "0") { map[*str.begin()-'a'][*str.rbegin()-'a'] = 1; cin >> str; } isDone = false; beg = (int)('b'-'a'); DFS(beg); if(isDone) cout <<"Yes."<< endl; else cout <<"No."<<endl; } return 0; }