标题:小弟初来乍到,请多指教
只看楼主
嗜睡
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2019-12-5
 问题点数:0 回复次数:9 
小弟初来乍到,请多指教
有一行N行N列的灯,开始时全部是灭的,当你点击其中一盏灯是他的上下左右(若存在的话)就会全部变亮。现要求以最少地步数,将全部的灯点亮。(可以将具体的步骤显示出来吗,)。
搜索更多相关主题的帖子: 显示 全部 要求 步骤 存在 
2019-12-05 21:20
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
肯定是N呀,哪有这么简单的题?
我觉得将“全部变亮”改为“全部改变自身状态”比较合理
2019-12-05 21:31
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
题目中的“上下左右”是必须相邻,还是不需要?
2019-12-05 21:33
嗜睡
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2019-12-5
得分:0 
回复 3楼 rjsp
必须相邻
2019-12-08 22:46
嗜睡
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2019-12-5
得分:0 
回复 2楼 rjsp
对,就是这个意思
2019-12-08 22:47
嗜睡
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2019-12-5
得分:0 
回复 2楼 rjsp
大佬,救救孩子吧
2019-12-08 22:55
YSZAYMT
Rank: 2
等 级:论坛游民
帖 子:7
专家分:15
注 册:2019-12-9
得分:0 
回复 楼主 我也初级
小弟也是啊
2019-12-09 10:46
嗜睡
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2019-12-5
得分:0 
#include<graphics.h>
#include<conio.h>
#include<iostream>
#define maxnumber 14   //单边格子最大数目
#define G_length 50    //格子边长

void welcome();
void PaintGrid(int mid_x, int mid_y, int num, int color);
void left_key(MOUSEMSG m, int num);
void right_key(int num);
int judgefull(int num, int array[maxnumber][maxnumber]);
int mouse(int num);
void nextlevel(int num);


//定义结构体
struct Grid
{
    int left;
    int right;
    int up;
    int down;
    int array[maxnumber][maxnumber];
    int num;           //单边格子数目
}
grid;

int main()
{
    int  end = 0;
    grid.num = 4;
    welcome();
    PaintGrid(320, 240, grid.num, RGB(0, 255, 0));
    MOUSEMSG m;
    while (end != 1)
    {
        
        end = mouse(grid.num);
    }
    closegraph();
    return 0;
}

//欢迎来到点灯游戏
void welcome() {
    initgraph(640, 480);
    cleardevice();
    settextstyle(64, 0, L"黑体");
    settextcolor(GREEN);
    outtextxy(160, 50, L"亮灯程序");
    settextstyle(15, 0, L"宋体");
    outtextxy(100, 200, L"每点一个格子,上下左右的的格子也会做出与现状相反的动作");
    outtextxy(200, 240, L"总共11关,左键填色,右键清空");
   
    int c = 255;
    while (!_kbhit())
    {
        settextcolor(RGB(0, c, 0));
        outtextxy(280, 400, L"请按任意键继续");
                  c =c- 8;
        if (c < 0)
            c = 255;
        Sleep(30);
    }
    cleardevice();
}

/*mid_x 中心X的坐标,mid_y 中心Y的坐标, num 单边格子数目, color 格子和线条颜色*/
void PaintGrid(int mid_x, int mid_y, int num, int color)
{
    int x, y, nx, ny;
//游戏区域大小
    grid.left = mid_x - num * G_length / 2;
    grid.right = mid_x + num * G_length / 2;
    grid.up = mid_y - num * G_length / 2;
    grid.down = mid_y + num * G_length / 2;
//画格子
    setlinecolor(color);
    for (x = grid.left;x <= grid.right;x += G_length)
        line(x, grid.up, x, grid.down);
    for (y = grid.up;y<= grid.down;y += G_length)
        line(grid.left,y,grid.right,y);

//外边框

    for (x = 20; x > 10; x--)

    {
        line(grid.left - x, grid.up - x, grid.right + x, grid.up - x);
        
        line(grid.left - x, grid.down + x, grid.right + x, grid.down + x);
        
        line(grid.left - x, grid.up - x, grid.left - x, grid.down + x);
        
        line(grid.right + x, grid.up - x, grid.right + x, grid.down + x);
        
    }
//清空单元格
    for (x = 0;x < num;x++)
        for (y = 0;y < num;y++)
            grid.array[x][y] = -1;
    for(nx=0;nx<num;nx++)
        for (ny = 0;ny < num;ny++)
        {
            setfillcolor(BLACK);
            x = nx * G_length + grid.left;
            y = ny * G_length + grid.up;
            solidrectangle(x+1,y+1,x+G_length-1,y+G_length-1);
        }
}

