码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Word Search

时间:2015-01-23 18:30:06      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:java   leetcode   深搜   回溯   

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 {
	boolean status[][];
	int move[][] = { { -1, 0, 1, 0 }, { 0, -1, 0, 1 } };
	public boolean exist(char[][] board, String word) {
		status = new boolean[board.length][board[0].length];
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board[0].length; j++) {
				if (board[i][j] == word.charAt(0)) {
					if(dfs(board, word, i, j, 0)) return true;
				}
			}
		}
		return false;
	}

	private boolean dfs(char[][] board, String word, int x, int y, int index) {
		if (index >= word.length()) {
			return true;
		}
		//【剪枝】超出边界,字符不匹配,已经被搜查过
		if(x < 0 || x >= board.length || y < 0 || y >= board[0].length||word.charAt(index) != board[x][y]||status[x][y]) return false;
		status[x][y] = true;
		for (int k = 0; k <= 3; k++) {
			int nx = x + move[0][k];
			int ny = y + move[1][k];
				if(dfs(board, word, nx, ny, index + 1)) return true;
		}
		//回溯
		status[x][y] = false;
		return false;
	}
}



[LeetCode]Word Search

标签:java   leetcode   深搜   回溯   

原文地址:http://blog.csdn.net/guorudi/article/details/43057487

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!