C 如何连接拼图,使右边缘和另一个拼图的左边缘具有相同的长度?

C 如何连接拼图,使右边缘和另一个拼图的左边缘具有相同的长度?,c,arrays,puzzle,C,Arrays,Puzzle,我有这样的谜题: === ==== === === === ==== 6 1 9 2 0 3 1 0 4 1 8 9 0 2 9 0 1 5 0 左边缘的长度为0到10000,右边缘的长度为1到10000。 所以问题是我是否可以构建一个矩形?像第一个谜题一样,左边缘的长度等于0,最后一个谜题有长度0的右边缘,在中间它们完全匹配? 我得到的谜题数量和它们的参数如下: === ==== === === === ==== 6 1 9 2 0 3 1 0 4 1

我有这样的谜题:

    === ==== 
=== ===
    === ====
6
1 9 2
0 3 1
0 4 1
8 9 0
2 9 0
1 5 0
左边缘的长度为0到10000,右边缘的长度为1到10000。 所以问题是我是否可以构建一个矩形?像第一个谜题一样,左边缘的长度等于0,最后一个谜题有长度0的右边缘,在中间它们完全匹配?

我得到的谜题数量和它们的参数如下:

    === ==== 
=== ===
    === ====
6
1 9 2
0 3 1
0 4 1
8 9 0
2 9 0
1 5 0
其结果可以是:

2
0 3 1
1 5 0

但是如果没有结果,我必须
printf(“没有结果”)

我必须在C中这样做,我想做一些树并用BFS搜索它,其中顶点有边长度,边有中间长度,当达到0时,我会一直向上收集数字,但很难编码。所以我决定做递归,但我也被卡住了:

#包括
int main(){
INTA;
scanf(“%d”,&a);//这里我得到了我有多少个拼图
int tab[a][3];//拼图数组
int result[][3];//结果数组
int k=0;//这将帮助我跟踪结果数组中有多少个谜题
对于(inti=0;i

我有一个错误,结果数组不能像这个
int result[][3]
,因为
[]
,但我不知道我要用多少谜题,所以?。。。这两个函数都有隐式声明。伙计们,请帮忙,我对C不太了解,解决这个问题非常困难。

我不确定我是否理解这个问题的整体逻辑,但您肯定需要一些大小可变的容器来处理结果和选项卡。数组的大小是固定的,必须在编译时定义。至少应编译以下内容而不发出警告:

#include<stdio.h>
#include<stdlib.h>

void print_result(int (*result)[3], int k){
    printf("%d", k);//how many puzzles i have
    printf("\n");
    for(int i = 0; i <= k; i++){//printing puzzles...
        for(int j = 0; j < 3; j++){
            printf("%d ", result[i][j]);
        }
        printf("\n");//...in separate lines
    }
}

void findx(int x, int a, int (*tab)[3], int (*result)[3], int k){//i am looking for puzzle with length x on start
    for(int i = 0; i < a; i++){
        if(tab[i][0] == x){//there i look for puzzles with x length at start
            if(tab[i][2] == 0){//if i find such puzzle i check if this is puzzle with edge length zero at the end
                for(int m = 0; m < 3; m++){//this for loop add to my result array last puzzle
                    result[k][m] = tab[i][m];
                }
                print_result(result, k);//we will return result go to print_result function
                return;
            }
            else{//if i have puzzle with x length on the left and this is not puzzle which ends rectangle i add this puzzle
                    //to my result array and again look for puzzle with x equal to end length of puzzle i found there
                for(int m = 0; m < 3; m++){
                    result[k][m] = tab[i][m];
                    k += 1;
                    ///** Increase size of result **/
                    //int (*newptr)[3] = realloc(result, (k+1) * sizeof(int[3]));
                    //if (newptr)
                    //    result = newptr;
                }
                findx(tab[i][2], a, tab, result, k);
            }
        }
    }
    printf("no result\n");
}

int main(){

    int a;
    scanf("%d", &a);//here i get how many puzzles i have
    int (*tab)[3] = malloc(a * sizeof(int[3]));//array for puzzles
    int (*result)[3] = malloc(a * sizeof(int[3]));//array for puzzles

    int k = 0;//this will help me track how many puzzles has my result array

    for(int i = 0; i < a; i++){//upload puzzles to array
        for(int j = 0; j < 3; j++){
            scanf("%d", &tab[i][j]);
        }
    }

    findx(0, a, tab, result, k);//start of recursion, because start has to be length 0

}

用于(int i=0;i
#包括
void findx(int x,int a,int tab[a][3],int result[200000][3],int puzzsinresult){//我正在寻找开始时长度为x的拼图
for(int i=0;i0){
结果-=1;
}
int isusedpuzzle=0;
for(int j=0;jfor(int i = 0; i < k; i++){//printing puzzles...
for(int i = 0; i <= k; i++){//printing puzzles...
#include<stdio.h>

void findx(int x, int a, int tab[a][3], int result[200000][3], int puzzlesinresult) { //i am looking for puzzle with length x on start
  for (int i = 0; i < a; i++) {
    if (tab[i][0] == x) { //there i look for puzzles with x length at start
      if (tab[i][2] == 0) { //if i find such puzzle i check if this is puzzle with edge length zero at the end
        for (int m = 0; m < 3; m++) { //this for loop add to my result array last puzzle
          result[puzzlesinresult][m] = tab[i][m];
        }
        return print_result(result, puzzlesinresult); //we will return result go to print_result function
      } else { //if i have puzzle with x length on the left and this is not puzzle which ends rectangle i add this puzzle
        //to my result array and again look for puzzle with x equal to end length of puzzle i found there
        while (result[puzzlesinresult - 1][2] != tab[i][0] && puzzlesinresult > 0) {
          puzzlesinresult -= 1;
        }
        int isusedpuzzle = 0;
        for (int j = 0; j < puzzlesinresult; j++) {
          if (result[j][0] == tab[i][0] && result[j][1] == tab[i][1] && result[j][2] == tab[i][2]) {
            isusedpuzzle = 1;
          } else {
            //pass
          }
        }
        if (isusedpuzzle == 0) {
          for (int m = 0; m < 3; m++) {
            result[puzzlesinresult][m] = tab[i][m];
          }
          puzzlesinresult += 1;
          findx(tab[i][2], a, tab, result, puzzlesinresult);
        }
      }
    }
  }
}

void print_result(int result[200000][3], int puzzlesinresult) {
  printf("%d\n", puzzlesinresult + 1); //how many puzzles i have
  for (int i = 0; i < puzzlesinresult + 1; i++) { //printing puzzles...
    for (int j = 0; j < 3; j++) {
      printf("%d ", result[i][j]);
    }
    printf("\n"); //...in separate lines
  }
  exit(0);
}

int main() {

  int a;
  scanf("%d", & a); //here i get how many puzzles i have
  int tab[a][3]; //array for puzzles
  int result[100][3]; //result array
  int puzzlesinresult = 0; //this will help me track how many puzzles has my result array

  for (int i = 0; i < a; i++) { //upload puzzles to array
    for (int j = 0; j < 3; j++) {
      scanf("%d", & tab[i][j]);
    }
  }
  for (int i = 0; i < a; i++) { //here i delete puzzles that doesnt contribute anything like 1 x 1,2 x 2,..
    if (tab[i][0] == tab[i][2] && tab[i][0] != 0) {
      for (int p = i; p < a; p++) {
        for (int j = 0; j < 3; j++) {
          tab[p][j] = tab[p + 1][j];
        }
      }
    }
  }

  findx(0, a, tab, result, puzzlesinresult); //start of recursion, because start has to be length 0
  printf("NONE");
}