首页 > 解决方案 > 有什么方法可以通过 2D 排列的索引,就好像它是游戏板上的一块或类似的东西?

问题描述

我正在尝试为分配给我的练习开发代码,但我找不到任何方法来完成我需要完成的工作。有问题的练习包括更改和打印 10x10 2d 数组的值,该数组用零填充,生成如下模式:

1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 1 1 1 1 1 1 1 1
1 0 1 0 0 0 0 0 0 0
1 0 1 0 1 1 1 1 1 1
1 0 1 0 1 0 0 0 0 1
1 0 1 0 1 0 1 1 1 1
1 0 1 0 1 0 1 0 0 0
1 0 1 0 1 0 1 0 1 1
1 0 1 1 1 0 1 1 1 0

它可能不是很明显,但是从数组的索引 a[9][0] 开始到数组的 a[8][9] 结束,它开始改变数组的那些零,形成蛇形图案. 想了一会儿,我试图用我制作的这段代码来解决它,但我认为它有几个错误,即使编译器没有标记任何如此明显的错误:

#include <iostream>
using namespace std;

int main()
{
    int px = 0;
    int py = 9;
    
    int a[10][10];
    
    for(int i = 0; i <= 10; i++) {
        for(int j = 0; j <= 10; j++) {
        
            a[i][j] = 0;
        
        }
    }


    if (py == 9 && px == 0) {
        while(py >= 0) {
            a[py][px] = 1;
            py = py - 1;
        }
    }
    if (py == 0 && px == 0) {
        while(px <= 9) {
            a[py][px] = 1;
            px = px + 1;
        }
    }
    if (py == 0 && px == 9) {
        while(py <= 2) {
            a[py][px] = 1;
            py = py + 1;
        }
    }
    if (py == 2 && px == 9) {
        while(px >= 2) {
            a[py][px] = 1;
            px = px - 1;
        }
    }
    if (py == 2 && px == 2) {
        while(py <= 9) {
            a[py][px] = 1;
            py = py + 1;
        }
    }
    if (py == 9 && px == 2) {
        while(px <= 4) {
            a[px][py] = 1;
            px = px + 1;
        }
    }
    if (py == 9 && px == 4) {
        while(py >= 4) {
            a[px][py] = 1;
            py = py - 1;
        }
    }
    if (py == 4 && px == 4) {
        while(px <= 9) {
            a[py][px] = 1;
            px = px + 1;
        }
    }
    if (py == 4 && px == 9) {
        while(py <= 6) {
            a[py][px] = 1;
            py = py + 1;
        }
    }
    if (py == 6 && px == 9) {
        while(px >= 6) {
            a[py][px] = 1;
            px = px - 1;
        }
    }
    if (py == 6 && px == 6) {
        while(py <= 9) {
            a[py][px] = 1;
            py = py + 1;
        }
    }
    if (py == 9 && px == 6) {
        while(px <= 8) {
            a[py][px] = 1;
            px = px + 1;
        }
    }
    if (py == 9 && px == 8) {
        while(py >= 8) {
            a[py][px] = 1;
            py = py - 1;
        }
    }
    if (py == 8 && px == 8) {
        while(px <= 9) {
            a[py][px] = 1;
            px = px + 1;
        }
    }
    
    for(int k=0; k<=9; k++) {       
        for(int l=0; l<=9; l++) {
            cout << a[k][l] << " ";
        }
        cout << endl;   
    }
return 0;
}

我希望你能帮助我,我仍然继续学习一点 C++ 的基本知识,可能是它让我摆脱了一些我不知道的概念或类似的东西。提前致谢。

标签: c++arrays

解决方案


请参考这个在第一个 conv 函数中我做了一个没有蛇的一般模式然后我专注于蛇连接其他行或列的位置

#include <bits/stdc++.h>
using namespace std;

void conv1(int a[10][10])
{
    int temp=0;
    for(int i=0; i<10; i++)
    {
        if(i%2==0)
        {
            for(int j=temp; j<10; j++)
            {
                a[i][j]=1;
                a[j][i]=1;
            }
        }
        temp=temp+1;
    }
}
/*
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0
1 0 1 1 1 1 1 1 1 1
1 0 1 0 0 0 0 0 0 0
1 0 1 0 1 1 1 1 1 1
1 0 1 0 1 0 0 0 0 0
1 0 1 0 1 0 1 1 1 1
1 0 1 0 1 0 1 0 0 0
1 0 1 0 1 0 1 0 1 1
1 0 1 0 1 0 1 0 1 0
*/
void conv2(int a[10][10])
{
    int temp=0;
    for(int i=0; i<10; i++)
    {
        if(i%2!=0&&temp==0)
        {
            a[i][9]=1;
            temp=1;
        }
        else if(i%2!=0)
        {
            temp=0;
        }
    }
    for(int i=0; i<10; i++)
    {
        if(i%2!=0&&temp==0)
        {
            a[9][i]=1;
            temp=1;
        }
        else if(i%2!=0)
        {
            temp=0;
        }
    }
    a[9][9]=0;
}

int main(){

    int a[10][10];
    for(int i=0; i<10;i++)
    {
        for(int j=0; j<10; j++)
        {
            a[i][j]=0;
        }
    }
    conv1(a);
    conv2(a);
    for(int i=0; i<10;i++)
    {
        for(int j=0; j<10; j++)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
}

推荐阅读