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

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

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.mzph.cn/news/540532.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

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类用于…

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…

构建安全网络 比格云全系云产品30天内5折购

一年之计在于春&#xff0c;每年的三、四月&#xff0c;都是个人创业最佳的起步阶段&#xff0c;也是企业采购最火热的时期。为了降低用户的上云成本&#xff0c;让大家能无门槛享受到优质高性能的云服务&#xff0c;比格云从3月16日起&#xff0c;将上线“充值30天内&#xff…

数据结构 基础知识

一。逻辑结构: 是指数据对象中数据 素之间的相互关系。 其实这也是我 今后最需要关注的问题 逻辑结构分为以 四种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;但是对于国内无数的企业来说却可能是个会改变企业命运的日。驻云科技联合国内云服务提供商阿里云及国外…

浏览器端已支持 ES6 规范(包括 export import)

当然&#xff0c;是几个比较优秀的浏览器&#xff0c;既然是优秀的浏览器&#xff0c;大家肯定知道是那几款啦&#xff0c;我就不列举了&#xff0c;我用的是 chrome。 对 script 声明 type 为 module 后就可以享受 es6 规范所带来的模块快感了。 基础语法既然是全支持&#xf…

一文读懂深度学习框架下的目标检测(附数据集)

从简单的图像分类到3D位置估算&#xff0c;在机器视觉领域里从来都不乏有趣的问题。其中我们最感兴趣的问题之一就是目标检测。 如同其他的机器视觉问题一样&#xff0c;目标检测目前为止还没有公认最好的解决方法。在了解目标检测之前&#xff0c;让我们先快速地了解一下这个领…

设计一个应用程序,以在C#中的按钮单击事件上在MessageBox中显示TextBox中的文本...

Here, we took two controls on windows form that are TextBox and Button, named txtInput and btnShow respectively. We have to write C# code to display TextBox’s text in the MessageBox on Button Click. 在这里&#xff0c;我们在Windows窗体上使用了两个控件&…

Oracle优化器:星型转换(Star Query Transformation )

Oracle优化器&#xff1a;星型转换&#xff08;Star Query Transformation &#xff09;Star query是一个事实表&#xff08;fact table&#xff09;和一些维度表&#xff08;dimension&#xff09;的join。每个维度表都跟事实表通过主外键join&#xff0c;且每个维度表之间不j…

JavaScript | 声明数组并使用数组索引分配元素的代码

Declare an array, assign elements by indexes and print all elements in JavaScript. 声明一个数组&#xff0c;通过索引分配元素&#xff0c;并打印JavaScript中的所有元素。 Code: 码&#xff1a; <html><head><script>var fruits [];fruits[0]"…

Kubernetes基础组件概述

本文讲的是Kubernetes基础组件概述【编者的话】最近总有同学问Kubernetes中的各个组件的相关问题&#xff0c;其实这些概念内容在官方文档中都有&#xff0c;奈何我们有些同学可能英文不好&#xff0c;又或者懒得去看&#xff0c;又或者没有找到&#xff0c;今天有时间就专门写…

c语言将链表写入二进制文件_通过逐级遍历将二进制树转换为单链表的C程序

c语言将链表写入二进制文件Problem statement: Write a C program to convert a binary tree into a single linked list by traversing level-wise. 问题陈述&#xff1a;编写一个C程序&#xff0c;通过逐级遍历将二进制树转换为单个链表 。 Example: 例&#xff1a; The ab…

洛谷 P2689 东南西北【模拟/搜索】

题目描述 给出起点和终点的坐标及接下来T个时刻的风向(东南西北)&#xff0c;每次可以选择顺风偏移1个单位或者停在原地。求到达终点的最少时间。 如果无法偏移至终点&#xff0c;输出“-1”。 输入输出格式 输入格式&#xff1a; 第一行两个正整数x1,y1&#xff0c;表示小明所…

单链表遍历_单链表及其遍历实现的基本操作

单链表遍历单链表 (Single linked list) Single linked list contains a number of nodes where each node has a data field and a pointer to next node. The link of the last node is to NULL, indicates end of list. 单个链表包含许多节点&#xff0c;其中每个节点都有一…

8086简单的指令流水线_在8086微处理器中执行流水线的指令和概念的步骤

8086简单的指令流水线Any computer or machine works according to some instructions. These instructions are responsible for all the work that the machine does. But how does a machine work to understand and execute that instruction? 任何计算机或机器都按照某些…

node.js 爬虫入门总结

node.js爬虫 前端同学可能向来对爬虫不是很感冒&#xff0c;觉得爬虫需要用偏后端的语言&#xff0c;诸如 php &#xff0c; python 等。当然这是在 nodejs 前了&#xff0c;nodejs 的出现&#xff0c;使得 Javascript 也可以用来写爬虫了。由于 nodejs 强大的异步特性&#xf…

将八进制数制转换为二进制,十进制和十六进制数制

1)将八进制数制转换为二进制数制 (1) Conversion of Octal Number System to Binary Number System) To convert octal numbers into binary numbers, we can use the relationship between octal and binary numbers. 要将八进制数转换为二进制数&#xff0c;我们可以使用八进…

想提高用户访问的响应速度和成功率还不赶快学习CDN

2019独角兽企业重金招聘Python工程师标准>>> 课程介绍 CDN可以将源站内容分发至最接近用户的节点&#xff0c;使用户可就近取得所需内容&#xff0c;提高用户访问的响应速度和成功率。解决因分布、带宽、服务器性能带来的访问延迟问题&#xff0c;适用于站点加速、点…