2011年度变态迷宫数学题:从左边入口处的 2011 进去,在迷宫里转悠,最后变成 2012 从右边出来。你可以在迷宫里转圈,可以重复之前走过的路,但不能往回退着走。

news/2024/7/8 4:55:51 标签: struct, 图形

这是昨天在人人看见的一个题目。所以没事干,就解决了一下这个题目。思路就是广域搜索,比较笨的方法,但是能解决问题。不知道谁有更好的方法,给个建议。

题目要求的图形如右所示。


代码:

#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
typedef struct node
{
    int x;//记录上一次的位置
    double y;//记录结果
    int num;
    char str[10];//记录走的方向
}node;


int getresult(vector<node>&vec)
{
    int i=0;
    int j=0;
    while(i<10000000)
    {
        node n=vec[i];
        node temp = n;
        int k=i;
        switch(temp.num)
        {
            case 7:
            {
                temp.y=temp.y/2;
                temp.y=temp.y+7;
                temp.num=temp.num;
                strcpy(temp.str,"/2+7");
                temp.x=i;
                vec.push_back(temp);
                temp = n;
                temp.y=temp.y-5;//-5 *3
                if(temp.y==2012)
                    return i;
                temp.y=temp.y*3;
                strcpy(temp.str,"-5*3");
                temp.x=i;
                temp.num=3;
                vec.push_back(temp);


                temp=n;
                temp.x=i;
                temp.y=temp.y*3;
                if(temp.y==2012)
                    return i;
                temp.y=temp.y-5;
                strcpy(temp.str,"*3-5");
                temp.num=5;
                vec.push_back(temp);


            }
            break;
            case 2:
            {
                temp.y=temp.y+7;
                temp.y=temp.y/2;
                temp.num=temp.num;
                strcpy(temp.str,"+7/2");
                temp.x=i;
                vec.push_back(temp);


                temp = n;
                temp.y=temp.y-5;//-5 *3
                if(temp.y==2012)
                    return i;
                temp.y=temp.y*3;
                temp.x=i;
                strcpy(temp.str,"-5*3");
                temp.num=3;
                vec.push_back(temp);


                temp=n;
                temp.x=i;
                temp.y=temp.y*3;
                if(temp.y==2012)
                    return i;
                temp.y=temp.y-5;
                temp.num=5;
                strcpy(temp.str,"*3-5");
                vec.push_back(temp);
            }
            break;
            case 3:
            {
                temp.y=temp.y-5;
                temp.y=temp.y*3;
                temp.num=temp.num;
                strcpy(temp.str,"-5*3");
                temp.x=i;
                vec.push_back(temp);
                if(temp.y==2012)
                    return i;
                temp = n;
                temp.y=temp.y+7;//-5 *3
                temp.y=temp.y/2;
                temp.x=i;
                strcpy(temp.str,"+7/2");
                temp.num=2;
                vec.push_back(temp);


                temp=n;
                temp.x=i;
                temp.y=temp.y/2;
                temp.y=temp.y+7;
                strcpy(temp.str,"/2+7");
                temp.num=7;
                vec.push_back(temp);


            }
            break;
            case 5:
            {
                temp.y=temp.y*3;
                temp.y=temp.y-5;
                temp.num=temp.num;
                strcpy(temp.str,"*3-5");
                temp.x=i;
                vec.push_back(temp);
                if(temp.y==2012)
                    return i;
                temp = n;
                temp.y=temp.y+7;//-5 *3
                temp.y=temp.y/2;
                strcpy(temp.str,"+7/2");
                temp.x=i;
                temp.num=2;
                vec.push_back(temp);
                temp=n;
                temp.x=i;
                temp.y=temp.y/2;
                temp.y=temp.y+7;
                strcpy(temp.str,"/2+7");
                temp.num=7;
                vec.push_back(temp);
            }
            break;
        }
        i++;
    }
    return -1;
}
int getList(vector<node> &vec,int index)
{
    node n=vec[index];
    if(n.x==0||n.x==1)
    {
        cout<<n.str<<"  "<<endl;
        if(n.x==0)
        return 0;
        else
        return 1;
    }
    int k=getList(vec,n.x);
    cout<<n.str<<"  "<<endl;
    return k;


}
int main()
{
    vector<node> vec;
    node n;
    n.x=0;n.y=2011+7;n.num=7;//初始时选择+7
    vec.push_back(n);
    n.x=0;n.y=2011/2;n.num=2;//或者初始时选择/2
    vec.push_back(n);
    int x=getresult(vec);//广域搜索
    if(x>0)
    {
        cout<<"找到结果路线:"<<endl;
    }
    else
    {
        cout<<"找不到路线:"<<endl;
        return 1;
    }
    x = getList(vec,x);
    if(x==0)
        cout<<"初始选择2011+7 即:2018";
    else
        cout<<"初始选择2011/2 即:1005";
    return 0;
}


