Question

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

For example,
Given board =

[
["ABCE"],
["SFCS"],
["ADEE"]
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

Solution

Because we have four choices to go, so this problem is not suitable to be solved by DP.

We use DFS here to traverse all possible paths.

One tricky note here is to check whether target equals to board[i][j] at every begining.

 public class Solution {
public int[][] directions = {{0, 1},{0, -1},{1, 0},{-1, 0}}; public boolean exist(char[][] board, String word) {
int m = board.length, n = board[0].length;
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < m; i++)
Arrays.fill(visited[i], false);
char target = word.charAt(0);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (dfsSearch(board, word, 0, visited, i, j))
return true;
}
}
return false;
} private boolean dfsSearch(char[][] board, String word, int index, boolean[][] visited, int startX, int startY) {
if (index >= word.length())
return true;
int m = board.length, n = board[0].length;
if (startX < 0 || startX >= m || startY < 0 || startY >= n)
return false;
char target = word.charAt(index);
if (board[startX][startY] != target)
return false;
if (visited[startX][startY])
return false;
visited[startX][startY] = true; // Traverse four directions
boolean result = dfsSearch(board, word, index + 1, visited, startX, startY + 1) ||
dfsSearch(board, word, index + 1, visited, startX, startY - 1) ||
dfsSearch(board, word, index + 1, visited, startX + 1, startY) ||
dfsSearch(board, word, index + 1, visited, startX - 1, startY);
// Reset visited[startX][startY]
visited[startX][startY] = false;
return result;
}
}

Word Search 解答的更多相关文章

  1. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

  2. [LeetCode] Word Search II 词语搜索之二

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  3. [LeetCode] Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  4. Leetcode: word search

    July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...

  5. Word Search I & II

    Word Search I Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  6. 【leetcode】Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. 51. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  9. 79. 212. Word Search *HARD* -- 字符矩阵中查找单词

    79. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be co ...

随机推荐

  1. bzoj3431 [Usaco2014 Jan]Bessie Slows Down

    Description [Brian Dean, 2014] Bessie the cow is competing in a cross-country skiing event at the wi ...

  2. Merge Sorted Array 解答

    Question Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...

  3. STL容器是否是线程安全的

    转载http://blog.csdn.net/zdl1016/article/details/5941330 STL的线程安全. 说一些关于stl容器的线程安全相关的话题. 一般说来,stl对于多线程 ...

  4. 虚拟机IOS开发环境搭建教程

    来源:http://www.cnblogs.com/xiaoyaoju/archive/2013/05/21/3091171.html 安装条件: 硬件:一台拥有支持虚拟技术的64位双核处理器和2GB ...

  5. java遍历泛型的方法

    一.List遍历 Java中List遍历有三种方法来遍历泛型,主要为: 1.for循环遍历 2.iterator遍历 3.foreach遍历 package com.gmail.lsgjzhuwei; ...

  6. C++入门学习——标准模板库之vector

    vector(向量容器),是 C++ 中十分实用一个容器.vector 之所以被觉得是一个容器,是由于它可以像容器一样存放各种类型的对象,简单地说,vector 是一个可以存放随意类型(类型可以是in ...

  7. Microsoft Visual C++ 不支持long long

    Microsoft Visual C++ 不支持long long 在C/C++中,64为整型一直是一种没有确定规范的数据类型.现今主流的编译器中,对64为整型的支持也是标准不一,形态各异.一般来说, ...

  8. a标签伪类的顺序

    在一次开发项目中,我用a链接来做效果,测试的时候发现,a:hover被点击后的效果就不再了!我百度才知道,原来在css写a链接也是有顺序之分的. 顺序应该是: a:link a标签还未被访问的状态: ...

  9. html5 视频

    HTML规定了一种通过video元素来包含视频的标准方法 一段HTML5视频不可缺少的元素有video.controls等.. 直到现在,仍然不存在一项在网页上显示视频的标准. 大多数视频是通过fla ...

  10. django中使用原生sql

    在Django中使用原生Sql主要有以下几种方式: 一:extra:结果集修改器,一种提供额外查询参数的机制 二:raw:执行原始sql并返回模型实例 三:直接执行自定义Sql ( 这种方式完全不依赖 ...