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.

public class Solution {
public boolean exist(char[][] board, String word) {
if(word.length()==0)
return true;
if(board.length==0)
return false;
boolean[][] bol = new boolean[board.length][board[0].length]; for(int i=0;i<board.length;i++){
for(int j=0;j<board[i].length;j++){ if(board[i][j]==word.charAt(0)&&findExist(i,j,0,board,bol,word)){
return true;
}
}
}
return false; } private boolean findExist(int x, int y,int wa, char[][] board, boolean[][] bol, String word) {
if(wa>=word.length())
return true;
if(x<0||y<0)
return false;
if(x>=board.length||y>=board[x].length)
return false;
if(bol[x][y])
return false;
if(board[x][y]!=word.charAt(wa))
return false; bol[x][y]=true;
boolean re =findExist(x+1,y,wa+1,board,bol,word)||findExist(x,y+1,wa+1,board,bol,word)||
findExist(x-1,y,wa+1,board,bol,word)||findExist(x,y-1,wa+1,board,bol,word);
bol[x][y]=false;
return re;
}
}

【WordSearch】Word Search的更多相关文章

  1. 【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 ...

  2. 【leetcode】Word Search (middle)

    今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

  3. 【leetcode】Word Search II(hard)★

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

  4. 【Leetcode】【Medium】word search

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

  5. 【数组】word search

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  6. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  7. 【CF528D】Fuzzy Search(FFT)

    [CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...

  8. 【计算机视觉】Selective Search for Object Recognition论文阅读3

    Selective Search for Object Recoginition surgewong@gmail.com http://blog.csdn.net/surgewong       在前 ...

  9. 【HDU2222】Keywords Search(AC自动机)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

随机推荐

  1. LeetCode OJ——Minimum Depth of Binary Tree

    http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ 贡献了一次runtime error,因为如果输入为{}即空的时候,出现了c ...

  2. Python语言 介绍

    一.python介绍python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语 ...

  3. R语言table()函数

    R语言table()函数比较有用,两个示例尤其是混淆矩阵这个案例比较有用: 例子一:统计频次 z<-c(1,2,2,4,2,7,1,1);z1<-table(z);summary(z1); ...

  4. Codeforces 622F The Sum of the k-th Powers(数论)

    题目链接 The Sum of the k-th Powers 其实我也不懂为什么这么做的……看了无数题解觉得好厉害哇…… #include <bits/stdc++.h> using n ...

  5. NEERC15

    2015-2016 ACM-ICPC Northeastern European Regional Contest 再开一个新坑吧 目前姿势有限,C.H.I仍然处于弃坑状态 代码戳这里 Problem ...

  6. 第四期coding_group笔记_用CRF实现分词-词性标注

    一.背景知识 1.1 什么是分词? NLP的基础任务分为三个部分,词法分析.句法分析和语义分析,其中词法分析中有一种方法叫Tokenization,对汉字以字为单位进行处理叫做分词. Example ...

  7. 邁向IT專家成功之路的三十則鐵律 鐵律二十一:IT人用才之道-穿透

    在以道德為基礎的企業主管之人,其最根本的能力除了須要有洞悉事物的敏捷思維之外,眼光還必要有像水柱般一樣的穿山引石之能,如此不僅能夠為企業找到適才之人,更能為企業的永續經營奠定有如泰山般的基石.只可惜大 ...

  8. SilverLight: 数据绑定(1)-绑定到数据对象

    ylbtech-SilverLight-DataBinding: Binding to Data Objects(绑定到数据对象) 1.A, Building  a Data Object(创建一个数 ...

  9. PS 抠图如何使用通道法处理头发

      通道抠图法抠出美女飘逸头发-PS抠图实例教程 抠图更换背景后效果图 通道抠图法抠出美女飘逸头发-PS抠图实例教程 教程步骤: 1  打开原图,进入通道面板. 通道抠图法抠出美女飘逸头发-PS抠图实 ...

  10. vue2.0 vue-router

    一.SPA中路由的简单实现 main.js import Vue from 'vue' import App from './App' import VueRouter from 'vue-route ...