`
phinecos
  • 浏览: 341426 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

简单搜索题:马的走法

 
阅读更多

一个4×5的棋盘,输入马的起始坐标,求马能返回初始位置的所有不同走法的总数(马走过的位置不能重复,马走字)。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->#include<iostream>
usingnamespacestd;

constintROWS=4;//行数
constintCOLUMS=5;//列数
intchess[ROWS][COLUMS];//棋盘
intnumCount=0;
intposX,posY;
intdirection[2][8]={{-1,-1,-2,-2,2,2,1,1},{-2,2,1,-1,1,-1,2,-2}};//马走"日"字

voidSolve(intx,inty)
{
inti,j,desX,desY;
for(i=0;i<8;++i)
{
desX
=x+direction[0][i];//目标位置x坐标
desY=y+direction[1][i];//目标位置y坐标
if(desX>=0&&desX<4&&desY>=0&&desY<5&&chess[desX][desY]==0)
{
//满足规则,走到目标处,并继续搜索
chess[desX][desY]=1;
Solve(desX,desY);
chess[desX][desY]
=0;
}
elseif(desX==posX&&desY==posY)
{
//回到了起点
numCount++;
}
}
}
intmain()
{
cin
>>posX>>posY;
memset(chess,
0,sizeof(chess));
numCount
=0;//走法数
chess[posX][posY]=1;//起始步
Solve(posX,posY);//开始搜索
cout<<numCount<<endl;
system(
"pause");
return0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics