字符串矩陣轉換成長字符串_字符串矩陣

字符串矩陣轉換成長字符串

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矩陣顯示輸入,以便更好地可視化。

string matrix (1)

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)開始

string matrix (2)

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

可以毫無跳躍地到達的下一塊巖石(0,1)

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

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

string matrix (3)

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)

string matrix (4)

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)

string matrix (5)

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)

string matrix (6)

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)

string matrix (7)

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).

現在,從這里無法到達下一塊巖石,因此我們需要回溯并找到其他選擇。 (紅色填充區域表示已經探索但失敗了)。

string matrix (8)

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)已經被探索并且失敗了。 尋找替代品,這里不再有巖石。 我們需要再次回溯。

string matrix (9)

Such backtrack will ultimately yield the following state.

這種回溯最終將產生以下狀態。

string matrix (10)

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)重新開始,然后再次開始相同的過程。 如果繼續這樣做,您會看到最終結果是:

string matrix (11)

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

字符串矩陣轉換成長字符串

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540532.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540532.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540532.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

pythonchallenge_level2

level2 地址&#xff1a;http://www.pythonchallenge.com/pc/def/ocr.html。 源碼&#xff1a;gitcode.aliyun.com:qianlizhixing12/PythonChallenge.git。 問題&#xff1a;找出頁面源碼一點提示注釋中的稀有字符。 #!/usr/bin/env python3 # -*- coding:UTF-8 -*-# Level 2im…

[轉載] python類運算符的重載

參考鏈接&#xff1a; Python中的運算符重載 alist input().split() blist input().split() n float(input()) class Vector: def __init__(self, x0, y0, z0): # 請在此編寫你的代碼(可刪除pass語句) self.X x self.Y y self.Z z # 代碼結束 def __add__(self, other):…

r語言 運算符_R語言運算符

r語言 運算符R語言中的運算符 (Operators in R Language) Generally speaking, an operator is a symbol that gives proper commands to the compiler regarding a specific action to be executed. The operators are used for carrying out the mathematical or logical cal…

[轉載] Python基礎之類型轉換與算術運算符

參考鏈接&#xff1a; Python中的運算符函數| 1 一、注釋 1.注釋&#xff1a;對程序進行標注和說明&#xff0c;增加程序的可讀性。程序運行的時候會自動忽略注釋。 2.單行注釋&#xff1a;使用#的形式。但是#的形式只能注釋一行&#xff0c;如果有多行&#xff0c;就不方便…

java awt 按鈕響應_Java AWT按鈕

java awt 按鈕響應The Button class is used to implement a GUI push button. It has a label and generates an event, whenever it is clicked. As mentioned in previous sections, it extends the Component class and implements the Accessible interface. Button類用于…

解決“由于應用程序的配置不正確,應用程序未能啟動,重新安裝應用程序可能會糾正這個問題”...

在VS2005下用C寫的程序&#xff0c;在一臺未安裝VS2005的系統上&#xff0c; 用命令行方式運行&#xff0c;提示&#xff1a; “系統無法執行指定的程序” 直接雙擊運行&#xff0c;提示&#xff1a; “由于應用程序的配置不正確&#xff0c;應用程序未能啟動&#xff0c;重新安…

qgis在地圖上畫導航線_在Laravel中的航線

qgis在地圖上畫導航線For further process we need to know something about it, 為了進一步處理&#xff0c;我們需要了解一些有關它的信息&#xff0c; The route is a core part in Laravel because it maps the controller for sending a request which is automatically …

Logistic回歸和SVM的異同

這個問題在最近面試的時候被問了幾次&#xff0c;讓談一下Logistic回歸&#xff08;以下簡稱LR&#xff09;和SVM的異同。由于之前沒有對比分析過&#xff0c;而且不知道從哪個角度去分析&#xff0c;一時語塞&#xff0c;只能不知為不知。 現在對這二者做一個對比分析&#xf…

[轉載] python學習筆記2--操作符,數據類型和內置功能

參考鏈接&#xff1a; Python中的Inplace運算符| 1(iadd()&#xff0c;isub()&#xff0c;iconcat()…) 什么是操作符&#xff1f; 簡單的回答可以使用表達式4 5等于9&#xff0c;在這里4和5被稱為操作數&#xff0c;被稱為操符。 Python語言支持操作者有以下幾種類型。 算…

