鱼C论坛

 找回密码
 立即注册
查看: 753|回复: 1

[技术交流] C++刷剑指offer(面试题12. 矩阵中的路径)【回溯算法】

[复制链接]
发表于 2020-4-6 12:36:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目描述:
  1. 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一格开始,每一步可以在矩阵中向左、右、上、下移动一格。如果一条路径经过了矩阵的某一格,那么该路径不能再次进入该格子。例如,在下面的3×4的矩阵中包含一条字符串“bfce”的路径(路径中的字母用加粗标出)。

  2. [["a","b","c","e"],
  3. ["s","f","c","s"],
  4. ["a","d","e","e"]]

  5. 但矩阵中不包含字符串“abfb”的路径,因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后,路径不能再次进入这个格子。

  6.  

  7. 示例 1:

  8. 输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
  9. 输出:true
  10. 示例 2:

  11. 输入:board = [["a","b"],["c","d"]], word = "abcd"
  12. 输出:false
  13. 提示:

  14. 1 <= board.length <= 200
  15. 1 <= board[i].length <= 200

  16. 来源:力扣(LeetCode)
  17. 链接:https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof
  18. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码



  1. #include <vector>
  2. #include <iostream>
  3. #include <string>

  4. using namespace std;

  5. bool dfs(vector<vector<char>>& board, string& word, int i,int j,int length){
  6.     if(i >= board.size() || j >= board[0].size() || i<0 || j<0 || length >= word.size()|| word[length]!=board[i][j]){
  7.         return false;
  8.     }
  9.     if(length == word.size()-1 && word[length] == board[i][j]){
  10.         return true;
  11.     }
  12.     char temp = board[i][j];
  13.     board[i][j] = '0';
  14.     bool flag = dfs(board,word,i,j+1,length+1)||dfs(board,word,i,j-1,length+1)||dfs(board,word,i+1,j,length+1)||dfs(board,word,i-1,j,length+1);
  15.     board[i][j] = temp;
  16.     return flag;
  17. }
  18.    
  19. bool solution(vector<vector<char> >& input, string target){
  20.         for(int i = 0; i < input.size(); i++){
  21.                 for(int j = 0; j < input[0].size(); j++){
  22.                         if(dfs(input, target, i, j, 0)){
  23.                                 return true;
  24.                         }
  25.                 }
  26.                
  27.         }
  28.         return false;
  29. }



  30. int main(void){
  31.         int row, col;
  32.         cout << "send row for the matrix" << endl;
  33.         cin >> row;
  34.         cin.clear();
  35.         cout << "send column for the matrix" << endl;
  36.         cin >> col;
  37.         cin.clear();
  38.         cout << "send the element for the matrix" << endl;
  39.         vector<vector<char> > input;
  40.         input.resize(row);
  41.         char a;
  42.         for(int i = 0; i < row; i++){
  43.                 for(int j = 0; j < col; j++){
  44.                         cin >> a;
  45.                         input[i].push_back(a);
  46.                 }
  47.         }
  48.         cin.clear();
  49.         cout << "print the matrix" << endl;
  50.         for(int i = 0; i < row; i++){
  51.         for(int j = 0; j < col; j++){
  52.             cout << input[i][j] << " ";
  53.         }
  54.         cout << endl;
  55.         }
  56.        
  57.         cout << "send the target" << endl;
  58.         string target;
  59.         cin >> target;

  60.        
  61.         bool res = solution(input, target);
  62.         cout << res << endl;
  63.         return 0;
  64. }
复制代码



注意事项:
1.参考链接:https://leetcode-cn.com/problems ... ian-sou-suo-by-z1m/
2.目标查找的字符串的第一个字符可以出现在matrix中的任意一个位置,因此用了两层for循环暴力搜索。
3.在两层for循环中使用回溯算法,回溯算法包括:剪枝、递归。
4.已经访问到的字符标记为0,否则没有找到后回溯将这个字符由0变为原来的字符。

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-6 12:51:49 | 显示全部楼层
这道题做的不好,还需要再做一遍。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 19:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表