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分。该睡觉了。
题目要求的图形如右所示。
代码:
#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分。该睡觉了。
相关文章推荐
- 就是左边是一个FRAME,右边也是一个FRAME,两个之间有分隔线,在分隔线上可以设一个小图标,一点击左边的FRAME就缩进去了,整个屏幕都显示右边的那个FRAME
- 大家看看一道题:编号为123456789的火车经过如下轨道从左边入口处移动右边出口处(每车都必须且只能进临时轨道M一次,且不能再回左边入口处)
- 2011-2012,走过的痕迹(3)
- 左边select内容选择到右边select内容里面的js实现方法(可以多选和全选)
- 2011-2012,走过的痕迹(4)
- 2011最后的几个小时了,迎接2012
- 三叉树解2011年度最变态的迷宫难题
- 走过曲折的2011,面对纠结的2012
- dll加密处理 你DLL要加密,不能让人家可以用Reflector反编译出来,不然什么算法都没有用
- 左边select内容选择到右边select内容里面的js实现方法(可以多选和全选)
- dtree nodeId不能重复,不然树加载不出来,浏览器会死
- eclipse export出来的jar包不能正常工作 ,调试模式下可以正常运行
- 二分查找--查找重复有序数组中最左边最右边的target
- 写一个SQL完成左边的表变成右边的表
- 果园里有一堆苹果,一共n头(n大于1小于9)熊来分,第一头为小东,它把苹果均分n份后,多出了一个,它扔掉了这一个,拿走了自己的一份苹果,接着第二头熊重复这一过程,即先均分n份,扔掉一个然后拿走一份,以此类推直到最后一头熊都是这样(最后一头熊扔掉后可以拿走0个,也算是n份均分)。问最初这堆苹果最少有多少个。
- tyvj 1088 给出长度为N的数列{A_i},每次可以从最左边或者最右边取走一个数,第i次取数得到的价值是i * A_j。求价值之和最大的取数方案。
- 程序员面试题目总结--数组(四)【数列中符合条件数对的个数、数组是否存在重复元素、重新排列数组使数组左边为奇数,右边为偶数、数组中的第二大数、数组中的最小值和最大值】
- JS高级群的日常!写一个从10到0的倒计时,用console.log打印,不可以用 setInterval!本来说好的研究avalonJS最后演变成了看着大神在那边互相比拼实力。。
- 关于文本模式设置为ie7列表不能显示出来,i9以上可以
- 第二百九十八节,python操作redis缓存-Set集合类型,可以理解为不能有重复元素的列表