Coursera 算法二 week 4 Boggle
这次的作业主要用到了单词查找树和深度优先搜索。
1.在深度优先搜索中,在当前层的递归调用前,将marked数组标记为true。当递归调用返回到当前层时,应将marked数组标记为false。这样既可以使访问完当前节点之后不会访问到达当前节点路径上的节点,又可以从其它路径上访问到该节点。
2.当词典中没有单词以到达当前节点后的字符串为前缀时,即可停止深度优先搜索。
3.重写单词查找树,R为26,避免消耗过多的空间。另外keysWithPrefix函数改为hasKeysWithPrefix函数,因为不需要查找以某一字符串为前缀的所有单词,而只需判断是否有以某一字符串为前缀的单词。
- import java.util.HashSet;
- import java.util.Set;
- public class BoggleSolver
- {
- private TrieSET2 dictionary = new TrieSET2();
- public BoggleSolver(String[] dictionary)
- {
- for (String s : dictionary)
- this.dictionary.put(s);
- }
- private int m, n;
- public Iterable<String> getAllValidWords(BoggleBoard board)
- {
- Set<String> set = new HashSet<String>();
- m = board.rows();
- n = board.cols();
- for (int i = 0; i < m; i++)
- {
- for (int j = 0; j < n; j++)
- {
- marked = new boolean[m][n];
- dfs (set, board, i, j, "");
- }
- }
- return set;
- }
- private boolean[][] marked;
- private boolean isMarked(int i, int j)
- {
- if (i < 0 || i >= m || j < 0 || j >= n)
- return true;
- return marked[i][j];
- }
- private void dfs(Set<String> set, BoggleBoard board, int i, int j, String s)
- {
- char c = board.getLetter(i, j);
- if (c == 'Q') s += "QU";
- else s += c;
- if (!dictionary.hasKeysWithPrefix(s)) return;
- marked[i][j] = true;
- if (s.length() > 2 && dictionary.contains(s))
- set.add(s);
- for (int k = -1; k < 2; k++)
- {
- for (int l = -1; l < 2; l++)
- {
- if (!isMarked(i + k, j + l))
- dfs(set, board, i + k, j + l, s);
- }
- }
- marked[i][j] = false;
- }
- public int scoreOf(String word)
- {
- if (!dictionary.contains(word))
- return 0;
- int length = word.length();
- if (length < 3) return 0;
- else if (length < 5) return 1;
- else if (length == 5) return 2;
- else if (length == 6) return 3;
- else if (length == 7) return 5;
- else return 11;
- }
- }
- import edu.princeton.cs.algs4.Queue;
- public class TrieSET2
- {
- private static int R = 26;
- private Node root;
- private static class Node
- {
- private boolean hasWord;
- private Node[] next = new Node[R];
- }
- public void put(String key)
- {
- root = put(root, key, 0);
- }
- private int charAt(String s, int d)
- {
- return s.charAt(d) - 'A';
- }
- private Node put(Node x, String key, int d)
- {
- if (x == null) x = new Node();
- if (d == key.length())
- {
- x.hasWord = true;
- return x;
- }
- int c = charAt(key, d);
- x.next[c] = put(x.next[c], key, d+1);
- return x;
- }
- public boolean contains(String key)
- {
- Node x = get(root, key, 0);
- if (x == null) return false;
- return x.hasWord;
- }
- private Node get(Node x, String key, int d)
- {
- if (x == null) return null;
- if (d == key.length()) return x;
- int c = charAt(key, d);
- return get(x.next[c], key, d+1);
- }
- public boolean hasKeysWithPrefix(String pre)
- {
- return get(root, pre, 0) != null;
- }
- }
Coursera 算法二 week 4 Boggle的更多相关文章
- Coursera 算法二 week2 Seam Carving
这周作业设计到的算法是有向无环图的最短路径算法,只需要按照顶点的拓扑顺序去放松顶点即可.而在这个题目中拓扑顺序就是按照行的顺序或列的顺序. 用到的数据结构为一个二维数组picture同来存储每个像素的 ...
- Coursera 算法二 week 5 BurrowsWheeler
本打算周末完成这次作业,但没想到遇到了hard deadline,刚开始看不懂题意,后来发现算法4书上有个类似的问题,才理解了题意.最后晚上加班,上课加班,还好在11:35也就是课程结束前25分钟完成 ...
- Coursera 算法二 week 3 Baseball Elimination
这周的作业不需要自己写算法,只需要调用库函数就行,但是有些难以理解,因此用了不少时间. import edu.princeton.cs.algs4.FlowEdge; import edu.princ ...
- coursera 算法二 week 1 wordnet
这周的作业可谓是一波三折,但是收获了不少,熟悉了广度优先搜索还有符号图的建立.此外还知道了Integer.MAX_VALUE. SAP: 求v和w的大概思路是对v和w分别广度优先搜索,然后遍历图中每一 ...
- TensorFlow 入门之手写识别(MNIST) softmax算法 二
TensorFlow 入门之手写识别(MNIST) softmax算法 二 MNIST Fly softmax回归 softmax回归算法 TensorFlow实现softmax softmax回归算 ...
- 分布式共识算法 (二) Paxos算法
系列目录 分布式共识算法 (一) 背景 分布式共识算法 (二) Paxos算法 分布式共识算法 (三) Raft算法 分布式共识算法 (四) BTF算法 一.背景 1.1 命名 Paxos,最早是Le ...
- Floyd算法(二)之 C++详解
本章是弗洛伊德算法的C++实现. 目录 1. 弗洛伊德算法介绍 2. 弗洛伊德算法图解 3. 弗洛伊德算法的代码说明 4. 弗洛伊德算法的源码 转载请注明出处:http://www.cnblogs.c ...
- Dijkstra算法(二)之 C++详解
本章是迪杰斯特拉算法的C++实现. 目录 1. 迪杰斯特拉算法介绍 2. 迪杰斯特拉算法图解 3. 迪杰斯特拉算法的代码说明 4. 迪杰斯特拉算法的源码 转载请注明出处:http://www.cnbl ...
- Prim算法(二)之 C++详解
本章是普里姆算法的C++实现. 目录 1. 普里姆算法介绍 2. 普里姆算法图解 3. 普里姆算法的代码说明 4. 普里姆算法的源码 转载请注明出处:http://www.cnblogs.com/sk ...
随机推荐
- 未能写入输出文件 “c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\6ee8fd15\5fc973dd\App_Web_default.aspx.cdcab7d2.e1voeq0d.dll”--“拒绝访问
在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Fil ...
- BKMyFAQ
邮箱配置如图 发送格式: { "bk_app_code": "bk_monitor", #该字段可以查看文件:/data/install/.app.token ...
- [hdu 1671] Phone List - Trie
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- Java 8 Optional类使用的实践经验
前言 Java中空指针异常(NPE)一直是令开发者头疼的问题.Java 8引入了一个新的Optional类,使用该类可以尽可能地防止出现空指针异常. Optional 类是一个可以为null的容器对象 ...
- oracle基本命令
1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新用户的密码也可以不创建新 ...
- tomcat mamcached session共享方法
下载后输入命令安装命令: c:\memcached\memcached.exe -d install 然后再输入如下命令把其作为win service常驻启动: c:\memcached\memcac ...
- t.call is not a function
js 写错了... 代码: $("form").each({$(this).submit(function(){return false;})}); 丢了一个 function() ...
- BigDecimal默认用四舍五入方式
import java.math.BigDecimal; target.setWeight(source.getWeight().setScale(3, BigDecimal.ROUND_HALF_U ...
- redis常用
redis的key和string类型value限制均为512MB
- Repair 暴力
Description standard input/outputStatements Alex is repairing his country house. He has a rectangula ...