执行结果:


即:

最后的结果:2011+7/2+7/2+7-5×3/2+7/2+7×3-5/2+7/2+7-5*3-5*3/2+7-5*3/2+7-5 = 2012,按此步骤走(加减乘除没有优先级),可以走出去。呵呵。现在2:42分。该睡觉了。


http://www.niftyadmin.cn/n/1460570.html

相关文章

Oracle常用监控SQL

1.监控事例的等待&#xff1a; select event,sum(decode(wait_time,0,0,1)) prev, sum(decode(wait_time,0,1,0)) curr,count(*) from v$session_wait group by event order by 4;2.回滚段的争用情况&#xff1a; select name,waits,gets,waits/gets ratio from v$rollstat a,…

远程唤醒windows的shell脚本

cat start-windows.sh #!/bin/bash if [ "$1" windows1 ] then ether-wake -D -i ens192 -b 18:31:BF:DE:97:10 fi if [ "$1" windows2 ] then ether-wake -D -i ens192 -b 0a:e0:af:a5:36:3b fi ./start-windows.sh windows2

车牌图像识别

1.车牌预处理 车牌预处理过程的好坏直接影响到车牌图像进行后期处理过程&#xff0c;比如车牌字符分割等。车牌预处理也是尽可能的消除噪声&#xff0c;减少后期处理带来的不必要的麻烦。 输入的车牌是24Bit的BMP真彩色图像&#xff0c;车牌照有黄底黑字&#xff0c;蓝底白字…

ORACLE锁机制研究

锁概念基础 数据库是一个多用户使用的共享资源。当多个用户并发地存取数据时&#xff0c;在数据库中就会产生多个事务同时存取同一数据的情况。若对并发操作不加控制就可能会读取和存储不正确的数据&#xff0c;破坏数据库的一致性。 加锁是实现数据库并发控制的一个非常重要的…

系统运行时间shell简单脚本

#!/bin/bash DATEdate "%Y-%m-%d" echo "日期是 $DATE" USERSwho | wc -l echo "当前的登陆用户数量是 $USERS个" UPuptime | awk {print $3 $4}| cut -d, -f1 echo "该系统运行了$UP"chmod x yunxing.sh sh yunxing.sh

快速搭建FTP服务的shell脚本

需求&#xff1a;为新装的服务器搭好vsftpd服务 -装包、起服务、设开机自运行 -通过脚本实现上述操作 #cat /root/ftpon.sh #!/bin/bash yum -y install vsftpd &>/dev/null #//装包&#xff0c;忽略输出 service vsftpd restart #//起服务 chkconfig vsftpd…

jquery添加自定义方法

方法一&#xff1a; jQuery.fn.setApDivfunction () { //apDiv浮动层显示位置居中控制 var wheight$(window).height(); var wwidth$(window).width(); var apHeightwheight-$("#apDiv").height(); var apWidthwwidth-$(…

A*算法简介

A*算法简介写这篇文章的初衷是应一个网友的要求&#xff0c;当然我也发现现在有关人工智能的中文站点实在太少&#xff0c;我在这里 抛砖引玉&#xff0c;希望大家都来热心的参与。 还是说正题&#xff0c;我先拿A*算法开刀&#xff0c;是因为A*在游戏中有它很典型的用法&#…