scala bitset_Scala中的BitSet

scala bitsetScala BitSet (Scala BitSet) Set is a collection of unique elements. 集合是唯一元素的集合。 Bitset is a set of positive integers represented as a 64-bit word. 位集是一組表示為64位字的正整數。 Syntax: 句法&#xff1a; var bitset : Bitset Bits…

構建安全網絡 比格云全系云產品30天內5折購

一年之計在于春&#xff0c;每年的三、四月&#xff0c;都是個人創業最佳的起步階段&#xff0c;也是企業采購最火熱的時期。為了降低用戶的上云成本&#xff0c;讓大家能無門檻享受到優質高性能的云服務&#xff0c;比格云從3月16日起&#xff0c;將上線“充值30天內&#xff…

python中 numpy_Python中的Numpy

python中 numpyPython中的Numpy是什么&#xff1f; (What is Numpy in Python?) Numpy is an array processing package which provides high-performance multidimensional array object and utilities to work with arrays. It is a basic package for scientific computati…

[轉載] python之路《第二篇》Python基本數據類型

參考鏈接&#xff1a; Python中的Inplace運算符| 1(iadd()&#xff0c;isub()&#xff0c;iconcat()…) 運算符 1、算數運算&#xff1a; 2、比較運算&#xff1a; 3、賦值運算&#xff1a; 4、邏輯運算&#xff1a; 5、成員運算&#xff1a; 6、三元運算 三元運算&…

數據結構 基礎知識

一。邏輯結構: 是指數據對象中數據 素之間的相互關系。 其實這也是我 今后最需要關注的問題 邏輯結構分為以 四種1. 集合結構 2.線性結構 3.數形結構 4&#xff0c;圖形結構 二。物理結構&#xff1a; 1&#xff0c;順序存儲結,2 2. 鏈式存儲結構 一&#xff0c;時間復雜…

ruby 變量類中范圍_Ruby中的類

ruby 變量類中范圍Ruby類 (Ruby Classes) In the actual world, we have many objects which belong to the same category. For instance, I am working on my laptop and this laptop is one of those laptops which exist around the globe. So, this laptop is an object o…

以云計算的名義 駐云科技牽手阿里云

本文講的是以云計算的名義 駐云科技牽手阿里云一次三個公司的牽手 可能會改變無數企業的命運 2017年4月17日&#xff0c;對于很多人來說可能只是個平常的工作日&#xff0c;但是對于國內無數的企業來說卻可能是個會改變企業命運的日。駐云科技聯合國內云服務提供商阿里云及國外…

[轉載] python學習筆記

參考鏈接&#xff1a; Python | a b并不總是a a b 官網http://www.python.org/ 官網library http://docs.python.org/library/ PyPI https://pypi.python.org/pypi 中文手冊&#xff0c;適合快速入門 http://download.csdn.net/detail/xiarendeniao/4236870 py…

標志寄存器_訪問標志寄存器,并與寄存器B |交換標志寄存器F的內容 8085微處理器...

標志寄存器Problem statement: 問題陳述&#xff1a; Write an assembly language program in 8085 microprocessor to access Flag register and exchange the content of flag register F with register B. 在8085微處理器中編寫匯編語言程序以訪問標志寄存器&#xff0c;并…

瀏覽器端已支持 ES6 規范(包括 export import)

當然&#xff0c;是幾個比較優秀的瀏覽器&#xff0c;既然是優秀的瀏覽器&#xff0c;大家肯定知道是那幾款啦&#xff0c;我就不列舉了&#xff0c;我用的是 chrome。 對 script 聲明 type 為 module 后就可以享受 es6 規范所帶來的模塊快感了。 基礎語法既然是全支持&#xf…

[轉載] Python學習:Python成員運算符和身份運算符

參考鏈接&#xff1a; Python中和is運算符之間的區別 Python成員運算符 除了以上的一些運算符之外&#xff0c;Python還支持成員運算符&#xff0c;測試實例中包含了一系列的成員&#xff0c;包括字符串&#xff0c;列表或元組。 運算符 描述 實例 in 如果在指定的序列中找…