C ++中的矩阵相邻元素组合 [英] Matrix adjacent elements combinations in C++

查看:75
本文介绍了C ++中的矩阵相邻元素组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个矩阵:

A B C

D E F

G H I

我想获取长度为 3 相邻单元格和对角线单元格的所有可能组合,例如:

I want to get all possible combinations of adjacent cells and diagonal cells with a lenght of 3, for example:

从A开始:

 - *ABC* right right
 - *ABE* right down
 - *ABF* right diagonal-right
 - *ABD* right diagonal-left
 - ecc ecc

我试图创建一个名为"lettera"的新类,以字母为键,并带有一个指示向右,向左,向下,向上ecc的指针的成员.并且还有一个名为"sequenza"的成员,该字符串将其接触的每个字母连接起来.

I tried to create a new class called "lettera", with as key the letter, and with a member that indicates the pointer to right, left, down, up ecc. And also with a member called "sequenza", a string that concatenate every letter it touch.

例如,如果a作为键"B",则我有B->下== * E,B->左== * A,B->右== * C,依此类推... 而且有效.然后我为每个字母放一个计数器:当它到达3时,它应该停止确定组合.

For example if a have as key, "B", i have B->down == *E, B->left == *A, B->right == *C and so on... And it works. Then I put a counter for each letter: when it arrives at 3 it should stop determinating combinations.

然后是问题的核心:每个字母的路径...我尝试创建一个递归函数,但这确实起作用.

Then the core of the problem: the path for each letter to follow... I tried do create a recursive function, but it doenst work.

您能通过观看此内容或通过其他方式向我提出建议来帮助我吗?

Can you please help me by watching this or by suggesting me another way?

非常感谢.

代码:

    void decisione(lettera *c) {

            if (c == nullptr) return ;

            c->count++;
            c->sequenza = c->sequenza + c->key;

            if (c->count == 2) 
                    cout << "\n" << c->sequenza; 
                 //the sequence of letters accumulated in every call
            decisione(c->Up);
            decisione(c->Down);

        }

例如,它给我AAA和BBB,然后它崩溃=(

It gives me for example AAA and BBB and then it crashes =(

推荐答案

从A开始,您可以去哪里? B和D.假设您现在去B,您可以去哪里? A,C和E.您已经去过A,并且不想返回,所以只有C和E.假设您选择了C,因为您已经选择了三个字母,那么函数将停止,然后选择E依此类推(我不是在选择对角线邻居),这是程序:

Start at A, where can you go? B and D. Suppose you go to B, now, where can you go? A, C and E. You've already been in A and you don't want to return, so you only have C and E. Suppose you pick C, as you have already picked three letters the function stops and then you pick E and so on (I'm not choosing diagonal neighbors), here is the program:

#include <cstdio>
#include <cstdlib>

int a[] = {-1,-1,-1,0,0,1,1,1}, b[] = {-1,0,1,-1,1,-1,0,1},cont;
char s[3],mat[3][3];
bool flag[9];

void display() {
  for(int i = 0; i < 3; ++i) printf("%c",s[i]);
  puts("");
}

void show(int x,int y) {//You are in mat[x][y]
  s[cont] = mat[x][y];
  if(cont == 2) {
    display();
    return;
  }
  flag[mat[x][y] - 'A'] = true;
  int xx,yy;
  for(int i = 0; i < 8; ++i) {
    xx = x + a[i], yy = y + b[i];
    if(0 <= xx and xx < 3 and 0 <= yy and yy < 3 and !flag[mat[xx][yy] - 'A']) {
      ++cont;
      show(xx,yy);
      --cont;
    }
  }
  flag[mat[x][y] - 'A'] = false;

}

int main() {
  cont = 0;
  for(int i = 0; i < 3; ++i) {
    for(int j = 0; j < 3; ++j) {
      mat[i][j] = ('A' + 3*i + j);
    }
  }
  for(int i = 0; i < 3; ++i) {
    for(int j = 0; j < 3; ++j) {
      show(i,j); //You start from mat[i][j]: {'A','B','C',...,'I'}
    }
  }
  return 0;
}

这篇关于C ++中的矩阵相邻元素组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