一个智力题的判断程序

题目描述如下:

闲着也是闲着,不如开动脑筋,来试试这道题吧!
123
456
789     如图,有九个开关呈3x3排列。按动其中一个开关可以影响他本身以及周围相邻的开关。如,按动5,会影响24568一共五个开关。那么,如果我只想改变1的状态,最少需要几步呢?

下面的代码中我将0设置为原先状态,1设计为改变后的状态

代码:

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
using namespace std;
int step=0;
int maxstep=0;
int stepfun[11][10]={0};
int ststep=100;
int Mt[11][11];
int Cp[11][11];
int maxx,maxy;
int stopx,stopy;
int temp;
int count;
int stepway[11];
int power=0;
int nbu;
int count1=0;
int stepmax=0;
void _checkway()
{
    for (int i=1;i<=maxy;i++)
    {
        for (int j=1;j<=maxx;j++)
        {
            if (Mt[i][j]==0) count++;
        }
    }
    if ((count==(maxx*maxy-1))&&Mt[stopy][stopx]==1)
    {
            power=1;
            stepmax=step;
            Sleep(1000);
            system("cls");
            step--;
            return;
    }
}
void _showway()
{
    if (power==1)
    {
        printf("在%d步内有解(步骤为):",nbu);
    }
    for (int i=1;i<=stepmax;i++)
    {
        printf("%d ",stepway[i]);
    }
    printf("\n");
}
void _show()
{
    count1++;
    printf("此时的状态:\n");
    for (int i=1;i<=maxy;i++)
    {
        for (int j=1;j<=maxx;j++)
        {
            printf("%d ",Mt[i][j]);
        }
        printf("\n");
    }
}
void change(int y,int x)
{
    Mt[y][x]=!Mt[y][x];
    if (x+1<=maxx)
    {
        Mt[y][x+1]=!Mt[y][x+1];
    }
    if (x-1>=1)
    {
        Mt[y][x-1]=!Mt[y][x-1];
    }
    if (y-1>=1)
    {
        Mt[y-1][x]=!Mt[y-1][x];
    }
    if (y+1<=maxy)
    {
        Mt[y+1][x]=!Mt[y+1][x];
    }
}
//回溯函数,每次按都有9种选择 
void An()
{    
    if (power==1) return;
    temp=0;
    step++;
    if (step>nbu)
    {
        step--;
        return;
    }
    while(1)
    {
        if (power==1) return;
        count=0;
        if (power==1) return;
        while(1)
        {
            temp++;
            if (stepfun[step][temp]==0)
            {
                break;
            }
        }
        switch(temp-1)
        {
            case 1:
                change(1,1);
                system("cls");
                _show();
                break;
            case 2:
                change(1,2);
                system("cls");
                _show();
                break;
            case 3:
                change(1,3);
                system("cls");
                _show();
                break;
            case 4:
                change(2,1);
                system("cls");
                _show();
                break;
            case 5:
                change(2,2);
                system("cls");
                _show();
                break;
            case 6:
                change(2,3);
                system("cls");
                _show();
                break;
            case 7:
                change(3,1);
                system("cls");
                _show();
                break;
            case 8:
                change(3,2);
                system("cls");
                _show();
                break;
            case 9:
                change(3,3);
                system("cls");
                _show();
                break;
            
        }
        switch (temp)
        {
            case 1:
                if (stepfun[step][1]==0)
                {
                    stepway[step]=1;
                    stepfun[step][1]=1;
                    change(1,1);
                    system("cls");
                    _show();
                    _checkway();
                    An();
                }
                break;
            case 2:
                if (stepfun[step][2]==0)
                {
                    stepway[step]=2;
                    stepfun[step][2]=1;
                    change(1,2);
                    system("cls");
                    _show();
                    _checkway();
                    An();
                }
                break;
            case 3:
                if (stepfun[step][3]==0)
                {
                    stepway[step]=3;
                    stepfun[step][3]=1;
                    change(1,3);
                    system("cls");
                    _show();
                    _checkway();
                    An();
                }
                break;
            case 4:
                if (stepfun[step][4]==0)
                {
                    stepway[step]=4;
                    stepfun[step][4]=1;
                    change(2,1);
                    system("cls");
                    _show();
                    _checkway();
                    An();
                }    
                break;        
            case 5:
                if (stepfun[step][5]==0)
                {
                    stepway[step]=5;
                    stepfun[step][5]=1;
                    change(2,2);
                    system("cls");
                    _show();
                    _checkway();
                    An();
                }
                break;
            case 6:
                if (stepfun[step][6]==0)
                {
                    stepway[step]=6;
                    stepfun[step][6]=1;
                    change(2,3);
                    system("cls");
                    _show();
                    _checkway();
                    An();
                }
                break;
            case 7:
                if (stepfun[step][7]==0)
                {
                    stepway[step]=7;
                    stepfun[step][7]=1;
                    change(3,1);
                    system("cls");
                    _show();
                    _checkway();
                    An();
                }
                break;
            case 8:
                if (stepfun[step][8]==0)
                {
                    stepway[step]=8;
                    stepfun[step][8]=1;
                    change(3,2);
                    system("cls");
                    _show();
                    _checkway();
                    An();
                }
                break;
            case 9:
                if (stepfun[step][9]==0)
                {
                    stepway[step]=9;
                    stepfun[step][9]=1;
                    change(3,3);
                    system("cls");
                    _show();
                    _checkway();
                    An();
                }
                break;
            default:
                for (int i=1;i<=9;i++)
                {    
                    stepfun[step][i]=0;
                }
                step--;
                temp=0;
                return;        
        }
    }
    step--;
    temp=0;
    return;

int main()
{
    int art;
    printf("请输入矩阵大小(格式如:5 5):\n");
    scanf("%d %d",&maxx,&maxy);
    printf("请输入该矩阵:\n");
    for (int i=1;i<=maxy;i++)
    {
        for (int j=1;j<=maxx;j++)
        {
            scanf("%d",&Mt[i][j]);
            Cp[i][j]=Mt[i][j];
        }
    }
    printf("你只想改变谁的状态(x,y):");
    scanf("%d %d",&stopx,&stopy);
    printf("你想验证它在几步内有解:");
    scanf("%d",&nbu);
    system("cls");
    An();
    if (power==1)
    {
        printf("在%d步内有解(步骤为):",nbu);
    }
    for (int i=1;i<=stepmax;i++)
    {
        printf("%d ",stepway[i]);
    }
    if (power==1)
    {
        printf("\n");
        _show();
        printf("\n");
        printf("若要开始演绎请按1:");
        scanf("%d",&art);
    }
    if (stepmax>0&&art==1)
    {
        for (int i=1;i<=maxy;i++)
        {
            for (int j=1;j<=maxx;j++)
            {
                Mt[i][j]=Cp[i][j];
            }    
        }
        system("cls");
        printf("演绎开始:\n");
        _showway();
        _show();
        Sleep(2000);
    }
    else if(power==0)
    {
        printf("在%d步内无解",nbu);
    }
    for (int i=1;i<=stepmax&&art==1;i++)
    {
        switch (stepway[i])
        {
            case 1:
                change(1,1);
                system("cls");
                printf("演绎开始:\n");
                _showway();
                _show();
                Sleep(2000);
                break;
            case 2:
                change(1,2);
                system("cls");
                printf("演绎开始:\n");
                _showway();
                _show();
                Sleep(2000);
                break;
            case 3:
                change(1,3);
                system("cls");
                printf("演绎开始:\n");
                _showway();
                _show();
                Sleep(2000);
                break;
            case 4:
                change(2,1);
                system("cls");
                printf("演绎开始:\n");
                _showway();
                _show();
                Sleep(2000);
                break;        
            case 5:
                change(2,2);
                system("cls");
                printf("演绎开始:\n");
                _showway();
                _show();
                Sleep(2000);
                break;
            case 6:
                change(2,3);
                system("cls");
                printf("演绎开始:\n");
                _showway();
                _show();
                Sleep(2000);
                break;
            case 7:
                change(3,1);
                system("cls");
                printf("演绎开始:\n");
                _showway();
                _show();
                Sleep(2000);
                break;
            case 8:
                change(3,2);
                system("cls");
                printf("演绎开始:\n");
                _showway();
                _show();
                Sleep(2000);
                break;
            case 9:
                change(3,3);
                system("cls");
                printf("演绎开始:\n");
                _showway();
                _show();
                Sleep(2000);
                break;
        }
    }
    if (power==1) printf("\n演绎结束"); 
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值