【发布时间】:2015-02-21 10:08:45
【问题描述】:
我面临一个很长一段时间以来一直无法解决的问题。 我得到了一个如下图,在一个 M x N 矩阵中:
2 2
a b
a c
注意 我将上面的图解释为一个矩阵,只包含非对角边。
这里第一行分别代表M和N的值。 该图仅沿垂直或相邻方向连接,即上、下、左和右。对角线边缘不存在。
为了找到图的邻接表(这里需要输出):
a-b-c
b-a-c
c-a-b
我在代码中遵循的步骤:
1.将M x N矩阵读入二维数组。
2.创建图的唯一顶点列表作为Unode[arrmax]。
3.对于矩阵的每个元素,如果字符与唯一顶点列表的元素匹配,我调用了修改邻接列表过程,该过程搜索相关矩阵顶点的邻居并填充/附加到找到不同节点时的邻接列表。
- 它将 i、j、M、N、AdjList、列表中元素的数量作为参数并进行更改。
5.我将节点列表保持为全局以便于修改。
6.接下来我打算使用生成的邻接表在DFS程序中使用并找到DFS林。
问题陈述:
输入由大小为 M X N 的网格组成。网格中的每个单元格 包含英文字母的小写字母。以自然的方式, 细胞有两种类型:边界细胞和内部细胞。每个 网格中的内部单元格在左侧之一中有四个邻居, 右、上、下方向。一串字符可以由 从任何单元格开始并通过邻居遍历网格。 您必须根据以下内容打印所有可能的字符串 约束:
**No two characters in a string can be same **No two strings can be same in the final output **The strings should be printed in alphabetically sorted order.
INPUT:
First line contains two integers M and N
Next M lines contains N space separated characters each
OUTPUT:
Print all possible strings in sorted order and obeying the above constraints.
INPUT SIZE:
1 <= M, N <= 20
SAMPLE INPUT:
2 2
a b
a c
SAMPLE OUTPUT:
a ab abc ac acb b ba bc bca c ca cb cba
[更新]: 完全重新设计了代码,使用了图形节点的结构,以及一个用于处理索引的结构。 然而我得到的结果:
a--b-a
b--a
a
c--a
我的代码[相关部分]:
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define ADJMAX 20
#define arrmax 400
typedef struct uniq_node{
char ch;
char AdjList[ADJMAX];
int numofelem;
int visited;
}unode;
unode Ulist[arrmax];
int uniq_tot=0;
typedef struct index
{
int i,j;
}Ind;
Ind indx;
int charcomp(char sch,char arr[],int arrlim);
void adjModify(unode*,char*,int,int,Ind);
int chIndex(int,int,int,int);
int main(void) {
int mvar,nvar;
char str[15],*token;
long integer;
/*To scan the values of M & N*/
scanf("%d %d\n",&mvar,&nvar);
int iter,iterv,jterv;
/*To create the character matrix of M x N*/
char cmat[mvar][nvar];
/*Initializing the unique nodes list*/
/*To read-in the matrix from the stdin:-A LOT OF HARD WORK*/
for(iterv=0;iterv<mvar;iterv++)
{
fgets(str,50,stdin);
jterv=0;
token=strtok(str," ");
while(token)
{
/*Assigning value to the character matrix*/
cmat[iterv][jterv]=*token;
/*Code to populate the list of unique elements*/
if(charcomp(*token,Ulist[uniq_tot].AdjList,uniq_tot)==3)
{
Ulist[uniq_tot].ch=*token;
uniq_tot++;
Ulist[uniq_tot].numofelem=1;
Ulist[uniq_tot].AdjList[0]=*token;
//Ulist[uniq_tot].visited=0;
}
jterv++;
token = strtok(NULL, " ");
}
}
/*To populate the adjacency lists */
char ch;
for(iterv=0;iterv<mvar;iterv++)
{
for(jterv=0;jterv<nvar;jterv++)
{
ch=cmat[iterv][jterv];
indx.i=iterv;
indx.j=jterv;
for(iter=0;iter<uniq_tot;iter++)
{
if(ch==Ulist[iter].ch)
break;
}
adjModify(&Ulist[iter],(char*)cmat,mvar,nvar,indx);
}
}
/*for(iter=0;iter<uniq_tot;iter++)
{
printf("%c",Ulist[iter].ch);
printf("\n%s\n",Ulist[iter].AdjList);
for(iterv=0;iterv<Ulist[iter].numofelem;iterv++)
{
printf("-%c",Ulist[iter].AdjList[iterv]);
}
printf("\n");
}*/
return 0;
}
int chIndex(int i,int j,int mvar,int nvar)
{
return (i>=0 && i<mvar && j>=0 && j<nvar);
}
void adjModify(unode* Unode,char* mat,int mvar,int nvar,Ind mind)
{
int idum,jdum;
if(chIndex(mind.i,mind.j-1,mvar,nvar))
{
idum=mind.i;
jdum=mind.j-1;
if(charcomp(*(mat+idum*nvar+jdum),Unode->AdjList,Unode->numofelem)==3)
{
++Unode->numofelem;
Unode->AdjList[Unode->numofelem]=*(mat+idum*nvar+jdum);
printf("\nI'm here in coord:(%d,%d), with element: %c, and AdjList: %s for character: %c",idum,jdum,*(mat+idum*nvar+jdum),Unode->AdjList,Unode->ch);
}
}
if(chIndex(mind.i,mind.j+1,mvar,nvar))
{
idum=mind.i;
jdum=mind.j+1;
if(charcomp(*(mat+idum*nvar+jdum),Unode->AdjList,Unode->numofelem)==3)
{
++Unode->numofelem;
Unode->AdjList[Unode->numofelem]=*(mat+idum*nvar+jdum);
printf("\nI'm here in coord:(%d,%d), with element: %c, and AdjList: %s for character: %c",idum,jdum,*(mat+idum*nvar+jdum),Unode->AdjList,Unode->ch);
}
}
if(chIndex(mind.i-1,mind.j,mvar,nvar))
{
idum=mind.i-1;
jdum=mind.j;
if(charcomp(*(mat+idum*nvar+jdum),Unode->AdjList,Unode->numofelem)==3)
{
++Unode->numofelem;
Unode->AdjList[Unode->numofelem]=*(mat+idum*nvar+jdum);
printf("\nI'm here in coord:(%d,%d), with element: %c, and AdjList: %s for character: %c",idum,jdum,*(mat+idum*nvar+jdum),Unode->AdjList,Unode->ch);
}
}
if(chIndex(mind.i+1,mind.j,mvar,nvar))
{
idum=mind.i+1;
jdum=mind.j;
if(charcomp(*(mat+idum*nvar+jdum),Unode->AdjList,Unode->numofelem)==3)
{
++Unode->numofelem;
Unode->AdjList[Unode->numofelem]=*(mat+idum*nvar+jdum);
printf("\nI'm here in coord:(%d,%d), with element: %c, and AdjList: %s for character: %c",idum,jdum,*(mat+idum*nvar+jdum),Unode->AdjList,Unode->ch);
}
}
}
/*Comparison routine*/
int charcomp(char fchar,char arr[],int ucindex)
{
int ivar;
for(ivar=0;ivar<ucindex;ivar++)
{
if(arr[ivar]==fchar)
return;
}
return 3;
}
【问题讨论】:
-
对不起,矩阵是什么? 2 2 a b a c 在我看来不像矩阵。这代表什么?这看起来像是创建一个简单图形的大量代码。您还说它是单向的,那为什么您的链接看起来像是定向的?
-
2 2 分别代表M和N。由于 No._of_edges/2=no。独特的顶点,因此我将其视为无向的。此外,对于重复节点,我使用了相同的邻接列表。
-
当你说邻接矩阵时,我期望像这样en.wikipedia.org/wiki/Adjacency_matrix我不知道如何解释你的。
-
对不起,如果我不明确,但我打算创建一个邻接列表。这是一个邻接矩阵表示,我没有使用它,因为我发现 DFS 的算法更方便为 Adj List 编写代码。另外,我认为我的比较例程有缺陷,和/或某个地方的空白正在蔓延到 Adj清单,我相信一双新鲜的眼睛会成功:-)
-
没问题,还有一件事,就是这个“a->b->c b->a->c c->a->b”的输入,而你正在尝试解析那个确切的文字?箭头是否暗示方向性?