聪明文档网

聪明文档网

最新最全的文档下载
当前位置: 首页> poj 2488 A Knights JourneyDFS 比较水

poj 2488 A Knights JourneyDFS 比较水

时间:2011-05-22 19:53:12    下载该word文档

晕死 就是一个深搜+回朔 很简单 但是一定要注意方向 一定要按题目中给的方向搜索

题目大意:求一个骑士不重复的把所有棋盘上的点都遍历一遍的路径并输出 如果没有就输出impossible

解题思路:8方向的DFS+回朔 其他的看代码注释

code:

#include

using namespace std;

const int N = 27;

int n, m_x, m_y;

int a_x[8] = { -1, 1, -2, 2, -2, 2, -1, 1 };//方向 非常恶心 一定要注意 是字典顺序

int a_y[8] = { -2, -2, -1, -1, 1, 1, 2, 2 };

bool f[N][N];

struct node{

int x, y;

node( int X = 0, int Y = 0 ){ x = X; y = Y; }

}path[N*N+1];//记录路径用的结构体数组

bool isover( int x, int y )//判断越界

{

if( x >= m_x || x <0 || y >= m_y || y < 0 )

   return 1;

return 0;

}

bool dfs( int dip, int x, int y )

{

if( dip == n )

{

   int j;

   for( j = 0; j找到答案则输出路径

   {

    printf( "%c%d", path[j].y+'A', path[j].x+1 );

   }

   cout << endl;

   return true;

}

int i;

for( i = 0; i<8; i++ )

{

   int tx, ty;

   tx = x + a_x[i];

   ty = y + a_y[i];

   if( !isover(tx, ty) && !f[ty][tx] )

   {

    f[ty][tx] = true;

    path[dip].x = tx;

    path[dip].y = ty;

    if( dfs( dip+1, tx, ty ) )

     return true;

    f[ty][tx] = false;//算是回朔吧

   }

}

return false;

}

int main()

{

int cn, cnt;

scanf( "%d", &cn );

for( cnt = 1; cnt <= cn; cnt++ )

{

   scanf( "%d %d", &m_x, &m_y );//A,1

   n = m_x * m_y;

   memset( f, 0, sizeof f );

  

   printf( "Scenario #%d:\n", cnt);

   f[0][0] = true;//标记第一点 只要从A1开始即可。不需要注意:start at any position of the chessboard.

   path[0].y = 0;

   path[0].x = 0;

   if( !dfs( 1, 0, 0 ) )

    printf( "impossible\n" );

   printf("\n");

}

return 0;

}

二、解题思路

使用回溯法进行深度优搜索。

A1开始搜索,如果能找到一个路径则立即返回,如果从A1出发搜索所有的路径都不能找到一个路径则输出impossible

注意几个问题:

1、是8个可能的搜索方向的顺序问题。网友贴出来一个顺序是dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};

2、不要忘了输出空行。

三、代码

 

#include<iostream>

using namespace std;

struct Position

{

    int row;

    int col;

};

int Curr;//遍历个数



int Sum;//方格总数



int Row,Col;//棋盘行数和列数



Position path[50];//保存路径



bool V[50][50];//是否已经遍历



int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};

bool DFS(int r,int c )

{

    path[Curr].row=r;

    path[Curr].col=c;

    Curr++;

    if(Curr>Sum)

        return true;

    int R,C;

    for(int i=0;i<8;++i)

    {

        R=r+dir[i][0];

        C=c+dir[i][1];

        if(R>=1 && R<=Col && C>=1 && C<=Row && (V[R][C]==false))

        {    

            V[r][c]=true;

            if(DFS(R,C))

                return true;

            V[R][C]=false;

            Curr--;

        }

    }

    return false;

}

int main()

{

    int n;

    int i;

    //cout<



    cin>>n;

    for(i=1;i<=n;++i)

    {

        cin>>Row>>Col;

        memset(V,0,sizeof(bool)*50*50);

        Sum=Row*Col;

        Curr=1;

        

        cout<<"Scenario #"<<i<<":"<<endl;

        if(DFS(1,1))

        {

            for(int j=1;j<=Sum;j++)

            {

                cout<<(char)(path[j].row+64)<<path[j].col;

            }

            cout<<endl;

        }

        else

            cout<<"impossible"<<endl;

        cout<<endl;

    }

    return 0;

}

这是一道深搜回溯的题目。我这个新手初次做深搜,浏览了无数大牛的解题报告,收获很大。我原来总以为是要用栈来存储结点,却没想到回溯如此简单。

 1 #include

 2 #include

 3 int n;

 4 int r,c;

 5 int ok;

 6 int visit[100][100];

 7 int px[50],py[50];

 8 int yy[8] = {-1,1,-2,2,-2,2,-1,1};//在这就把输出字典顺序确定了

 9 int xx[8] = {-2,-2,-1,-1,1,1,2,2};

10 

11 int isok(int x,int y)

12 {

13     return x >= 1 && x <= r && y >= 1 && y <= c;

14 }

15 

16 void dfs(int x,int y,int length);

17 int main()

18 {

19     scanf("%d",&n);

20     for(int i =1;i <= n;i++){

21         scanf("%d%d",&c,&r);

22         visit[1][1] = 1;

23         ok = 0;

24         dfs(1,1,1);

25         printf("Scenario #%d:\n",i);

26         if(ok){

27             for(int j = 1;j <= r*c;j++){

28                 printf("%c",px[j]+'A'-1);

29                 printf("%d",py[j]);

30             }

31         }

32         else printf("impossible");

33         printf("\n\n");

34     }

35     system("pause");

36     return 0;

37         

38 }

39 void dfs(int x,int y,int length)

40 {

41     px[length] = x;

42     py[length] = y;

43     if(length == r*c){

44         ok = 1;

45         return;

46     }

47     for(int i = 0;i < 8;i++){

48         int tx = x + xx[i];

49         int ty = y + yy[i];

50         if(isok(tx,ty) && !visit[tx][ty] && !ok){

51             visit[tx][ty] = 1;

52             dfs(tx,ty,length+1);

53             visit[tx][ty] = 0;//巧妙,也是必须的

54         }

55     }

56 }

57 

免费下载 Word文档免费下载: poj 2488 A Knights JourneyDFS 比较水

  • 29.8

    ¥45 每天只需1.0元
    1个月 推荐
  • 9.9

    ¥15
    1天
  • 59.8

    ¥90
    3个月

选择支付方式

  • 微信付款
郑重提醒:支付后,系统自动为您完成注册

请使用微信扫码支付(元)

订单号:
支付后,系统自动为您完成注册
遇到问题请联系 在线客服

常用手机号:
用于找回密码
图片验证码:
看不清?点击更换
短信验证码:
新密码:
 
绑定后可用手机号登录
请不要关闭本页面,支付完成后请点击【支付完成】按钮
遇到问题请联系 在线客服