这次的作业主要用到了单词查找树和深度优先搜索。

1.在深度优先搜索中,在当前层的递归调用前,将marked数组标记为true。当递归调用返回到当前层时,应将marked数组标记为false。这样既可以使访问完当前节点之后不会访问到达当前节点路径上的节点,又可以从其它路径上访问到该节点。

2.当词典中没有单词以到达当前节点后的字符串为前缀时,即可停止深度优先搜索。

3.重写单词查找树,R为26,避免消耗过多的空间。另外keysWithPrefix函数改为hasKeysWithPrefix函数,因为不需要查找以某一字符串为前缀的所有单词,而只需判断是否有以某一字符串为前缀的单词。

  1. import java.util.HashSet;
  2. import java.util.Set;
  3.  
  4. public class BoggleSolver
  5. {
  6. private TrieSET2 dictionary = new TrieSET2();
  7.  
  8. public BoggleSolver(String[] dictionary)
  9. {
  10. for (String s : dictionary)
  11. this.dictionary.put(s);
  12. }
  13. private int m, n;
  14. public Iterable<String> getAllValidWords(BoggleBoard board)
  15. {
  16. Set<String> set = new HashSet<String>();
  17. m = board.rows();
  18. n = board.cols();
  19. for (int i = 0; i < m; i++)
  20. {
  21. for (int j = 0; j < n; j++)
  22. {
  23. marked = new boolean[m][n];
  24. dfs (set, board, i, j, "");
  25. }
  26. }
  27. return set;
  28. }
  29. private boolean[][] marked;
  30. private boolean isMarked(int i, int j)
  31. {
  32. if (i < 0 || i >= m || j < 0 || j >= n)
  33. return true;
  34. return marked[i][j];
  35. }
  36. private void dfs(Set<String> set, BoggleBoard board, int i, int j, String s)
  37. {
  38. char c = board.getLetter(i, j);
  39. if (c == 'Q') s += "QU";
  40. else s += c;
  41. if (!dictionary.hasKeysWithPrefix(s)) return;
  42. marked[i][j] = true;
  43. if (s.length() > 2 && dictionary.contains(s))
  44. set.add(s);
  45. for (int k = -1; k < 2; k++)
  46. {
  47. for (int l = -1; l < 2; l++)
  48. {
  49. if (!isMarked(i + k, j + l))
  50. dfs(set, board, i + k, j + l, s);
  51. }
  52. }
  53. marked[i][j] = false;
  54. }
  55.  
  56. public int scoreOf(String word)
  57. {
  58. if (!dictionary.contains(word))
  59. return 0;
  60. int length = word.length();
  61. if (length < 3) return 0;
  62. else if (length < 5) return 1;
  63. else if (length == 5) return 2;
  64. else if (length == 6) return 3;
  65. else if (length == 7) return 5;
  66. else return 11;
  67. }
  68. }
  1. import edu.princeton.cs.algs4.Queue;
  2.  
  3. public class TrieSET2
  4. {
  5. private static int R = 26;
  6. private Node root;
  7.  
  8. private static class Node
  9. {
  10. private boolean hasWord;
  11. private Node[] next = new Node[R];
  12. }
  13.  
  14. public void put(String key)
  15. {
  16. root = put(root, key, 0);
  17. }
  18.  
  19. private int charAt(String s, int d)
  20. {
  21. return s.charAt(d) - 'A';
  22. }
  23.  
  24. private Node put(Node x, String key, int d)
  25. {
  26. if (x == null) x = new Node();
  27. if (d == key.length())
  28. {
  29. x.hasWord = true;
  30. return x;
  31. }
  32. int c = charAt(key, d);
  33. x.next[c] = put(x.next[c], key, d+1);
  34. return x;
  35. }
  36.  
  37. public boolean contains(String key)
  38. {
  39. Node x = get(root, key, 0);
  40. if (x == null) return false;
  41. return x.hasWord;
  42. }
  43.  
  44. private Node get(Node x, String key, int d)
  45. {
  46. if (x == null) return null;
  47. if (d == key.length()) return x;
  48. int c = charAt(key, d);
  49. return get(x.next[c], key, d+1);
  50. }
  51.  
  52. public boolean hasKeysWithPrefix(String pre)
  53. {
  54. return get(root, pre, 0) != null;
  55. }
  56. }

