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

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

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


代码:

#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分。该睡觉了。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值