矩阵中的路径/79. 单词搜索

矩阵中的路径

回溯法

class Solution {
public:
	bool hasPath(char* matrix, int rows, int cols, char* str)
	{
		if (!matrix || rows <= 0 || cols <= 0 || !str)return false;
		int *visited = new int[rows*cols];
		memset(visited, 0, rows*cols);
		int lenpath = 0;//从零步开始

		for (int i = 0; i < rows; i++)
		for (int j = 0; j<cols; j++)
		if (hasPathCore(matrix, rows, cols, i, j, str, visited, lenpath))
			return true;
		delete[] visited;
		return false;
	}

	bool hasPathCore(char* matrix, int rows, int cols, int i, int j, char* str, int *visited, int& lenpath)
	{
		if (str[lenpath] == '\0')//递归的条件
			return true;

		bool haspath = false;
		if (i >= 0 && j >= 0 && i < rows && j < cols && matrix[i*cols + j] == str[lenpath] && visited[i*cols + j]!=1)
		{
			visited[i*cols + j] = 1;
			lenpath++;
			haspath = hasPathCore(matrix, rows, cols, i + 1, j, str, visited, lenpath)
				|| hasPathCore(matrix, rows, cols, i - 1, j, str, visited, lenpath)
				|| hasPathCore(matrix, rows, cols, i, j + 1, str, visited, lenpath)
				|| hasPathCore(matrix, rows, cols, i, j - 1, str, visited, lenpath);
			if (!haspath)
			{
				lenpath--;
				visited[i*cols + j] = 0;
			}
		}
		return haspath;
	}
};

为什么写成if(....&&!visited[i*cols+j])牛客网通过不了
这个小问题花了一下午检查出来。

问题出现在memset:
因某些编译器分配空间时,内存中默认值并不为0.

int *visited = new int[rows*cols];
		memset(visited, 0, rows*cols);
		for (int i = 0; i < rows*cols; i++)
		cout << visited[i]<<endl;

以下是打印的visitted(整型)
在这里插入图片描述


我试试吧visited改为bool型,

bool *visited = new bool[rows*cols];
		memset(visited, 0, rows*cols);
		for (int i = 0; i < rows*cols; i++)
		cout << visited[i]<<endl;

结果却是
在这里插入图片描述

79. 单词搜索

28 ms :

class Solution {
 public:
	 bool help(vector<vector<char>>& board, vector<vector<bool>>& visited,string s, int i, int j, int lenpath)
	 {
		 int row = board.size();
		 int col = board[0].size();
		 if (lenpath == s.length())
			 return true;

		 bool haspath = false;
		 if (i >= 0 && j >= 0 && i < row&&j < col&&board[i][j] == s[lenpath] && visited[i][j] == false)
		 {
			 visited[i][j] = true;
			 lenpath++;
			 haspath = help(board, visited, s, i + 1, j, lenpath) 
				 || help(board, visited, s, i -1, j, lenpath)
				 || help(board, visited, s, i , j+1, lenpath)
				 || help(board, visited, s, i , j-1, lenpath);
			 if (!haspath)
			 {
				 lenpath--;
				 visited[i][j] = false;
			 }
			
		 }
		 return haspath;
	 }
	 bool exist(vector<vector<char>>& board, string word) {
		 if (board.empty())return false;
		 int row = board.size();
		 int col = board[0].size();
         //初始化visited
		vector<vector<bool>> visited(row, vector<bool>(col, false));
		 for(int i=0;i<row;i++)
			 for (int j = 0; j < col; j++)
			 {
				
				 if (help(board, visited, word, i, j, 0))
					 return true;
			 }
		 return false;
		  
	 }
 };
 static const auto speedup = []() {
	 std::ios::sync_with_stdio(false);
	 std::cin.tie(nullptr);
	 return nullptr;
 }();

来一个最fast的code:

class Solution {
public:
    //执行用时: 8 ms, 在Word Search的C++提交中击败了100.00% 的用户
    bool dfs (vector<vector<char>>& board, string& word,
             int size, int x, int y, vector<vector<int>>& f){
        if (size == word.size()){
            return true;
        }//outofbound
        if (x < 0 || x >= board.size() 
           || y < 0 || y > board[0].size() 
           || board[x][y] != word[size]){
            return false;
        }
        if (f[x][y] == 0) {
            f[x][y] = 1;
            if (dfs(board, word, size+1, x+1, y, f) 
               || dfs(board, word, size+1, x-1, y, f) 
               || dfs(board, word, size+1, x, y+1, f) 
               || dfs(board, word, size+1, x, y-1, f)){
                return true;
            }
            f[x][y] = 0;
        }
        return false;
    }
    
    bool exist(vector<vector<char>>& board, string& word) {
        if (board.empty() || word.empty()){
            return false;
        }
        int row = board.size(), col = board[0].size();
        vector<vector<int>> f(row, vector<int>(col, 0));
        for (int i = 0; i < row; ++i){
            for (int j = 0; j < col; ++j){
                if (dfs(board, word, 0, i,j, f)){
                    return true;
                }
            }
        }
        return false;
    }
};

static const auto __lamda = []() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    return nullptr;
}();
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值