该题目的大意是:给定一个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 ;
}

(SOJ) check if a word is on a given Boggle board相关推荐

  1. [Swift]LeetCode212. 单词搜索 II | Word Search II

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  2. java POI对word中的表格动态插入固定数据,以及插入不确定数量的的数据

    java POI对word中的表格动态插入固定数据,以及插入不具体的数据 遇到个项目本来是用Execl导出的,相对简单,客户要求用Word导出,并按照他们给的模板进行导出: 从网上百度了一下,然后自己 ...

  3. 《D o C P》学习笔记(6 - 1)Word Games

    备注1:每个视频的英文字幕,都翻译成中文,太消耗时间了,为了加快学习进度,我将暂停这个工作,仅对英文字幕做少量注释. 备注2:将.flv视频文件与Subtitles文件夹中的.srt字幕文件放到同1个 ...

  4. 79. Word Search

    leetcod79题在矩阵中寻找某个单词 """ 79. Word Search MediumShare Given a 2D board and a word, fin ...

  5. 在矩阵中查找字符串 Word Search

    2019独角兽企业重金招聘Python工程师标准>>> 问题: Given a 2D board and a word, find if the word exists in the ...

  6. 79. Word Search 单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

  7. leetcode 79. Word Search | 79. 单词搜索(回溯+DFS)

    题目 https://leetcode.com/problems/word-search/ 题解 回溯 + DFS,回溯是有后效性的,所以不能转化为 DP class Solution {int M; ...

  8. LeetCode() Word Search II

    超时,用了tire也不行,需要再改. class Solution {class TrieNode { public:// Initialize your data structure here.Tr ...

  9. [Swift]LeetCode79. 单词搜索 | Word Search

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

最新文章

  1. C语言科学计算器思路,大神教你如何用C语言实现科学计算器
  2. OpenCV中使用YOLO对象检测
  3. 《敏捷迭代开发:管理者指南》—第2章2.5节渐进开发和自适应开发
  4. Android 2.2 r1 API 中文文档系列(11) —— RadioButton
  5. C#委托-委托不关心被封装的类
  6. 教你如何一篇博客读懂设计模式之—--原型模式
  7. 学生计算机如何用数字小游戏,【数学思维】小学生数学趣味游戏:你身上的计算器...
  8. js正则校验 统一社会信用代码
  9. 文献检索与论文写作——学习笔记
  10. 人民币大小写转换(php版),JavaScript实现人民币大小写转换
  11. 移动端touch事件和鼠标事件
  12. Linux中shell编程——编写shell脚本,实现功能:在用户输入年、月后自动打印出对应年月的日
  13. 帆软报表写switch函数
  14. PS基础-day03套索,魔棒,裁剪,修复画笔工具组,蒙版
  15. 轩迅汇如何做好个人定位?定位越早,受益越多
  16. MAC 地址以及它的单播、多播、组播
  17. 【基础知识】~ FIFO
  18. KubeEdge 超详细部署记录问题记载6.28
  19. 2018ICPC北京赛总结
  20. 微信小程序跳一跳辅助之路(ios版)

热门文章

  1. Build a project
  2. vue项目中 一行文本 文字 根据关键字 改变颜色 改变展示颜色
  3. 倪衡:大数据时代小微企业质量管理创新
  4. 在Excel数据最后一行下面写入数据的一种方法
  5. 千年瑶里:人间失落的天堂(一)
  6. 可以DIY装修的商城系统,你也能拥有
  7. 某乎上看到的专业技术解读
  8. WinUsb_ReadPipe和WinUsb_WritePipe函数功能理解
  9. 3D游戏编程 作业六 打飞碟改进
  10. Roblox剑九之剑一