字符串矩阵转换成长字符串

Description:

描述:

In this article, we are going to see how backtracking can be used to solve following problems?

在本文中,我们将看到如何使用回溯来解决以下问题?

Problem statement:

问题陈述:

A matrix of characters schematically represents a swamp. The swamp is composed of muddy areas, represented with the '.' character, and rocky areas, represented with the '*' character.

字符矩阵示意性地表示沼泽。 沼泽由泥泞的地区组成,以“。”表示 字符和岩石区域,以“ *”字符表示。

Example of swamp:

沼泽的例子:

    **.......
.......**.
...........
......**..
..........

Write a C program that searches a path in the swamp, from the left to the right, without jumps, only including consecutive rocky areas. Suppose that each rocky area can have at most one other rocky area on its right (there are no branches), i.e., either on the same row, or in the previous row, or the following one. The program shall print the row sequence of the path (the columns are implicit – there shall be a rocky area for each column), or report that no path exists.

编写一个C程序,该程序从左到右搜索沼泽中的一条路径,没有跳跃,仅包括连续的岩石区域。 假设每个岩石区域在其右侧最多可以有一个其他岩石区域(没有分支),即位于同一行,上一行或下一行。 程序应打印路径的行顺序(列是隐式的–每列应有一块岩石区域),或报告不存在路径。

Explanation with example:

举例说明:

Let's discuss the following input:

让我们讨论以下输入:

    **.*.*....*
..*.*...**
*...*.*....
.*.*.*.*.*.
..*.*...*.*

Let's display the input in a 2D matrix for better visualization.

让我们以2D矩阵显示输入,以便更好地可视化。

Let's start from the 0th column (0 indexing),

让我们从第0列(索引为0)开始,

There is a rock in the row 0

第0行有一块岩石

Start from (0, 0)

从(0,0)开始

Next rock that can be reached without any jump (0, 1)

可以毫无跳跃地到达的下一块岩石(0,1)

Path: (0, 0) -> (0, 1)

路径:(0,0)->(0,1)

Next rock that can be reached without any jump (1, 2)

可以毫无跳跃地到达的下一块岩石(1、2)

Path: (0, 0) -> (0, 1) -> (1, 2)

路径:(0,0)->(0,1)->(1,2)

Next rock that can be reached without any jump (0, 3)

可以毫无跳跃地到达的下一块岩石(0,3)

Path: (0, 0) -> (0, 1) -> (1, 2)-> (0, 3)

路径:(0,0)->(0,1)->(1,2)->(0,3)

Next rock that can be reached without any jump (1, 4)

可以毫无跳跃地到达的下一块岩石(1、4)

Path: (0, 0) -> (0, 1) -> (1, 2)-> (0, 3) -> (1, 4)

路径:(0,0)->(0,1)->(1,2)->(0,3)->(1,4)

Next rock that can be reached without any jump (0, 5)

可以毫无跳跃地到达的下一块岩石(0,5)

Path: (0, 0) -> (0, 1) -> (1, 2)-> (0, 3) -> (1, 4) -> (0, 5)

路径:(0,0)->(0,1)->(1,2)->(0,3)->(1,4)->(0,5)

Now, there is no next rock that can be reached from here, so we need to backtrack and find other alternatives. (red filled area refers that already explored but failed).

现在,从这里无法到达下一块岩石,因此我们需要回溯并找到其他选择。 (红色填充区域表示已经探索但失败了)。

So, we backtrack to previous state and the last point on our path is (1, 4). (0, 5) is already explored and failed option. Looking for alternative there is no more rock that can be reached from here. We need to backtrack again.

因此,我们回溯到先前的状态,并且路径上的最后一点是(1,4)。 (0,5)已经被探索并且失败了。 寻找替代品,这里不再有岩石。 我们需要再次回溯。

Such backtrack will ultimately yield the following state.

这种回溯最终将产生以下状态。

So basically, all the path we had explored is failed.

因此,基本上,我们探索的所有路径都是失败的。

We will start fresh from (2, 0) and start the same procedure again. If you keep doing you can see that the ultimate result is:

我们将从(2,0)重新开始,然后再次开始相同的过程。 如果继续这样做,您会看到最终结果是:

