您的位置:首页 > 其它

矩阵中的路径--79 word Search--DFS回溯

2016-08-09 21:53 239 查看

请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串”bcced”的路径,但是矩阵中不包含”abcb”路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入该格子。

题目地址

http://www.nowcoder.com/practice/c61c6999eecb4b8f88a98f66b273a3cc?tpId=13&tqId=11218&rp=4&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

ac代码 (待优化)

bool flag;

class Solution {
public:

bool hasPath(char* matrix, int rows, int cols, char* str)
{
int len1 = strlen(matrix);
int len2 = strlen(str);
if (len1 == 0 || len2 == 0)
return false;

vector<bool> vis(rows*cols, false);
int len = (int)strlen(str);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (matrix[cols*i + j] == str[0]){
vis[cols*i + j] = true;
flag = false;
dfs(matrix, rows, cols, 1, str, len2, i, j, vis);
if (flag)
return true;
vis[cols*i + j] = false;
}
}
}
return false;
}

// 位置是否合理
bool isRight(int rows, int cols, int i, int j)
{
if (i < 0 || i >= rows || j < 0 || j >= cols)
return false;
return true;
}

void dfs(char* matrix, int rows, int cols, int k , char *str,int len, int curi, int curj, vector<bool> &vis)
{
if (k == len)
{
flag = true;
return ;
}
//上
if (isRight(rows, cols, curi - 1, curj) && !vis[cols*(curi - 1) + curj] && str[k] == matrix[cols*(curi - 1) + curj])
{
vis[cols*(curi - 1) + curj] = true;
dfs(matrix, rows, cols, k + 1, str, len, curi - 1, curj, vis);
vis[cols*(curi - 1) + curj] = false;
}
//下
if (isRight(rows, cols, curi + 1, curj) && !vis[cols*(curi + 1) + curj] && str[k] == matrix[cols*(curi + 1) + curj])
{
vis[cols*(curi + 1) + curj] = true;
dfs(matrix, rows, cols, k + 1, str, len, curi + 1, curj, vis);
vis[cols*(curi + 1) + curj] = false;
}
// 左
if (isRight(rows, cols, curi, curj - 1) && !vis[cols*curi + curj - 1] && str[k] == matrix[cols*curi + curj - 1])
{
vis[cols*curi + curj - 1] = true;
dfs(matrix, rows, cols, k + 1, str, len, curi, curj - 1, vis);
vis[cols*curi + curj - 1] = false;
}
// 右
if (isRight(rows, cols, curi, curj + 1) && !vis[cols*curi + curj + 1] && str[k] == matrix[cols*curi + curj + 1])
{
vis[cols*curi + curj + 1] = true;
dfs(matrix, rows, cols, k + 1, str, len, curi, curj + 1, vis);
vis[cols*curi + curj + 1] = false;
}

}// end dfs

};


优化代码1

bool flag;

class Solution {
public:

bool hasPath(char* matrix, int rows, int cols, char* str)
{
int len1 = strlen(matrix);
int len2 = strlen(str);
if (len1 == 0 || len2 == 0)
return false;

vector<bool> vis(rows*cols, false);
int len = (int)strlen(str);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (matrix[cols*i + j] == str[0])
{
vis[cols*i + j] = true;
flag = false;
dfs(matrix, rows, cols, 1, str, len2, i, j, vis);
if (flag)
return true;
vis[cols*i + j] = false;
}
}
}
return false;
}

// 位置是否合理
bool isRight(int rows, int cols, int i, int j)
{
if (i < 0 || i >= rows || j < 0 || j >= cols)
return false;
return true;
}

void dfs(char* matrix, int rows, int cols, int k , char *str,int len, int curi, int curj, vector<bool> &vis)
{
if (k == len)
{
flag = true;
return ;
}

int nextpos[4][2] = {
{ curi - 1, curj }, // 上
{ curi + 1, curj }, // 下
{ curi, curj - 1 }, // 左
{ curi, curj + 1 } // 右
};

for (int i = 0; i < 4; i++)
{
int nx = nextpos[i][0];
int ny = nextpos[i][1];

//位置合理 没有被访问,并且当前字符与str一致
if (isRight(rows, cols, nx, ny) && !vis[cols*nx + ny] && str[k] == matrix[cols*nx + ny])
{
vis[cols*nx + ny] = true;
dfs(matrix, rows, cols, k + 1, str, len, nx, ny, vis);
vis[cols*nx + ny] = false;
}
}

}// end dfs

};


79. Word Search(leetcode)

题目地址

https://leetcode.com/problems/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 =

[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]


word = “ABCCED”, -> returns true,

word = “SEE”, -> returns true,

word = “ABCB”, -> returns false.

ac代码(dfs回溯,待优化)

vector<vector<char>> mp;
int row, col;
string target;
int targetLen;

int dir[][2] = { { 0, 1 }, { 1, 0 },{ 0, -1 },{ -1, 0 } };

bool isRightPos(int x, int y)
{
if (x >= 0 && x < row && y >= 0 && y < col)
return true;
return false;
}

bool flag;

class Solution {
public:

void dfs(vector<bool> &vis, int m, int n, int depth)
{
if (flag)
return;
if (depth == targetLen)
{
flag = true;
return;
}

for (int i = 0; i < 4; i++)
{
int new_m = m + dir[i][0];
int new_n = n + dir[i][1];

if (isRightPos(new_m, new_n) && mp[new_m][new_n] == target[depth] && !vis[new_m*col + new_n])
{
vis[new_m*col + new_n] = true;
dfs(vis,new_m, new_n, depth + 1);
vis[new_m*col + new_n] = false;
}
if (flag)
return;
}
}

bool exist(vector<vector<char>>& board, string word) {
row = board.size();
if (row == 0)
return false;
col = board[0].size();
if (col == 0)
return false;

mp = board;
target = word;
targetLen = word.size();

for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
flag = false;
if (word[0] == board[i][j])
{
vector<bool> vis(row*col);
vis[i*col + j] = true;
dfs(vis,i,j, 1);
}
if (flag == true)
return true;
}
}

return false;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: