字符串矩阵转换成长字符串
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
字符串矩阵转换成长字符串