Description

Given a 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的更多相关文章

  1. Programming Assignment 4: Boggle

    编程作业四 作业链接:Boggle & Checklist 我的代码:BoggleSolver.java 问题简介 Boggle 是一个文字游戏,有 16 个每面都有字母的骰子,开始随机将它们 ...

  2. 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 ...

  3. Trie树 + DFS - CSU 1457 Boggle

    Boggle Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1457 Mean: 给定n个串,有m个询问. 每个询问 ...

  4. UVa - 11283 - PLAYING BOGGLE

    先上题目 Problem F PLAYING BOGGLE Boggle® is a classic word game played on a 4 by 4 grid of letters. The ...

  5. 《算法问题实战策略》 BOGGLE

    oj地址是韩国网站 连接比较慢 https://algospot.com/judge/problem/read/BOGGLE大意如下 输入输出 输入 URLPM XPRET GIAET XTNZY X ...

  6. GCPC 2013_A Boggle DFS+字典树 CSU 1457

    上周比赛的题目,由于那个B题被神编译器的优化功能给卡了,就没动过这个题,其实就是个字典树嘛.当然,由于要在Boggle矩阵里得到初始序列,我还一度有点虚,不知道是用BFS还是DFS,最后发现DFS要好 ...

  7. UVALive 7299 Boggle(深搜的姿势)

    一开始确实是我的锅,我把题意理解错了,以为是一个q周围没有q的时候才可以当时qu,其实是只要碰到q,他就是qu,所以我们也可以通过预处理的方式,把字典中的不满足qu连在一起的直接去掉. 后来的各种TI ...

  8. DFS csu1719 Boggle

    传送门:id=1719">点击打开链接 题意:真正的题意是,告诉你一些字符串.然后告诉你非常多个字符格子,问这些字符串是否能在字符格子中连起来,在格子中对角线也觉得是连在一起的.假设格 ...

  9. Coursera 算法二 week 4 Boggle

    这次的作业主要用到了单词查找树和深度优先搜索. 1.在深度优先搜索中,在当前层的递归调用前,将marked数组标记为true.当递归调用返回到当前层时,应将marked数组标记为false.这样既可以 ...

  10. 玲珑OJ 1082:XJT Loves Boggle(爆搜)

    http://www.ifrog.cc/acm/problem/1082 题意:给出的单词要在3*3矩阵里面相邻连续(相邻包括对角),如果不行就输出0,如果可行就输出对应长度的分数. 思路:爆搜,但是 ...

随机推荐

  1. [转帖]Redis持久化--Redis宕机或者出现意外删库导致数据丢失--解决方案

    Redis持久化--Redis宕机或者出现意外删库导致数据丢失--解决方案 https://www.cnblogs.com/xlecho/p/11834011.html echo编辑整理,欢迎转载,转 ...

  2. [转帖]PostgreSQL 参数调整(性能优化)

    PostgreSQL 参数调整(性能优化) https://www.cnblogs.com/VicLiu/p/11854730.html 知道一个 shared_pool 文章写的挺好的 还没仔细看 ...

  3. 如何选择CPU

    一.品牌: 选择哪家公司的处理器,AMD公司和inter公司的处理器相比较,AMD在三维制作.游戏应用.和视频处理方面突出,inter的处理器在商业应用.多媒体应用.平面设计方面有优势,性能方面,同档 ...

  4. Grafana邮件报警

    一.概述 报警是Grafana的一项革命性功能,它让Grafana从一个数据可视化工具变成一个真正的任务监控工具.报警规则可以使用现有的图表控制面板设置,阈值可以通过拖拉右边的线控制,非常简单.Gra ...

  5. CentOS 7.0 更改SSH 远程连接 端口号

    许多学习过redhat 7的同学们,在使用centos的时候总会遇到一些问题,因为centos在安装时会默认开启一些服务,今天我们就来更改下centos 7.0的SSH端口. 操作步骤: 远程登录到c ...

  6. .NET / C# EF中的基础操作(CRUD)

    查 public List<users> Querys() { datatestEntities db = new datatestEntities(); var a = db.users ...

  7. C# vb .NET读取识别条形码线性条码code128

    code128是比较常见的条形码编码规则类型的一种.如何在C#,vb等.NET平台语言里实现快速准确读取该类型条形码呢?答案是使用SharpBarcode! SharpBarcode是C#快速高效.准 ...

  8. spring 请求参数和路径变量

    请求参数和路径变量:客户端传递参数给服务端的两种方式 请求参数可以发送值传递给服务器,请求参数采用key=value的形式并使用“&”符号进行参数间的分隔,例如: http://localho ...

  9. 【转】Webpack 快速上手(下)

    由于文章篇幅较长,为了更好的阅读体验,本文分为上.中.下三篇: 上篇介绍了什么是 webpack,为什么需要 webpack,webpack 的文件输入和输出 中篇介绍了 webpack 在输入和输出 ...

  10. npm ERR! code ELIFECYCLE webpack-dev-server --inline --progress --config build/webpack.dev.conf.js`

    “E:\Program Files\JetBrains\WebStorm 2018.1.4\bin\runnerw.exe” G:\node\nodejs\node.exe G:\node\nodej ...