我的代码:如果不对,欢迎指正、、、
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
int dir[4][2] = {-1,0,1,0,0,-1,0,1};
int vis[7][4],flag=0,xx;
int dfs(int x,int y,int step)
{
int sx,sy;
if(x==6 && y==1 && step==xx)
{ return 1; }
for(int i=0; i<4; i++)
{
sx=x+dir[i][0]; sy=y+dir[i][1];
if(sx>=1 && sx<=6 && sy>=1 && sy<=3 && vis[sx][sy]==0)
{
vis[sx][sy]=1;
if ( dfs(sx,sy,step+1) )
return 1 ;
vis[sx][sy]=0;
}
}
return 0;
}
int main()
{
cout<<"如果走 XX 步不重复的话是否能到达??"<<": "<<endl;
for(int i=1; i<=18; i++)
{xx=i;
memset(vis,0,sizeof(vis));
vis[6][3]=1;
int k=dfs(6,3,1);
cout<<"当xx= "<<xx<<" : ";
if(k==1)
printf("YES \n");
else printf("NO \n");
}
return 0;
}
运行结果: