(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
- (SOJ) check if a word is on a given Boggle board
- Check if a given Binary Tree is SumTree
- (SOJ) Is it a word ladder? (词梯)
- Check if a port is being used on a Windows machine
- Check if a string value is numeric
- Check if a linked list is palindrome
- If given a choice
- Q2.7 Check if a list is a palindrome
- 【Leetcode】Given a binary tree, check whether it is a mirror of itself
- Check if a File is in Internet Explorers Cache
- Q1.8 Check if s2 is a rotation of s1
- Q4.5 Check if a binary search is BST
- Interview Check If n Is A Perfect Square
- There is a board with
- Given two binary trees, write a function to check if they are equal or not.
- 2.7 Implement a function to check if a linked list is a palindrome
- Given an integer, write a function to determine if it is a power of two.
- Given an integer, write a function to determine if it is a power of two
- Java一个对象的初始化过程
- 使用apache CXF和maven开发Web Service
- java面试笔试资料 百度网盘
- android handler详解
- CSS/CSS3 文本
- (SOJ) check if a word is on a given Boggle board
- StackFrame栈帧
- 【Java数据结构与算法】| 数组--天平称量问题
- Android IntentService完全解析 当Service遇到Handler
- 清华镜像源下载Android源码
- 软件工程简介(上)--个人看法
- Uiautomator自动化测试环境搭建eclipse+sdk+ADT——小白入门超详细
- SVProgressHUD网络延迟点击返回蒙板不消失的解决方案
- /usr/bin/env: node: 没有那个文件或目录