This is what backtrack is, explore through all the choices possible, backtrack if there is no next move. Of course, this kind of search technique is greedy, but it helps sometimes when you have no choices.

这就是回溯,探索所有可能的选择,如果没有下一步行动,则回溯。 当然,这种搜索技术是贪婪的,但有时在您别无选择时会有所帮助。

N.B: there can be multiple paths possible, depends on your implementation. If you terminate the program while the goal is reached it will return one path only. But if you keep exploring other possibilities as well, you can find other possible paths.

注意:可能有多种路径,具体取决于您的实现。 如果在达到目标时终止程序,它将仅返回一个路径。 但是,如果您也继续探索其他可能性,则可以找到其他可能的途径。

Algorithm:

算法:

    1.  Start: start from initial point
2.  Explore one from the possible next moves
3.  If no more moves possible & goal is not reached
backtrack and choose one from other alternatives.
4.  If goal is reached, success
5.  Else failure.

C Implementation:

C实现:

#include <stdio.h>
#define ROW 25
#define COL 80
char arr[ROW][COL];
int vis[COL],store[COL];
int issafe(int vis[],int curr,int curc,int r,int c){//printf("%c\n",arr[curr][curc]);
if(curr<0 || curr>=r || curc<0 || curc>=c || arr[curr][curc]=='.')
return 0;
return 1;
}
int findpath(int vis[],int store[],int curr,int curc,int r,int c){//base case
if(curc==c){//store[curc]=curr;
printf("The path can be: ");
for(int i=0;i<c;i++){printf("%d ",store[i]);
}
printf("\n");
return 1;
}
if(issafe(vis,curr,curc,r,c)){vis[curc]=1;
store[curc]=curr;
//printf("%d\n",store[curc]);
if(findpath(vis,store,curr,curc+1,r,c))
return 1;
if(findpath(vis,store,curr+1,curc+1,r,c))
return 1;
if(findpath(vis,store,curr-1,curc+1,r,c))
return 1;
vis[curc]=0;
store[curc]=0;
return 0;
}
else{return 0;
}
}
int main()
{// FILE *fptr;
// fptr = fopen("input.txt", "r");
// if (fptr == NULL)
// {
//     printf("Cannot open file \n");
//     exit(0);
// }
int r,c;
printf("Enter number of rows and column\n");
scanf("%d %d",&r,&c);
printf("Enter the string matrix\n");
for(int i=0;i<r;i++){scanf(" %[^\n]",arr[i]);
}
// for(int i=0;i<r;i++){//     for(int j=0;j<c;j++){//         printf("%c ",arr[i][j]);
//     }
//     printf("\n");
// }
int flag=0;
for(int i=0;i<r;i++){for(int j=0;j<c;j++)
vis[j]=0;
for(int j=0;j<c;j++)
store[j]=0;
if(findpath(vis,store,i,0,r,c)){flag=1;
//don't break here, if you need all possible paths
break;
}
}
if(flag==0)
printf("No path there\n");
return 0;
}

Output

输出量

Enter number of rows and column
5 11
Enter the string matrix
**.*.*....*
..*.*...**.
*...*.*....
.*.*.*.*.*.
..*.*...*.*
The path can be: 2 3 4 3 4 3 2 3 4 3 4

翻译自: https://www.includehelp.com/icp/string-matrix.aspx

字符串矩阵转换成长字符串

