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

(SOJ) check if a word is on a given Boggle board_第1张图片

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

#include 
#include 
#include 
#include  
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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  > 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  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  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  > ss ;
				ss.clear() ;
				int row = i , column = j ;
				ss.insert( make_pair ( i , j ) ) ;
				queue < set < pair  > > path ;
				while ( ! path.empty() )
					path.pop() ;
				path.push ( ss ) ; 
				queue < pair < int , pair  > > q ;
				while ( ! q.empty() )
					q.pop() ;
				pair < int , pair  > 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  > sss = ss ;
						int x = xx + dir[k][0] , y = yy + dir[k][1] ;
						if ( x >= 0 && x < rows && y >= 0 && y < columns ) 
						{
							set < pair  >::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 ;
}


你可能感兴趣的:(soj,soj,sicily,Boggle)