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,如果可行就输出对应长度的分数. 思路:爆搜,但是 ...
随机推荐
- IAR_STM32_CCM内存使用
在IAR中,硬件环境为STM32F464单片机,额外的CCM不能用DMA访问,但可以作为内部额外扩充RAM使用 修改文件中的内容为: define symbol __ICFEDIT_region_RO ...
- 百度前端技术学院-task1.3源代码
因为其中有图片,所以就给有图片的位置加了边框和设置了大小,这样哪怕图片不显示也可以知道在哪里. <!DOCTYPE html> <html> <head> < ...
- Flask-SQLAlchemy 与 SQL Alchemy 的区别、联系
目录 一.SQL Alchemy 1. SQL Alchemy 基本操作 1.1 连接数据库 1.2 声明映射(建立数据库表的模型) 1.3 创建映射类的实例 1.4 创建会话 1.5 单表的增删改查 ...
- PTA 甲级 1139
https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 其实这道题目不难,但是有很多坑点! 首先数据 ...
- 推送一个docker 使用阿里docker hub
阿里docker hub 地址 打开容器镜像服务页https://cr.console.aliyun.com/cn-qingdao/namespaces 这个是我的私有库 配置加速 我这边用的也是阿里 ...
- C#里面如何判断一个Object是否是某种类型
第一种方法 var isA = oldObject.GetType() == typeof(Dictionary<string, string>) 第二种方法 var isB = oldO ...
- CMPP服务端源码
CMPP服务端,带数据库,可以接收第三方CMPP客户端的短信,并存入数据库,结合我的cmpp客户端服务程序,将可以实现接收第三方SP的短信并转发到网关实现发送,并将状态报告.上行短信转发给第三方SP, ...
- 高亮显示小Demo
public class ItemSearchImpl implements ItemSearchService { /** * 搜索 * * @param paramMap * @return */ ...
- CENTOS7-JAVA模拟CPU占用高及排查( 转)
环境 centos7 1核2GB Java8 模拟cpu占用高 新建一个名为jvm-learn的springboot项目 模拟代码如下 import org.springframework.boot. ...
- ZooKeeper基础:快速部署
本文主要介绍ZooKeeper的快速部署安装,更多信息请参考ZooKeeper 概述 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是 ...