您的位置:首页 > 其它

Sicily 1152 简单的马周游问题

2012-01-26 16:19 399 查看
DFS,这道题的棋盘只是5*6,没有剪枝也可以过。

#include <iostream>
using namespace std;
int arr[30];
struct dir {
int x;
int y;
};
struct dir p[8];
bool tab[5][6];
int flag = 0;
void solve( int k, int x, int y ) {
int x1,y1;
if ( flag ==1 )
return;
if ( k==30 ) {
flag=1;
for ( int i = 0; i < 29; i++ )
cout << arr[i] << " ";
cout << arr[29] << endl;
}
else {
//cout << "aaa";
for ( int j=0;j<8;j++ ) {
//  cout << "aaaaa";
x1=x+p[j].x;
y1=y+p[j].y;
//  cout << x1 << endl << y1 << endl;
if ( x1>=0 && x1<5 && y1>=0 && y1<6 && tab[x1][y1]==true ) {
//  cout << "p";
arr[k]=6*x1+y1+1;
tab[x1][y1]=false;
k++;
solve( k,x1,y1 );
tab[x1][y1]=true;
k--;
}
}
}
}

int main()
{
//struct dir p[8];
p[0].x=1;
p[0].y=-2;
p[1].x=2;
p[1].y=-1;
p[2].x=2;
p[2].y=1;
p[3].x=1;
p[3].y=2;
p[4].x=-1;
p[4].y=2;
p[5].x=-2;
p[5].y=1;
p[6].x=-2;
p[6].y=-1;
p[7].x=-1;
p[7].y=-2;
int n,i,k;
int x,y;
//  bool tab[5][6];
cin >> n;
while ( n!=-1 ) {
x=(n-1)/6;
y=(n-1)%6;
for ( i=0;i<5;i++ ) {
for ( k=0;k<6;k++ ) {
tab[i][k]=true;
}
}
flag = 0;
arr[0]=n;
tab[x][y]=false;
k=1;
solve( k,x,y );
cin >> n;
}
// system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: