简体   繁体   English

使用一维数组打印二维数组

[英]Printing a 2d array using a 1d array

I am working on an eight queens/ chess board problem in my class. 我正在上课时研究八个皇后/棋盘问题。 For my code, i am using a one dimensional array ie q[c](c is the column) to store the value of the row which consists of a queen. 对于我的代码,我正在使用一维数组,即q [c](c是列)存储由皇后组成的行的值。 for example, q[1]=3 means that in column 2 (0 based array), there is a queen in row 4. 例如,q [1] = 3表示在第2列(基于0的数组)中,第4行中有一个皇后。

The original problem used a 2d array, so b[r][c] would either = 0, or 1, 1 being a queen, 0 being the rest. 最初的问题使用2d数组,因此b [r] [c]可能等于0或1,1是皇后,0是余数。 The program used an ok function to test each queen it was placing against all previously placed queens, and at the end, printed the result. 该程序使用ok函数来测试它所放置的每个皇后区是否与之前放置的所有皇后区相对应,最后打印结果。

The print function was
for(int j=0; j<c;j++)
   cout<<endl;
   for(int i=0; i<c;i++)
      cout<< b[i][j];

which printed 92 solution boards that looked something like this 它打印了92个解决方案板,看起来像这样

10000000
00001000
01000000
00010000
00000010
00100000
00000100
00000001

Now for my issue: I can NOT for the life of me figure out how to get the aforementioned 1d array b[c] to print a board which looks the same as this. 现在,我要解决的问题是:我一辈子都无法弄清楚如何获取上述1d数组b [c]来打印与此相同的电路板。 I am a beginner comp sci student but i still thought this would be much more trivial. 我是初学者计算机科学专业的学生,​​但我仍然认为这将变得微不足道。

I am not looking for an answer, more of a hint to lead me in the right direction, or maybe the first line of code. 我不是在寻找答案,更多是在向正确的方向指引我,或者可能是第一行代码。 Help is MUCH appreciated. 非常感谢您的帮助。 thanks 谢谢

Something like.... 就像是....

for (int y = 0; y<8; y++) {
   int val = b[y];
   for (int x=0; x<8; x++) {
      x==val ? printf("1") : printf("0") ;
   }
   printf("\n");
}

or 要么

    for (int y = 0; y<8; y++) {
       char[9] line;
       strcpy(line,"00000000");
       line[b[y]] = '1';
       printf("%s\n",line);
    }

I guess that if your array would tell you in which column of each row a queen is, you'd figure out the solution easily. 我猜想,如果您的数组可以告诉您皇后区在每一的哪一 ,那么您会很容易地找到解决方案。 Unfortunately, you have to produce your output row-by-row since that is how text terminals like it. 不幸的是,您必须逐行生成输出,因为这是文本终端喜欢的方式。 It's not that hard either: 这也不难:

  • For each row: 对于每一行:
    • For each column: 对于每一列:
      • If there is a queen at (row, column): print 1, else print 0 如果在(行,列)处有一个女王:打印1,否则打印0

You already have the code for this. 您已经有了此代码。 Except for the “if there is a queen…” part. 除了“如果有皇后……”部分。 So let's break the problem up and make it a function: 因此,让我们分解问题并使之成为一个函数:

bool is_there_a_queen(const int[8] board, int row, int col);

Will you be able to implement is_there_a_queen ? 您将能够实现is_there_a_queen吗?

#include <iostream>

/*
10000000
00001000
01000000
00010000
00000010
00100000
00000100
00000001
*/

int main()
{
   int q[] = {0, 4, 1, 3, 6, 2, 5, 7};
   for (int i = 0; i < 8; ++i )
   {
      for (int j = 0; j < 8; ++j )
      {
         if ( q[i] == j )
         {
            std::cout << 1;
         }
         else
         {
            std::cout << 0;
         }
      }
      std::cout << std::endl;
   }
   return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM