include "stdafx.h"

#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<stack>
using namespace std; class Solution {
public: bool hasPath(char* matrix, int rows, int cols, char* str)
{
vector<vector<bool>> visited(rows, vector<bool>(cols,false));
vector<vector<char>> mat(rows, vector<char>(cols, 'a'));
vector<char> s;
for (int i = 0;i < rows;i++)
{
for (int j = 0;j < cols;j++)
{
mat[i][j] =matrix[ i*cols + j];
}
}
for (int i = 0;i < strlen(str);i++)
{
s.push_back(str[i]);
}
bool flag = false;
for (int i = 0;i < rows;i++)
{ for (int j = 0;j < cols;j++)
{
if (mat[i][j] == str[0])
{
if (getPath(mat, i, j, s, 0, visited) == true)
{
flag = true;
break;
}
}
if (flag == true)
{
break;
}
}
}
return flag;
}
bool getPath(vector<vector<char>> mat, int rows, int cols, vector<char> s,int num, vector<vector<bool>> visited)
{
if (num == s.size() - 1 && mat[rows][cols] == s[num])
return true;
if (mat[rows][cols] == s[num])
{
visited[rows][cols] = true; //已经被访问
num++;
bool left = false;
bool right = false;
bool up = false;
bool down = false;
//向左走
if (cols - 1 >= 0 && visited[rows][cols - 1] == false)
left = getPath(mat, rows, cols - 1, s, num, visited);
//向右走
if (cols + 1 < mat[0].size() && visited[rows][cols + 1] == false)
right = getPath(mat, rows, cols + 1, s, num, visited);
//向上走
if (rows - 1 >= 0 && visited[rows - 1][cols] == false)
up = getPath(mat, rows - 1, cols, s, num, visited);
//向下走
if (rows + 1 < mat.size() && visited[rows + 1][cols] == false)
down = getPath(mat, rows + 1, cols, s, num, visited);
visited[rows][cols] = false;
return left || right || up || down;
}
else
{
return false;
} } };
int main()
{
Solution s;
cout << s.hasPath("ABCESFCSADEE", 3, 4, "ABCCED") << endl; return 0;
}

最新文章

  1. angular源码分析:angular中jqLite的实现——你可以丢掉jQuery了
  2. AngularJS_对象数组-filter-orderBy
  3. 七:zookeeper与paxos的分析
  4. Red Hat Linux认证
  5. WPF 多线程处理(6)
  6. Ansible好像很好玩的样子哟
  7. 自定义一个searchBar
  8. (原)ubuntu16中编译boost1.61.0库
  9. 将 Shiro 作为应用的权限基础 一:shiro的整体架构
  10. 云计算之路-阿里云上-容器难容:优化自建 docker swarm 集群的部署
  11. Fibonacci数列的解法
  12. 在 .NET Core 中结合 HttpClientFactory 使用 Polly(上篇)
  13. IDEA 对比eclipse环境调节
  14. jenkins持续集成:构建多个job同时执行
  15. Django之Models(二)
  16. css3 伸缩布局 display:flex等
  17. java4/9 异常处理
  18. 安装 scws出现 autoconf 需要先安装
  19. windows7 Cygwin 下安装 YouCompleteMe 插件
  20. jquery ajax contentType设置

热门文章

  1. Problem D: 零起点学算法40——多组测试数据(求和)IV
  2. 【R笔记】R语言中的字符串处理函数
  3. 搭建SSH框架–使用篇
  4. Delphi 自动检测U盘插入、拔出及获取U盘盘符!
  5. CentOS6 安装golang
  6. Android Studio使用过程中Java类突然报红,但项目可运行解决方案
  7. 安装notepad++ in ubuntu16.04
  8. 为甚么要将某个方法声明为final呢?
  9. Hadoop+Spark+Hbase部署整合篇
  10. 【菜鸟也疯狂UML系列】——概述