(SOJ) check if a word is on a given Boggle board

来源:互联网 发布:恒生杠杆软件 编辑:程序博客网 时间:2024/05/19 16:49

该题目的大意是:给定一个4X4的字母表,按照Boggle的原则查找一个字符串是否在字母表中。

#include <iostream>#include <cstdio>#include <cstring>#include <string> #include <vector>#include <list>#include <set>#include <queue>#include <deque>#include <map>#include <algorithm>#include <cstdlib>#include <cmath>#include <ctime>#include <iomanip>#include <fstream>using namespace std ;/******************************************************************************/class BoggleBoard {public:    BoggleBoard ( string inputfile ) ;//¸ù¾ÝÎļþ¹¹½¨×Ö·ûÅÌ£¨grid£©     BoggleBoard ( int n , int m ) ; //Ëæ»ú¹¹½¨nÐÐmÁÐ×Ö·ûÅÌ void printBoard () ;     char get ( int i , int j ) ;//È¡µÃ(i,j)µÄ×Ö·û     int rows () ;     //·µ»Ø×Ö·ûÅÌÐÐÊýint cols () ; //·µ»Ø×Ö·ûÅÌÁÐÊývector < vector <char> > grid ;private :int row , column ;};   BoggleBoard::BoggleBoard ( string inputfile ) //¸ù¾ÝÎļþ¹¹½¨×Ö·ûÅÌ£¨grid£© {grid.clear() ;row = 0 ;column = 0 ;ifstream inp ( inputfile.c_str() ) ;string str ;int validColumns = 0 ;vector <char> temp ;temp.clear() ;while ( inp >> str ){if ( validColumns && validColumns != str.size() ){cout << "The format of file is invalid!!!" << endl ;exit ( 1 ) ;}validColumns = str.size() ;grid.push_back ( temp ) ;for ( int j = 0 ; j < validColumns ; j ++ )grid[row].push_back( str[j] ) ;row ++ ;}column = validColumns ;inp.close () ;}BoggleBoard::BoggleBoard ( int n = 4 , int m = 4 ) //Ëæ»ú¹¹½¨nÐÐmÁÐ×Ö·ûÅÌ  {grid.clear() ;vector <char> temp ( m ) ;grid.resize( n , temp ) ;row = n ;column = m ;srand ( time (0) ) ;for ( int i = 0 ; i < n ; i ++ )for ( int j = 0 ; j < m ; j ++ )grid[i][j] = rand() % 26 + 'A' ;}void BoggleBoard::printBoard () {cout << "The board is: " << endl ;for ( int i = 0 ; i < row ; i ++ ){for ( int j = 0 ; j < column; j ++ ){cout << grid[i][j] ;}cout << endl ;}}char BoggleBoard::get ( int i , int j ) //È¡µÃ(i,j)µÄ×Ö·û{if ( i >= 0 && i < row && j >= 0 && j < column )return grid[i][j] ;cout << i << " " << j << endl ;cout << "¡¾false¡¿" << endl ; exit(1) ;}int BoggleBoard::rows ()      //·µ»Ø×Ö·ûÅÌÐÐÊý{return row ;}int BoggleBoard::cols ()  //·µ»Ø×Ö·ûÅÌÁÐÊý   {return column ;}  /******************************************************************************/bool isOnBoard ( BoggleBoard board , string w ){if ( w == "" )return true ;int dir[8][2] = { {1,0} , {1,1} , {0,1} , {-1,1} , {-1,0} , {-1,-1} , {0,-1} , {1,-1} }; // ¿É×ß·½Ïò int rows = board.rows() ,  columns = board.cols() ;for ( int i = 0 ; i < rows ; i ++ ){for ( int j = 0 ; j < columns ; j ++ ){if ( board.get( i , j ) == w[0] ){set < pair <int,int> > ss ;ss.clear() ;int row = i , column = j ;ss.insert( make_pair ( i , j ) ) ;queue < set < pair <int,int> > > path ;while ( ! path.empty() )path.pop() ;path.push ( ss ) ; queue < pair < int , pair <int,int > > > q ;while ( ! q.empty() )q.pop() ;pair < int , pair <int,int > > temp =  make_pair ( 1 , make_pair ( i , j ) ) ;q.push( temp ) ; while ( ! q.empty() ){temp = q.front() ;q.pop() ;int index = temp.first , xx = temp.second.first , yy = temp.second.second ;ss = path.front() ;path.pop() ;if ( index == w.size() )return true ;for ( int k = 0 ; k < 8 ; k ++ ){set < pair <int,int> > sss = ss ;int x = xx + dir[k][0] , y = yy + dir[k][1] ;if ( x >= 0 && x < rows && y >= 0 && y < columns ) {set < pair <int,int> >::iterator it = sss.find( make_pair ( x , y ) ) ;if ( it != sss.end() )continue ;if ( board.get( x , y ) == w[index] ){q.push( make_pair ( index + 1 , make_pair ( x , y ) ) ) ; sss.insert( make_pair ( x , y ) ) ;path.push( sss ) ; }}}}}}}return false ;}int main (){BoggleBoard a ( "grid1.txt" ) ;cout << isOnBoard ( a , "LTVL" ) << endl ;cout << isOnBoard ( a , "ETR" ) << endl ;cout << isOnBoard ( a , "HHGTH" ) << endl ;return 0 ;}


0 0
原创粉丝点击