0

私はこのマトリックスを持っています:

A B C

D E F

G H I

たとえば、長さが3の隣接セルと対角セルのすべての可能な組み合わせを取得したい:

A から:

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

文字をキーとして、右、左、下、上へのポインターを示すメンバーを使用して、「lettera」という新しいクラスを作成しようとしました。また、「sequenza」と呼ばれるメンバーと、接触するすべての文字を連結する文字列があります。

たとえば、キーとして「B」がある場合、B->down == *E、B->left == *A、B->right == *C など...そして機能します。次に、各文字にカウンターを置きます。3 に到達すると、組み合わせの決定を停止する必要があります。

次に、問題の核心:各文字がたどるパス...再帰関数を作成しようとしましたが、機能しません。

これを見るか、別の方法を提案してください。

どうもありがとう。

コード:

    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が表示され、クラッシュします=(

4

1 に答える 1

1

Aから始めて、どこに行けますか?BとD。Bに行ったとしましょう。今、どこに行けますか?A、C、E。あなたはすでにAにいて、戻りたくないので、CとEしかありません。Cを選択したとします。すでに3文字を選択しているので、関数は停止し、次にEを選択します。など(私は対角の隣人を選択していません)、ここにプログラムがあります:

#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;
}
于 2012-11-25T15:52:11.053 に答える