每次显示一个元素的二维数组

Showing 2D array with one element at the time

本文关键字:一个 元素 二维数组 显示      更新时间:2023-10-16

我有一个充满0和1的二维数组。我必须这样显示这个数组:-总是显示0- 1只显示一个

假设它看起来像一个迷宫,其中0是一堵墙,1是当前位置。我如何在c++中做到这一点?

编辑:我想到了一个解决办法,也许还有更简单的办法。如果我创建_array的副本复制0和空格而不是1。然后在循环中,我将_array"1"分配给第二个数组,然后显示整个数组,然后用空格交换1 ?

EDIT2:

int _tmain(int argc, _TCHAR* argv[])
{
    file();
    int k=0,l=0;
    for(int i=0;i<num_rows;i++)
    {
        for(int j=0;j<num_chars;j++)
        {
            if(_array[i][j] == 1)
            {
                k=i;
                l=j;
                break;
            }
        }
    }
    while(1)
    {
        for(int i=0;i<num_rows;i++)
        {
            for(int j=0;j<num_chars;j++)
            {
                if(_array[i][j] == 0) printf("%d",_array[i][j]);
                else if(_array[i][j]==1)
                {
                    if(k==i && l==j)
                    {
                        printf("1");                
                    }
                    else printf(" ");
                }               
                l++;
                if(l>num_chars) break;
            }
            k++;
            l=0;
            printf("n");               
        }
        k=0;
        system("cls");
    }
    return 0;
}

我写了一些类似的东西,但我仍然不知道如何在正确的时刻清除屏幕。函数file()从文件读取2D数组

假设你想要这样的内容

000000
0    0
0000 0
0 1  0
0 0000
000000

可以在出现0时打印一个,如果不出现则打印一个空格。要处理当前位置,您可以使用两个额外的变量,如posX, posY。现在,每次在数组中找到1时,检查if (j == posX && i = posY)并打印1,如果是的话…

由于您只需要在不同的可能位置可视化迷宫,我将提出一个简单的显示函数。DisplayMaze(int x, int y)以所需的格式将迷宫打印到屏幕上。如果_array[y][x] == 1也有一个单独的1

void DisplayMaze(int x, int y)
{
    for (int row = 0; row < num_rows; row++)
    {
        for (int col = 0; col < num_chars; col++)
        {
            if (_array[row][col] == 0)
                std::cout << "0 ";
            else if (row == y && col == x)
                std::cout << "1 ";
            else
                std::cout << "  ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

为了显示所有可能的位置,您必须遍历所有位置并检查当前位置是否在数组中标记为1(否则显示将没有意义)

for (int y = 0; y < num_rows; y++)
{
    for (int x = 0; x < num_chars; x++)
    {
        if (_array[y][x] == 1)
        {
            DisplayMaze(x, y);
        }
    }
}

输出应该如下所示:

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

然而,我推荐一种更像c++的方法,因为迷宫可以作为一个类来实现。这个类可以自带显示方法并封装内部数据。它可以像这样:

class Maze
{
public:
    // generate empty maze with given size
    Maze(int width, int height);
    // destructor
    ~Maze();
    // print maze if the given position is marked with 1
    void printPosition(int x, int y) const;
    // takes a cstring as input to initialize the maze from
    Maze& operator<<(const char* input);
    // returns true if the given position is marked with 1
    bool isValidPosition(int x, int y) const;
private:
    // this is the actual representation of the maze
    std::vector<std::vector<int> > grid_;
};

它的用法如下:

Maze myMaze(num_chars, num_rows);
myMaze << "000000"
    "011110"
    "000010"
    "011110"
    "010000"
    "000000";
for (int y = 0; y < num_rows; y++)
{
    for (int x = 0; x < num_chars; x++)
    {
        if (myMaze.isValidPosition(x,y))
        {
            myMaze.printPosition(x,y);
        }
    }
}

雇佣你去* (解决] *

#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
char map[x][y];
memset(map, 'a', sizeof(map));
int y_pos = 0;
for (int x_pos = 0; x_pos < x * y; x_pos++){
    if (x_pos == x){
       x_pos = 0;
       y_pos = y_pos + 1;
       cout<<endl;
       }
    if (y_pos == y){
       system("pause");
       return 0;       
       }
    cout<<map[x_pos][y_pos];
    }