您的位置:首页 > 其它

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

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

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


代码:

#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分。该睡觉了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