字符串矩陣轉換成長字符串
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
字符串矩陣轉換成長字符串