字符串矩阵转换成长字符串_字符串矩阵相关推荐

  1. python对输入的字符串进行解析_python数据类型_字符串常用操作(详解)

    这次主要介绍字符串常用操作方法及例子 1.python字符串 在python中声明一个字符串,通常有三种方法:在它的两边加上单引号.双引号或者三引号,如下: name = 'hello' name1 ...

  2. python 字符串大小写转换 其它不变_python字符串大小写如何转换

    平常开发过程中对字符串的一些操作:#字母大小写转换 #首字母转大写 #去除字符串中特殊字符(如:'_','.',',',';'),然后再把去除后的字符串连接起来 #去除'hello_for_our_w ...

  3. ker矩阵是什么意思_理解矩阵(二)

    作者:myan 来源:CSDN 原文:https://blog.csdn.net/myan/article/details/649018 接着理解矩阵. 上一篇里说"矩阵是运动的描述&quo ...

  4. java英文字符串大小写转换 必须使用_【Java基础】之字符串大小写转换不利用API....

    public class UpStr { static String str = "AbcDeFdDSfgdsadeADFSAFCfdsa"; public String tran ...

  5. php 数组到字符串的转换,php – 数组到字符串到数组的转换

    我有一个数组,我将其作为字符串存储在数据库中,以便更容易检索(通过cron每15-30分钟刷新一次新数据). 'player_list' -> 'Bob,Dave,Jane,Gordy' 'pl ...

  6. java字符串日期转换成数字,Java字符串到日期的转换

    用Java将" 2010年1月2日"格式的String转换为Date的最佳方法是什么? 最终,我想将月份,日期和年份分解为整数,以便可以使用 Date date = new Dat ...

  7. 字符串大小写转换html,javascript将字符串字母转换为大小写字母

    你知道,javascript当中把字符串当中的字母全部的转换成大写字母或者是小写字母要怎样才能实现吗?下面要给大家讲到的就是这个方面的内容. 闲话就不多说了,直接看代码吧! let str = &qu ...

  8. linux字符串编码转换函数,Linux C++ 字符串 编码识别、编码转换

    最近在做一个类似垂直下载的爬虫系统.下载之后有个解析模块,解析之后要求编码一致的向后传入索引,便遇到了编码转换问题. 1. 编码的识别 推荐使用 libchardet, 可以在这个页面下载,使用说明就 ...

  9. python 将三维数据转为二维_将三维矩阵转换/重塑为二维矩阵

    使用^{},然后使用^{},如下-X.transpose(1,2,0).reshape(-1,X.shape[0]) 解释- 1)您想要得到由X[:, 0, 0].X[:, 0, 1]等组成的行,也就 ...

最新文章

  1. Cocos Creator 的 动作(Action)系统:moveBy的使用
  2. seo说_百度指数看世间沉浮_如何快速排名-互点快速排名_网站关键词排名常见问题 - 搜狗快速排名...
  3. java底层编程_万字长文!从底层开始带你了解并发编程,彻底帮你搞懂Java锁!
  4. 操作系统03进程管理Process_Scheduling
  5. vue-router配置介绍和使用方法(三)
  6. 【Kafka】如何判断一个kafka集群是否稳定
  7. Q103:磨边的物体(Beveled Objects)
  8. Android Studio的单元测试
  9. delphi(注入)附部分源代码
  10. JS_js和jq获取屏幕高度、宽度的方法
  11. jmeter使用详解
  12. 手机怎样和宽带连接无线路由器设置路由器连接服务器,手机设置路由器步骤_用手机怎么设置路由器?-192路由网...
  13. 关于IE浏览器的一些思路
  14. Proof_Of_Work机制
  15. HDU 6045 Is Derek lying?
  16. Python中第三方库-Faker应用
  17. 比如说,你下午四点钟来。那么从三点钟起,我就开始感到幸福。时间越临近,我就越感到幸福。到了四点钟的时候,我就会坐立不安:我就会发现幸福的代价。...
  18. java中除法和取余的若干注意
  19. JAVA 完整实现滑块拼图验证码
  20. 现在面试都不问八股文了吗

热门文章

  1. java注意的一些细节问题
  2. javascript 本地对象和内置对象_详解 JavaScript 面向对象
  3. php3.2.3 升级,thinkphp3.2.3 升级到3.2.4时出错问题
  4. python无人机路径规划算法_RRT算法在Python中的实现,快速,拓展,随机,树
  5. mysql 分组查询原理,MySQL分組查詢Group By實現原理詳解
  6. windows设置mysql使用率_Windows下配置Mysql
  7. set和map去重调用什么方法_【ES6】Set、Map
  8. 热门搜索怎么实现_三个步骤教你学会,搜索引擎霸屏技术!
  9. datatype未定义是什么意思_TypeError:无法读取未定义的属性'then'
  10. 八个角最多可以把平面分成多少部分?_一个空间最多能被分成几块?