Boggle Game
Description
board which is a 2D matrix includes a-z and dictionary dict, find the largest collection of words on the board, the words can not overlap in the same position. return the size of largest collection.- The words in the dictionary are not repeated.
- You can reuse the words in the dictionary.
Example
Example 1:
Input:
["abc","def","ghi"]
{"abc","defi","gh"}
Output:
3
Explanation:
we can get the largest collection`["abc", "defi", "gh"]`
Example 2:
Input:
["aaaa","aaaa","aaaa","aaaa"]
{"a"}
Output:
16
Explanation:
we can get the largest collection`["a", "a","a","a","a","a","a","a","a","a","a","a","a","a","a","a"]
思路:tire + dfs。
字典树用于前缀查找。
dfs用于搜索,
找到单词时搜索下一个单词
没有搜索到单词时,四方向遍历(回溯 + 标记)
//建立tire树的过程
class Trie {
TrieNode root; Trie() {
root = new TrieNode('0');
} public void insert(String word) {
if(word == null || word.length() == 0) {
return;
}
TrieNode node = root;
for(int i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if(node.children[ch - 'a'] == null) {
node.children[ch - 'a'] = new TrieNode(ch);
}
node = node.children[ch - 'a'];
}
node.isWord = true;
}
}
//tire的结点
class TrieNode {
char value;
boolean isWord;
TrieNode[] children; TrieNode(char v) {
value = v;
isWord = false;
children = new TrieNode[26];
}
} public class Solution {
/**
* @param board a list of lists of character
* @param words a list of string
* @return an integer
*/
public int boggleGame(char[][] board, String[] words) {
// Write your code here
Trie trie = new Trie();
for(String word : words) {
trie.insert(word);
} int m = board.length;
int n = board[0].length;
List<String> result = new ArrayList<>();
boolean[][] visited = new boolean[m][n];
List<String> path = new ArrayList<>();
findWords(result, board, visited, path, 0, 0, trie.root);
return result.size();
}
//从当前位置出发寻单词存不存在
public void findWords(List<String> result, char[][] board, boolean[][] visited, List<String> words, int x, int y, TrieNode root) { int m = board.length;
int n = board[0].length; for (int i = x; i < m; i++) {
for (int j = y; j < n; j++) {
List<List<Integer>> nextWordIndexes = new ArrayList<>();
List<Integer> path = new ArrayList<>();
getNextWords(nextWordIndexes, board, visited, path, i, j, root);
for (List<Integer> indexes : nextWordIndexes) {
String word = "";
for (int index : indexes) {
int row = index / n;
int col = index % n;
visited[row][col] = true;
word += board[row][col];
} words.add(word);
if (words.size() > result.size()) {
result.clear();
result.addAll(words);
}
findWords(result, board, visited, words, i, j, root);
for (int index : indexes) {
int row = index / n;
int col = index % n;
visited[row][col] = false;
}
words.remove(words.size() - 1);
}
}
y = 0;
}
} int []dx = {0, 1, 0, -1};
int []dy = {1, 0, -1, 0};
//dfs搜索查找单词
private void getNextWords(List<List<Integer>> words, char[][] board,
boolean[][] visited, List<Integer> path, int i, int j, TrieNode root) {
if(i < 0 | i >= board.length || j < 0 || j >= board[0].length
|| visited[i][j] == true || root.children[board[i][j] - 'a'] == null) {
return;
}
//找下一个单词
root = root.children[board[i][j] - 'a'];
if(root.isWord) {
List<Integer> newPath = new ArrayList<>(path);
newPath.add(i * board[0].length + j);
words.add(newPath);
return;
}
//回溯标记
visited[i][j] = true;
path.add(i * board[0].length + j);
for (int k = 0; k < 4; k ++) {
getNextWords(words, board, visited, path, i + dx[k], j + dy[k], root);
}
path.remove(path.size() - 1);
visited[i][j] = false;
}
}
Boggle Game的更多相关文章
- Programming Assignment 4: Boggle
编程作业四 作业链接:Boggle & Checklist 我的代码:BoggleSolver.java 问题简介 Boggle 是一个文字游戏,有 16 个每面都有字母的骰子,开始随机将它们 ...
- uvalive 7299 Boggle
Boggle is a game in which 16 dice with letters on each side are placed into a 4 × 4 grid. Players th ...
- Trie树 + DFS - CSU 1457 Boggle
Boggle Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1457 Mean: 给定n个串,有m个询问. 每个询问 ...
- UVa - 11283 - PLAYING BOGGLE
先上题目 Problem F PLAYING BOGGLE Boggle® is a classic word game played on a 4 by 4 grid of letters. The ...
- 《算法问题实战策略》 BOGGLE
oj地址是韩国网站 连接比较慢 https://algospot.com/judge/problem/read/BOGGLE大意如下 输入输出 输入 URLPM XPRET GIAET XTNZY X ...
- GCPC 2013_A Boggle DFS+字典树 CSU 1457
上周比赛的题目,由于那个B题被神编译器的优化功能给卡了,就没动过这个题,其实就是个字典树嘛.当然,由于要在Boggle矩阵里得到初始序列,我还一度有点虚,不知道是用BFS还是DFS,最后发现DFS要好 ...
- UVALive 7299 Boggle(深搜的姿势)
一开始确实是我的锅,我把题意理解错了,以为是一个q周围没有q的时候才可以当时qu,其实是只要碰到q,他就是qu,所以我们也可以通过预处理的方式,把字典中的不满足qu连在一起的直接去掉. 后来的各种TI ...
- DFS csu1719 Boggle
传送门:id=1719">点击打开链接 题意:真正的题意是,告诉你一些字符串.然后告诉你非常多个字符格子,问这些字符串是否能在字符格子中连起来,在格子中对角线也觉得是连在一起的.假设格 ...
- Coursera 算法二 week 4 Boggle
这次的作业主要用到了单词查找树和深度优先搜索. 1.在深度优先搜索中,在当前层的递归调用前,将marked数组标记为true.当递归调用返回到当前层时,应将marked数组标记为false.这样既可以 ...
- 玲珑OJ 1082:XJT Loves Boggle(爆搜)
http://www.ifrog.cc/acm/problem/1082 题意:给出的单词要在3*3矩阵里面相邻连续(相邻包括对角),如果不行就输出0,如果可行就输出对应长度的分数. 思路:爆搜,但是 ...
随机推荐
- vs2015 出现的错误lnk:200:-main已在ax.obj中定义
原因是:一个项目里只能有一个main函数, 如果出现 error:LNK200 的错误,那么需要检查你是不是有两个源代码文件中都定义了main函数. 解决方案: 把其中的一个main函数删掉
- FMC与FPGA双口ram通讯
硬件环境:ARM+FPGA通过FMC互联,STM32F767和 EP4CE15F23I7 FMC设置,STM的系统时钟HCLK为216MHz /* FMC initialization functio ...
- 【Linux】CentOS7 打开关闭防火墙及端口
一.centos7版本对防火墙进行加强,不再使用原来的iptables,启用firewalld1.firewalld的基本使用启动: systemctl start firewalld查状态:syst ...
- 颜色rgba和16进制
今天阅读代码的时候看到了一个实现颜色渐变的效果,不同于以往使用函数实现的颜色渐变,这个是规律的递增rgba里面的几个参数完成的,看起来就像是等差数列一样.没想到还能这样来,简单的了解了一下 rgba的 ...
- 一,python编程100例
1.有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? #有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? number = (1 ,2,3,4) ...
- 2019-07-29 ThinkPHP简单的增删改查
在model里面,建立表名Model.class.php的控制器,用以连接数据表,代码如下 namespace Home\Model; use Think\Model; class NewsModel ...
- vuex简单化理解和安装使用
1.简单化理解 首先你要明白 vuex 的目的 就是为了 集中化的管理项目中 组件所有的 数据状态 (state) 0. 第一步你要明白 , store 的重要性 , store 类似一个中央基站, ...
- Nuxt项目搭建到发布部署
一.项目目录结构: 方式1.直接利用官方提供好的脚手架工具进行搭建:npx create-nuxt-app <项目名> 目录会是这样(具体目录都放些什么,请看官方文档,详细): 方式2.手 ...
- 前端工程师拿到全新的 Mac 需要做哪些准备
最近苹果退出了新款 Mac,用了3年15款Pro之后,终于盼到18款的降价,于是含泪更新换代 但是每次换电脑,重装环境的好多东西记不清,于是记个笔记 一.终端 安装 zsh sh -c "$ ...
- 为什么MES系统要定制化?看这三家汽车供应商的苦恼
很多企业对于为什么要对MES系统进行选择和定制化很不理解,今天,小编通过一个故事给大家进行阐述—— 故事背景: 汽车电子行业的三家企业A,B,C. A是整车厂一级供应商,主要产品为汽车电子配电盒. B ...