HDU 1181 变形课

HDU 1181 变形课
构图,BFS。
以下是我的代码:
#include < iostream >
#include
< string >
#include
< queue >
#include
< cstdio >
#include
< cstring >
using   namespace  std;
const   string  kEndSign ( " 0 " );
const   int  kBeginNum ( ' b ' - ' a ' + 1 ),kEndNum ( ' m ' - ' a ' + 1 );

int  main()
{
    
/*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    //
*/
    
    
string  s;
    
bool  map[ 27 ][ 27 ] = { false };
    
while (cin >> s)
    {
        
if (s == kEndSign)
        {
            
bool  visited[ 27 ] = { false },success( false );
            queue
< int >  q;
            q.push(kBeginNum);
            visited[kBeginNum]
= true ;
            
while ( ! q.empty())
            {
                
int  now = q.front();q.pop();
                
if (now == kEndNum)
                {
                    success
= true ;
                    
break ;
                }
                
for ( int  i = 1 ;i <= 26 ;i ++ )
                    
if ( ! visited[i]  &&  map[now][i])
                    {
                        q.push(i);
                        visited[i]
= true ;
                    }
            }
            
if (success)
                cout
<< " Yes. " << endl;
            
else  cout << " No. " << endl;
            
            memset(map,
false , 27 * 27 * sizeof ( bool ));
        }
        
else
            map[s[
0 ] - ' a ' + 1 ][s[s.size() - 1 ] - ' a ' + 1 ] = true ;
    }
    
return   0 ;
}

你可能感兴趣的:(HDU 1181 变形课)