Coursera 算法二 week 4 Boggle的更多相关文章

  1. Coursera 算法二 week2 Seam Carving

    这周作业设计到的算法是有向无环图的最短路径算法,只需要按照顶点的拓扑顺序去放松顶点即可.而在这个题目中拓扑顺序就是按照行的顺序或列的顺序. 用到的数据结构为一个二维数组picture同来存储每个像素的 ...

  2. Coursera 算法二 week 5 BurrowsWheeler

    本打算周末完成这次作业,但没想到遇到了hard deadline,刚开始看不懂题意,后来发现算法4书上有个类似的问题,才理解了题意.最后晚上加班,上课加班,还好在11:35也就是课程结束前25分钟完成 ...

  3. Coursera 算法二 week 3 Baseball Elimination

    这周的作业不需要自己写算法,只需要调用库函数就行,但是有些难以理解,因此用了不少时间. import edu.princeton.cs.algs4.FlowEdge; import edu.princ ...

  4. coursera 算法二 week 1 wordnet

    这周的作业可谓是一波三折,但是收获了不少,熟悉了广度优先搜索还有符号图的建立.此外还知道了Integer.MAX_VALUE. SAP: 求v和w的大概思路是对v和w分别广度优先搜索,然后遍历图中每一 ...

  5. TensorFlow 入门之手写识别(MNIST) softmax算法 二

    TensorFlow 入门之手写识别(MNIST) softmax算法 二 MNIST Fly softmax回归 softmax回归算法 TensorFlow实现softmax softmax回归算 ...

  6. 分布式共识算法 (二) Paxos算法

    系列目录 分布式共识算法 (一) 背景 分布式共识算法 (二) Paxos算法 分布式共识算法 (三) Raft算法 分布式共识算法 (四) BTF算法 一.背景 1.1 命名 Paxos,最早是Le ...

  7. Floyd算法(二)之 C++详解

    本章是弗洛伊德算法的C++实现. 目录 1. 弗洛伊德算法介绍 2. 弗洛伊德算法图解 3. 弗洛伊德算法的代码说明 4. 弗洛伊德算法的源码 转载请注明出处:http://www.cnblogs.c ...

  8. Dijkstra算法(二)之 C++详解

    本章是迪杰斯特拉算法的C++实现. 目录 1. 迪杰斯特拉算法介绍 2. 迪杰斯特拉算法图解 3. 迪杰斯特拉算法的代码说明 4. 迪杰斯特拉算法的源码 转载请注明出处:http://www.cnbl ...

  9. Prim算法(二)之 C++详解

    本章是普里姆算法的C++实现. 目录 1. 普里姆算法介绍 2. 普里姆算法图解 3. 普里姆算法的代码说明 4. 普里姆算法的源码 转载请注明出处:http://www.cnblogs.com/sk ...

随机推荐

  1. 未能写入输出文件 “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 ...

  2. BKMyFAQ

    邮箱配置如图 发送格式: { "bk_app_code": "bk_monitor", #该字段可以查看文件:/data/install/.app.token ...

  3. [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 ...

  4. Java 8 Optional类使用的实践经验

    前言 Java中空指针异常(NPE)一直是令开发者头疼的问题.Java 8引入了一个新的Optional类,使用该类可以尽可能地防止出现空指针异常. Optional 类是一个可以为null的容器对象 ...

  5. oracle基本命令

    1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新用户的密码也可以不创建新 ...

  6. tomcat mamcached session共享方法

    下载后输入命令安装命令: c:\memcached\memcached.exe -d install 然后再输入如下命令把其作为win service常驻启动: c:\memcached\memcac ...

  7. t.call is not a function

    js 写错了... 代码: $("form").each({$(this).submit(function(){return false;})}); 丢了一个 function() ...

  8. BigDecimal默认用四舍五入方式

    import java.math.BigDecimal; target.setWeight(source.getWeight().setScale(3, BigDecimal.ROUND_HALF_U ...

  9. redis常用

    redis的key和string类型value限制均为512MB

  10. Repair 暴力

    Description standard input/outputStatements Alex is repairing his country house. He has a rectangula ...