将二维数组转换为字符

时间:2015-12-08 17:08:11

标签: c arrays char maze

我使用Depth-First搜索算法作为学校项目制作迷宫。迷宫正在我想要的工作,但我有一点问题。我必须使用#作为墙而.作为打印迷宫时的路径,但我使用0作为墙,而1作为路径首先考虑它以后会很容易改变它,但我错了。如何将01更改为#.

代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void nahodne_suradnice(int *r, int *s, int n)
{
srand(time(NULL));
*r = ((rand() % n) - 1) + 2;
if (*r == 1 || *r == n)
{
    *s = (rand() % n) + 2;
}
else
{
    if (rand() % 2 == 1)
        *s = 1;
    else
        *s = n;
}
}
int main()
{
int i, j, n, r, s,smer,posledny_smer[1500];
int maze[500][500];
scanf_s("%d", &n);
if (n < 10 || n > 100)
{
    return 0;
}
//vynulovanie pola/bludiska
for (i = 1; i < n + 1; i++)
{
    for (j = 1; j < n + 1; j++)
    {
        maze[i][j] = 0;
    }
}
//nahodny vyber zaciatku bludiska
nahodne_suradnice(&r, &s, n);

//generovanie bludiska
j = 0;
maze[r][s] = 2;
for (i = 0 ;; i++)
{
    //backtracking
    if ((maze[r - 1][s] == 1 || maze[r - 2][s] == 1 || r - 2 <=1 || s==n || s==1) && (maze[r][s + 1] == 1 || maze[r][s + 2] == 1 || s + 2 >= n || r == n || r==1) && (maze[r + 1][s] == 1 || maze[r + 2][s] == 1 || r + 2 >= n || s == n || s==1) && (maze[r][s - 1] == 1 || maze[r][s - 2] == 1 || s - 2 <=1 || r == n || r==1))
    {
        if (posledny_smer[j-1] == 1)
            if (maze[r + 1][s] == 1 && maze[r + 2][s] == 1)
            {
                r += 2;
                j--;
                continue;
            }
            else
            {
                j--;
                continue;
            }
        if (posledny_smer[j-1] == 2)
            if (maze[r][s - 1] == 1 && maze[r][s - 2] == 1)
            {
                s -= 2;
                j--;
                continue;
            }
            else
            {
                j--;
                continue;
            }
        if (posledny_smer[j-1] == 3)
            if (maze[r - 1][s] == 1 && maze[r - 2][s] == 1)
            {
                r -= 2;
                j--;
                continue;
            }
            else
            {
                j--;
                continue;
            }
        if (posledny_smer[j-1] == 4)
            if (maze[r][s + 1] == 1 && maze[r][s + 2] == 1)
            {
                s += 2;
                j--;
                continue;
            }
            else
            {
                j--;
                continue;
            }
        if (j == 0)
        {
            if (r == n)
            {
                nahodne_suradnice(&r, &s,n);
                maze[1][s] = 3;
                maze[2][s] = 3;
            }
            if (r == 1)
            {
                nahodne_suradnice(&r, &s, n);
                maze[n][s] = 3;
                maze[n - 1][s] = 3;
            }
            if (s == n-2)
            {
                nahodne_suradnice(&r, &s, n);
                maze[r][1] = 3;
                maze[r][2] = 3;
            }
            if (s == 3)
            {
                nahodne_suradnice(&r, &s, n);
                maze[r][n] = 3;
                maze[r][n-1] = 3;
            }

                break;
        }
    }
    //buranie stien
    smer = (rand() % 4) + 1;
    if (smer == 1)
    {
        if (r - 2 >1 && s<n && s>1)
        {
            if (maze[r - 1][s] == 1 || maze[r - 2][s] == 1)
                continue;
            maze[r - 1][s] = 1;
            maze[r - 2][s] = 1;
            r -= 2;
            posledny_smer[j] = smer;
            j++;
            continue;
        }
    }
    if (smer == 2)
    {
        if (s + 2 < n && r < n && r>1)
        {
            if (maze[r][s+1] == 1 || maze[r][s+2] == 1)
                continue;
            maze[r][s + 1] = 1;
            maze[r][s + 2] = 1;
            s += 2;
            posledny_smer[j] = smer;
            j++;
            continue;
        }
    }
    if (smer == 3)
    {
        if (r + 2 < n && s < n && s>1)
        {
            if (maze[r + 1][s] == 1 || maze[r + 2][s] == 1)
                continue;
            maze[r + 1][s] = 1;
            maze[r + 2][s] = 1;
            r += 2;
            posledny_smer[j] = smer;
            j++;
            continue;
        }
    }
    if (smer == 4)
    {
        if (s - 2 >1 && r < n && r>1)
        {
            if (maze[r][s-1] == 1 || maze[r][s-2] == 1)
                continue;
            maze[r][s - 1] = 1;
            maze[r][s - 2] = 1;
            s -= 2;
            posledny_smer[j] = smer;
            j++;
            continue;
        }
    }
}


//vypis bludiska
for (i = 1; i < n + 1; i++)
{
    for (j = 1; j < n + 1; j++)
    {
        printf("%d", maze[i][j]);
    }
    printf("\n");
}
system("PAUSE");
return 0;
}

`

感谢。

2 个答案:

答案 0 :(得分:1)

将迷宫的定义更改为:

char maze[500][500]

并更改所有引用以使用char而不是int。然后返回&#39;#&#39;和&#39;。&#39;而不是0和1。

答案 1 :(得分:0)

您可以从0和1更改为#和。 (或任何其他表示)打印时。例如,您可以更改:

printf("%d", maze[i][j]);

switch(maze[i][j]) {
  case 0:
    printf("#");
    break;
  case 1:
    printf(".");
    break;
  default:
    printf("X"); /* in case you have some value other than 0 or 1 in the maze */
 }

如果您这样做,您可以更改要显示迷宫的字母而不更改代码的其他部分。

相关问题