您的位置:首页 > 其它

LeetCode--word Search问题

2014-11-09 09:14 183 查看
Word Search

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
.

问题地址:https://oj.leetcode.com/problems/word-search/

-----------------------------------------------------------------------------------------------------

编程思路:

1,初始化操作:
board 、 word 、board的行数、列数、记录board中各位置的状态(这个很重要)

2,迭代board,比较board当前位置与word当前位置的值是否相等,相等走-3-,不等,继续迭代

3,按各方向寻找下一个字符,(这里用的是递归的思想)

循环四个方向,符合则停止,不符合,继续

4,下一个字符的具体方法:

定义新位置的变量

1,获取此方向上的board位置:

if(方向==0)上 ii=i-1,jj=j;。。。

5,如果ii jj在board的范围内,且此位置的字符之前尚未用过,进行 6

6,比较值,等 递归;不等,判断方向是不是没有了,是已经全部找结束,判断是否回退;不是,则返回 false ,继续下一个方向

实现代码如下:

public class SearchWord20141109 {

public static int n;
public static int m;
public static int[][] isenable;

// 递归方式有问题,原先是一直递归下去的,按道理方向和回退应该是退出递归的过程,而不是再叠加深入递归
public static boolean exist(char[][] board, String word) {
// 获取维数
n = board.length;
m = board[0].length;
isenable = new int
[m];

for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
isenable[i][j] = -1;// -1为可用
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (board[i][j] == word.charAt(0)) {
if (word.length() < 2) {
return true;
}
// 下一步
if (AllnextDerection(1, i, j, board, word))
return true;
else
isenable[i][j] = -1;
}
}
}
return false;
}

public static boolean AllnextDerection(int word_i, int i, int j, char[][] board, String word) {
for (int j2 = 0; j2 < 4; j2++) {
// -1为尚未使用,其他为方向
isenable[i][j] = j2;
if (nextPath(word_i, i, j, j2, board, word))
return true;
else
isenable[i][j] = -1;
}
return false;
}

public static boolean nextPath(int word_i, int i, int j, int derection, char[][] board, String word) {
int ii = 0, jj = 0;
switch (derection) {
case 0:
ii = i - 1;
jj = j;
break;
case 1:
ii = i;
jj = j + 1;
break;
case 2:
ii = i + 1;
jj = j;
break;
case 3:
ii = i;
jj = j - 1;
break;
}
if (0 <= ii && ii < n && 0 <= jj && jj < m && isenable[ii][jj] == -1) {
if (board[ii][jj] == word.charAt(word_i)) {
word_i = word_i + 1;
// 第一次,这个地方未注意,字符串到头了,应该结束了。
if (word_i == word.length())
return true;
// 第三次,这个地方,进入下一层,记录进入的方向
isenable[ii][jj] = derection;
return AllnextDerection(word_i, ii, jj, board, word);
} else {
if (derection == 3) {
if (word_i != 1) {
// 回退到最初位置
// 第四次,回退后的位置,其标记应该重置为可用
isenable[i][j] = -1;
}
return false;
} else {
return false;
}
}
} else {
if (derection == 3) {
if (word_i != 1) {
// 回退到最初位置
// 第四次,回退后的位置,其标记应该重置为可用
isenable[i][j] = -1;
}
return false;
} else {
return false;
}
}
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// char[][] board = { { 'C', 'A', 'A'}, { 'A', 'A', 'A' }, { 'B', 'C',
// 'D'} };
//char[][] board = { { 'a', 'a', 'b', 'a', 'a', 'b' }, { 'b', 'a', 'b', 'a', 'b', 'b' }, { 'b', 'a', 'b', 'b', 'b', 'b' }, { 'a', 'a', 'b', 'a', 'b', 'a' },
//		{ 'b', 'b', 'a', 'a', 'a', 'b' }, { 'b', 'b', 'b', 'a', 'b', 'a' } };
//String word = "AAB";
char[][] board = {{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
String word = "ABCB";
System.out.println(exist(board, word));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: