6只青蛙问题 求解算法 请大家赐教

来源:互联网 发布:鸿芈人力 知乎 编辑:程序博客网 时间:2024/04/28 08:43

6只青蛙三只一组, 面对面坐在7个石头上  可能大家很多玩过, 突发奇想用程序能尝试所有方法 然后计算出正确方法

无奈本人功力尚浅, 没解出来 ,各位大侠路过的 方便帮着看看 

青蛙游戏地址http://www.sobyso.com/test/app28



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><style>#s1,#s2,#s3,#s4,#s5,#s6,#s7 { width:80px; height:80px; background:url(l.jpg); }</style></head><body><span id="dump" ></span><table width="600" border="0" cellspacing="0" cellpadding="0">  <tr>    <td id="s1"> </td>    <td id="s2"> </td>    <td id="s3"> </td>    <td id="s4"> </td>    <td id="s5"> </td>    <td id="s6"> </td>    <td id="s7"> </td>     </tr>  <tr>    <td height="50" background="st.jpg"> s1 </td>    <td height="50" background="st.jpg"> s2</td>    <td height="50" background="st.jpg"> s3</td>    <td height="50" background="st.jpg"> s4</td>    <td height="50" background="st.jpg"> s5</td>    <td height="50" background="st.jpg"> s6</td>    <td height="50" background="st.jpg"> s7</td>      </tr></table><script>var s= [0,1,2,3,0,4,5,6];// 1,2,3 向右跳的青蛙, 4,5,6 向左跳的青蛙var str="";var step=0;var test=0;var fback=0; for(m=0;m<7;m++){dojump();alert("step:"+step+",m:"+m);if(step<=m ){//跳回来lasts=str.split(",");last=lasts[lasts.length-2];jumpback(last);step=m+1;break;}}function jumpback(last){w=last%10;w0=parseInt(last/10);if(w==1)w1=w0-1;if(w==2)w1=w0-2;if(w==3)w1=w0+1;if(w==4)w1=w0+2;//alert("do last"+w0+","+w1);s[w0]=s[w1];s[w1]=0;fback=w0+","+w;dis();} function dojump(){for(j=1;j<8;j++)if(jump(j)){step++;dis();return 0;break;}return 1;}function jump(w) //s 位置编号{flag=0; //是否跳成功q=s[w];   //左跳1if(q>3 && s[w-1]==0 && w>1 ) //判断青蛙是否左跳{ flag=1;s[w-1]=s[w];s[w]=0;}//左跳2if(q>3 && s[w-1]!=0 && s[w-2]==0 && w>2 ) //判断青蛙是否左跳{ flag=2;s[w-2]=s[w];s[w]=0;}//右跳1if(q<4 && s[w+1]==0 && w<7 ) //判断青蛙是否左跳{ flag=3;s[w+1]=s[w];s[w]=0;}//右跳2if(q<4 && s[w+1]!=0 && s[w+2]==0 && w<6 ) //判断青蛙是否左跳{ flag=4;s[w+2]=s[w];s[w]=0;}if(flag!=0)str+=w+""+flag+",";//记录跳的方法return flag;}//function dump(str){document.getElementById("dump").innerHTML=str;}function dis(){for(n=1;n<8;n++){if(s[n]<4)document.getElementById("s"+n).style.background="url(r.jpg)";if(s[n]==0)document.getElementById("s"+n).style.background="url( )";if(s[n]>=4)document.getElementById("s"+n).style.background="url(l.jpg)";}}</script></body></html>


原创粉丝点击