异形数组的生成

 
// PBox.cpp : Defines the entry point for the console application.
/*
 
方格生成程序 by xmxoxo 
  题目:
如下图所示:把该图存于int data[10][10]中,请用代码实现
  1   2   3    4  5 
22 21 20 19  6
23 24 25 18  7
14 15 16 17  8
13 12 11 10  9

*/

#include 
"stdafx.h"
#include 
"stdlib.h"
#include 
"iostream.h"
#include 
"iomanip.h"

//最大100
int data[10][10];
int pic[10][10];
int dire[4][2]={{0,-1},
{
1,0},{0,1},{-1,0}};

void init()
{
    
int i,j;
    
for (i=0;i<10;i++)
    {
        
for (j=0;j<10;j++)
        {
            data[i][j]
=0;
        }
    }
}
//生成K方格
void gen(int k)
{
    
int x,y,n,d,od,ct,s;
    x 
= -1//初始坐标 -1,0
    y = 0;
    d 
= 1//初始方向1
    od = 1;
    n 
= 1//要填的数
    ct = 0//转弯计数器
    s=0//标志
    while (n<=k*k)
    {
        
//计算下一个位置
        x = x + dire[d][0];
        y 
= y + dire[d][1];
        
//判断超出
        if ((x>=k||x<0)||(y>=k||y<0))
        {
            
//,超出则退回原位
            x = x -  dire[d][0];
            y 
= y - dire[d][1];
            
//转方向
            if (s==0){od=d;}
            d
++;
            
if (d==4){d=0;}
            s 
= 1;
            
continue;
        }
        
//有数字
        if (data[x][y]!=0)
        {
            
//退回原位
            x = x -  dire[d][0];
            y 
= y - dire[d][1];
            
//转方向
            if (s==0){od=d;}
            d
++;
            
if (d==4){d=0;}
            s
=1;
            
continue;
        }
        
//判断下一个位置
        
//为空,则填入数
        if (data[x][y]==0
        {
            data[x][y] 
= n;
            
if (d==0||d==2)
            {
                pic[x][y] 
= 0;
            }
            
if (d==1||d==3)
            {
                pic[x][y] 
= 1;
            }

            
if (s==1//转角
            {
                
//pic[x -  dire[d][0]][y - dire[d][1]] = d+2+od;
                
                
switch (od)
                {
                    
case 0:
                        
if (d==1)
                        {
                            pic[x 
-  dire[d][0]][y - dire[d][1]] = 4;
                        }
                        
if (d==3)
                        {
                            pic[x 
-  dire[d][0]][y - dire[d][1]] = 2;
                        }
                        
break;
                    
case 1:
                        
if (d==0)
                        {
                            pic[x 
-  dire[d][0]][y - dire[d][1]] = 3;
                        }
                        
if (d==2)
                        {
                            pic[x 
-  dire[d][0]][y - dire[d][1]] = 2;
                        }
                        
break;
                    
case 2:
                        
if (d==1)
                        {
                            pic[x 
-  dire[d][0]][y - dire[d][1]] = 5;
                        }
                        
if (d==3)
                        {
                            pic[x 
-  dire[d][0]][y - dire[d][1]] = 3;
                        }
                        
break;
                    
case 3:
                        
if (d==0)
                        {
                            pic[x 
-  dire[d][0]][y - dire[d][1]] = 5;
                        }
                        
if (d==2)
                        {
                            pic[x 
-  dire[d][0]][y - dire[d][1]] = 4;
                        }
                        
break;
                }
                
            }

            n
++;
            
if (s==1)
            {
                ct
++;
                
if (ct==4){ct=0;}
                s
=0;
            }
            
//如果是第3次转向
            if (ct==3)
            {
                
//填写一个数后立即转下一个方向
                //注释掉下面4句得到的即为"回字型"
                if (s==0){od=d;}
                
d++;
                
f (d==4){d=0;}
                
s=1;
            }
            
continue;
        }
    }
}

//输出
void out(int k)
{
    
int i,j;
    
for (i=0;i<k;i++)
    {
        
for (j=0;j<k;j++)
        {
            cout 
<<setw(4<< data[j][i];
        }
        cout
<<endl;
    }

    
for (i=0;i<k;i++)
    {
        
for (j=0;j<k;j++)
        {
            
//cout<<pic[j][i];
            
            
switch (pic[j][i])
            {
            
case 0:
                cout 
<< "";
                
break;
            
case 1:
                cout 
<< "";
                
break;
            
case 2:
                cout 
<< "";
                
break;
            
case 3:
                cout 
<< "";
                
break;
            
case 4:
                cout 
<< "";
                
break;
            
case 5:
                cout 
<< "";
                
break;
            }
            
        }
        cout
<<endl;
    }
}

int main()
{
    
int m;
    
while (1)
    {
        cout
<< "请输入2-10之间的数[0退出]:";
        cin
>>m;
        
if (m==0)
        {
            
break;
        }
        
if (m<=10||m>=2)
        {
            init();
            gen(m);
            
out(m);
        }
    }
    
return 0;
}

"凹字型"

请输入2-10之间的数[0退出]:9
   1   2   3   4   5   6   7   8   9
  46  45  44  43  42  41  40  39  10
  47  48  49  50  51  52  53  38  11
  76  75  74  73  72  71  54  37  12
  77  78  79  80  81  70  55  36  13
  64  65  66  67  68  69  56  35  14
  63  62  61  60  59  58  57  34  15
  26  27  28  29  30  31  32  33  16
  25  24  23  22  21  20  19  18  17
────────┐
┌──────┐│
└─────┐││
┌────┐│││
└────││││
┌────┘│││
└─────┘││
┌──────┘│
└───────┘
请输入2-10之间的数[0退出]:0

"回字型"
请输入2-10之间的数[0退出]:10
   1   2   3   4   5   6   7   8   9  10
  36  37  38  39  40  41  42  43  44  11
  35  64  65  66  67  68  69  70  45  12
  34  63  84  85  86  87  88  71  46  13
  33  62  83  96  97  98  89  72  47  14
  32  61  82  95 100  99  90  73  48  15
  31  60  81  94  93  92  91  74  49  16
  30  59  80  79  78  77  76  75  50  17
  29  58  57  56  55  54  53  52  51  18
  28  27  26  25  24  23  22  21  20  19
─────────┐
┌───────┐│
│┌─────┐││
││┌───┐│││
│││┌─┐││││
││││─┘││││
│││└──┘│││
││└────┘││
│└──────┘│
└────────┘
请输入2-10之间的数[0退出]:0
Press any key to continue

 

这是一个比较有趣的问题,可以使用递归回溯算法来生成迷宫,并使用图形库来可视化迷宫的过程。 首先需要定义一个迷宫单元的类,包含以下属性和方法: - 属性:坐标、边数、是否已被访问、是否是起点和终点、与相邻单元的连接情况等。 - 方法:生成随机通道、检查是否已访问、获取相邻未访问单元等。 然后,定义一个迷宫类,包含以下属性和方法: - 属性:迷宫单元数组、起点和终点的坐标等。 - 方法:生成迷宫、可视化迷宫、走出迷宫等。 接下来是代码实现的一些细节: 1. 生成迷宫:从起点开始,使用递归回溯算法依次访问相邻单元,并随机生成通道,直到所有单元都被访问。 2. 可视化迷宫:使用图形库绘制每个迷宫单元和其相邻单元之间的通道,可以使用不同颜色表示起点和终点。 3. 走出迷宫:从起点开始,使用搜索算法(如深度优先搜索)依次访问相邻单元,并记录路径,直到到达终点或者无法继续前进。 下面是一个简单的C++实现,仅供参考: ``` #include <iostream> #include <vector> #include <stack> #include <cstdlib> #include <ctime> #include <graphics.h> // 迷宫单元类 class Cell { public: Cell(int x, int y, int s) : x(x), y(y), sides(s) {} int x; // 行坐标 int y; // 列坐标 int sides; // 边数 bool visited = false; // 是否已访问 bool start = false; // 是否是起点 bool end = false; // 是否是终点 std::vector<Cell*> neighbors; // 相邻单元指针数组 std::vector<bool> walls;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值