C 从阵列打印路径?

C 从阵列打印路径?,c,arrays,function,math,C,Arrays,Function,Math,我有一个2d数组a[3][3],程序读取数组上2个数字的2组ID。 我需要打印从一个号码到另一个号码的所有可能路径 到目前为止,我知道如何找到每次存在的路径数: scanf("%d %d",&ai,&aj); scanf("%d %d",&bi,&bj); distance_i = bi - ai; if(distance_i<0){distance_i=distance_i*-1;} distance_j = bj - aj; if(distance_j

我有一个2d数组
a[3][3]
,程序读取数组上2个数字的2组ID。 我需要打印从一个号码到另一个号码的所有可能路径

到目前为止,我知道如何找到每次存在的路径数:

scanf("%d %d",&ai,&aj);
scanf("%d %d",&bi,&bj);

distance_i = bi - ai;
if(distance_i<0){distance_i=distance_i*-1;}
distance_j = bj - aj;
if(distance_j<0){distance_j=ap_j*-1;}
path = 1+(distance_i*distance_j);

输出必须是:

there are 3 possible paths:
    a) 1,2,5,8
    b) 1,4,5,8
    c) 1,4,7,8
但是我找不到打印它们的方法。有什么想法吗?

您将使用回溯(深度优先搜索)查找所有可能的路线

请参见此处的程序测试

#定义有效的(x)((x)>=0&&(x)<3)
int arr[3][3];
//检测以前访问过的单元格并消除无限递归
短vis[3][3]={0};
int xtar,ytar;//目的细胞
int xsrc,ysrc;//源单元
//向以下方向移动:分别向下、向上、向右和向左
常量int dirx[]={0,0,1,-1};
常量int diry[]={1,-1,0,0};
//打印路径的临时缓冲区
//最大大小=arr的大小+零终止字符
字符tmp_路径[3*3+1];
void rec(int x,int y,int idx)//idx用于填充tmp_路径
{
int i;
tmp_路径[idx]=arr[y][x]+'0';
if(x==xtar&&y==ytar)//基本情况
{
tmp_路径[idx+1]=0;//放入零字符
printf(“%s\n”,tmp_路径);//打印路径
返回;
}
if(vis[y][x])return;//已访问
vis[y][x]=1;//否则,标记为已访问
对于(i=0;i<4;++i)//对于4个方向中的每个方向
if(有效(y+diry[i])&有效(x+dirx[i]))
rec(x+dirx[i],y+diry[i],idx+1);
vis[y][x]=0;//重置已访问,以便可以再次访问
}
main()
{
//输入xtar、ytar、xsrc、ysrc、arr
rec(xsrc,ysrc,0);
}

从位置[v1][h1]到位置[v2][h2]

移动类型有:
向下,向右

宽度为:
(v2-v1)*向下

高度为:
(h2-h2)*右侧

=>所有路径选择操作列表:
[宽度、高度]=[(v2-v1)*向下,(h2-h2)*向右]

示例:从位置[0][0]到位置[2][1]

动作列表<代码>=[下,下,右]

所有唯一路径选择为(它减去给定列表中的重复排列):

[下,下,右]

[下,右,下]


[右,下,下]

请解释“所有可能的路径”的含义。张贴你如何“发现每次存在多少条路线”。你可能会发现李的算法很有用,尽管与你的实际问题没有太大关系:如果你展示了你必须找到路线的代码,有人可能会告诉你如何打印它们。如果你能想出一个标题,描述你的技术问题多一点(而不是你生活中的问题)你也许可以自己找到解决这个问题的方法。似乎就是这样。但这比我们在学校里学到的东西要先进得多。复习答案中的链接。它打印出所有的路径。我不认为有更简单的方法,除非你说的“所有可能的路径”是另一回事这还不够清楚。然后你需要看看另一个答案。他只给你算法,你必须写它的C代码。
input_1: 0,0
input_2: 1,2
there are 3 possible paths:
    a) 1,2,5,8
    b) 1,4,5,8
    c) 1,4,7,8
#define VALID(x) ((x) >= 0 && (x) < 3)

int arr[3][3];

// to detect previous visited cells and eliminate infinite recursion
short vis[3][3] = { 0 }; 

int xtar, ytar; // destination cell
int xsrc, ysrc; // source cell

// to move in directions: down, up, right, and left, respectively
const int dirx[] = { 0, 0, 1, -1 };
const int diry[] = { 1, -1, 0, 0 };

// temp buffer to print paths
// max size = size of arr + zero termination char
char tmp_path[3 * 3 + 1];

void rec(int x, int y, int idx) // idx is used to fill tmp_path
{
   int i;
   tmp_path[idx] = arr[y][x] + '0';
   if (x == xtar && y == ytar) // basic case
   {
      tmp_path[idx + 1] = 0; // put zero char
      printf("%s\n", tmp_path); // print path
      return;
   }
   if (vis[y][x]) return; // already visited
   vis[y][x] = 1; // otherwise, mark as visited
   for (i = 0; i < 4; ++i) // for each of the 4 directions
      if (VALID(y + diry[i]) && VALID(x + dirx[i]))
         rec(x + dirx[i], y + diry[i], idx + 1);
   vis[y][x] = 0; // reset visited so that can be visited again
}

main()
{
   // input xtar, ytar, xsrc, ysrc, arr
   rec(xsrc, ysrc, 0);
}