马踏棋盘

来源:互联网 发布:怎样在淘宝注册店铺 编辑:程序博客网 时间:2024/04/30 16:32
<a target=_blank target="_blank" href="http://www.doudou.in/play/27-mtqp/?code=011affb6cfb69a633033d1add3f55e8j&isappinstalled=0&state=STATE&from=timeline#rd" style="text-decoration: none; color: rgb(34, 161, 219); overflow: hidden; font-family: 'Microsoft Yahei', Tahoma; line-height: 18px; white-space: nowrap;"><span style="font-family:punctuation, Hiragino Sans GB, Helvetica Neue, 微软雅黑, Tohoma;color:#5d7895;"><span style="overflow: hidden; line-height: 22px; white-space: nowrap; cursor: pointer; font-size: 14px;">马踏棋盘-豆豆游戏</span></span><span style="text-decoration: none; color: rgb(51, 51, 51); overflow: hidden; font-family: punctuation, 'Hiragino Sans GB', 'Helvetica Neue', 微软雅黑, Tohoma; line-height: 22px; white-space: nowrap; font-size: 14px;"> </span></a>,此游戏是在浏览QQ空间,见到一位好友转载的,号称无人能走完,怀着疑问的心态,打开游戏连接,的确,第一把游戏7步Gameover,不满失败的挫折,第二把30步以失败告终,由于本人是计算机专业,就尝试用C++进行编码,耗时一个多小时,编写算法时也特别苦恼,苦苦不出结果,也存在一些小错误,在室友的悉心劝导下,再接再厉,终于运行成功,奈何算法出现一些小问题,运行耗时比较长,和室友认真研究了一下算法,分析出算法的一些毛病,进行改进,最终调试出结果,但结果却显示如图1,有36步,按照运行结果在游戏中却只能运行34步,<img src="http://img.blog.csdn.net/20150514192148962?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FpbmlhbzE3MTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
又想一会,原来如此简单,太粗心了,少添加了一句语句<span style="white-space:pre"></span>visit[x][y] = true;,这种错误也不是一次两次了,添加上重新运行
<img src="http://img.blog.csdn.net/20150514192524121?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FpbmlhbzE3MTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
没错,运行结果只能是35步,因为马自己开始还占着一格,按照运行结果,重新玩了一局,哈哈<img src="http://img.blog.csdn.net/20150514192247637?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FpbmlhbzE3MTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
经过调试,感觉还行,现在将源代码附上<pre name="code" class="cpp">#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <string>#include <math.h>#include <algorithm>using namespace std;const int maxn = 10+10;typedef struct aa{int x, y;}Node;bool visit[maxn][maxn];Node pre_temp[maxn][maxn];Node pre[maxn][maxn];Node temp[maxn];int maxnum = -1;void inital(){for(int i = 0; i < maxn; i++){for(int j = 0; j < maxn; j++){visit[i][j] = false;pre_temp[i][j].x = -1; pre_temp[i][j].y = -1;}}temp[1].x = -1; temp[1].y = -2;temp[2].x = -2; temp[2].y = -1;temp[3].x = -2; temp[3].y = 1;temp[4].x = -1; temp[4].y = 2;temp[5].x = 1; temp[5].y = -2;temp[6].x = 2; temp[6].y = -1;temp[7].x = 2; temp[7].y = 1;temp[8].x = 1; temp[8].y = 2;}int jump_next(int x, int y){int count = 0;int temp_x = 0, temp_y = 0;for(int k = 1; k <= 8; k++){temp_x = x+temp[k].x; temp_y = y+temp[k].y;if(temp_x<1 ||(temp_x>6))continue;if(temp_y<1 ||(temp_y>6))continue;if(visit[temp_x][temp_y])continue;count = count+1;}return count;}void dfs(int x, int y, int cur_step){int minnum = 9, tempLocation = 0;int temp_x = x, temp_y = y;for(int k = 1; k <= 8; k++){temp_x = x+temp[k].x; temp_y = y+temp[k].y;if(temp_x<1 ||(temp_x>6))continue;if(temp_y<1 ||(temp_y>6))continue;if(visit[temp_x][temp_y])continue;int tempnum = jump_next(temp_x, temp_y);if(tempnum < minnum){minnum = tempnum;tempLocation = k;}}if(tempLocation != 0){temp_x = x+temp[tempLocation].x; temp_y = y+temp[tempLocation].y;visit[temp_x][temp_y] = true;pre_temp[x][y].x = temp_x; pre_temp[x][y].y = temp_y;dfs(temp_x,temp_y, cur_step+1);visit[temp_x][temp_y] = false;pre_temp[x][y].x = pre_temp[x][y].y = -1;}else {if(cur_step > maxnum){maxnum = cur_step;for(int i = 1; i < 7; i++){for(int j = 1; j < 7; j++){pre[i][j].x = pre_temp[i][j].x;pre[i][j].y = pre_temp[i][j].y;}}}}}void print(int x, int y){int temp_x=0, temp_y=0;for(; pre[x][y].x !=-1 ||pre[x][y].y !=-1; ){printf("(%d %d)->",x,y);temp_x = pre[x][y].x; temp_y = pre[x][y].y;x = temp_x; y = temp_y;}}int main(){int x = 0, y = 0;inital();while(scanf("%d %d",&x,&y) != EOF){visit[x][y] = true;dfs(x,y,0);printf("%d\n",maxnum);print(x,y);}return 0;}

                                             
0 0