//鼠标左键点击
void left_key(MOUSEMSG m, int num)
{
    int nx, ny, x, y;
    if (m.x > grid.left&& m.x<grid.right && m.y>grid.up&& m.y < grid.down)
    {
        nx = (int)(m.x - grid.left) / G_length;
        ny = (int)(m.y - grid.up) / G_length;
        grid.array[nx][ny] = -grid.array[nx][ny];
    //转换格子颜色
        if (nx >= 0 && nx < num - 1)
            grid.array[nx + 1][ny] = -grid.array[nx + 1][ny];
        if (nx > 0 && nx < num )
            grid.array[nx -1][ny] = -grid.array[nx - 1][ny];
        if (ny >= 0 && ny < num - 1)
            grid.array[nx][ny+1] = -grid.array[nx][ny+1];
        if (ny > 0 && ny < num)
            grid.array[nx][ny - 1] = -grid.array[nx][ny - 1];



        for (nx = 0; nx < num; nx++)

            for (ny = 0; ny < num; ny++) {

                if (grid.array[nx][ny] == 1)

                    setfillcolor(GREEN);
                else

                    setfillcolor(BLACK);
                x = nx * G_length + grid.left;
                y = ny * G_length + grid.up;

                solidrectangle(x + 1, y + 1, x + G_length - 1, y + G_length - 1);

            }
    }

}
//右键清空
void right_key(int num)
{
    int x, y, nx, ny;
    for (x = 0;x < num;x++)
        for (y = 0;y < num;y++)
            grid.array[x][y] = -1;
    for (nx = 0;nx < num;nx++)
        for (ny = 0;ny < num;ny++)
        {
            setfillcolor(BLACK);
            x = nx * G_length + grid.left;
            y = ny * G_length + grid.up;
            solidrectangle(x + 1, y + 1, x + G_length - 1, y + G_length - 1);
        }
}
//判断是否填满
int judgefull(int num, int array[maxnumber][maxnumber])
{
    int c = -1;
    int nx = 0, ny = 0;
    while (nx < num && ny < num)
    {
        for (nx = 0;nx < num;nx++)
            for (ny = 0;ny < num;ny++)
                if (array[nx][ny] == 1)
                    continue;
                else
                    return c;
    }
    c = 1;
    return c;
}

//下一关
void nextlevel(int num)
{
    //批量绘图
    BeginBatchDraw();
    for (int y = 0;y <= 480;y += 5)
    {
        setlinecolor(RGB(0, 255, 0));
        line(0, y, 640, y);
        line(0, 480 - y, 640, 480 - y);
        outtextxy(300, y - 8,L"再接再厉");
        FlushBatchDraw();
        Sleep(20);
        setfillcolor(BLACK);
        solidrectangle(0, y, 640, y - 10);
        solidrectangle(0,480- y, 640, 480-y+1);
    }
    EndBatchDraw();
    PaintGrid(320, 240, num, RGB(0, 255, 0));
}
//传递鼠标
 int mouse(int num)
{
    MOUSEMSG m;
    while (1)
    {
        m = GetMouseMsg();
        switch (m.uMsg)
        {
        case WM_LBUTTONDOWN:
            left_key(m, grid.num);
            if (judgefull(grid.num, grid.array) == 1)
            {
                grid.num++;
                  
                if (grid.num > maxnumber)
                {
                    return 1;
                    break;
                }
                else
                    nextlevel(grid.num);
            }
            break;
        case WM_RBUTTONDOWN:
            right_key(grid.num);
            break;
        default:
            break;
        }
    }
}


最少步数怎么弄啊

2019-12-25 13:17
嗜睡
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2019-12-5
得分:0 
最后一点点了,帮我看看吧
2019-12-25 13:18
YSZAYMT
Rank: 2
等 级:论坛游民
帖 子:7
专家分:15
注 册:2019-12-9
得分:0 
回复 2楼 rjsp
赚个分
2019-12-27 15:49



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-498229-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.086036 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved