poj2488 A Knight's Journey 简单DFS 注意搜索步骤

来源:互联网 发布:和还原精灵类似软件 编辑:程序博客网 时间:2024/05/17 04:43
  这一题是比较简单的搜索题,但是题目要求走法是按照字典序的,这样要求搜索时注意顺序。
///2014.3.10///poj2488//16MS/** *简单的DFS,但是题目有个坑人的地方,就是字典序 *因为要求字典序,在搜索时要注意搜索步骤 *在搜索步骤上浪费了两个小时啊!!! */#include <iostream>#include <cstdio>using namespace std;int p,q;int coordinate_x[30]; ///记录每一步的坐标int coordinate_y[30];bool findit;          ///标记有没有找到走法bool step[30][30];    ///标记每一个位置有没有走过void dfs(int x,int y,int deep){    if( findit )        return;    if( deep==p*q ){        findit = true;        coordinate_x[deep] = x;        coordinate_y[deep] = y;        return;    }    step[x][y] = true;    coordinate_x[deep] = x;    coordinate_y[deep] = y;    if( x-1>=1 && x-1<=p && y-2>=1 && y-2<=q && !step[x-1][y-2] ){        dfs(x-1,y-2,deep+1);        step[x-1][y-2] = false;    }    if( x+1>=1 && x+1<=p && y-2>=1 && y-2<=q && !step[x+1][y-2] ){        dfs(x+1,y-2,deep+1);        step[x+1][y-2] = false;    }    if( x-2>=1 && x-2<=p && y-1>=1 && y-1<=q && !step[x-2][y-1] ){        dfs(x-2,y-1,deep+1);        step[x-2][y-1] = false;    }    if( x+2>=1 && x+2<=p && y-1>=1 && y-1<=q && !step[x+2][y-1] ){        dfs(x+2,y-1,deep+1);        step[x+2][y-1] = false;    }    if( x-2>=1 && x-2<=p && y+1>=1 && y+1<=q && !step[x-2][y+1] ){        dfs(x-2,y+1,deep+1);        step[x-2][y+1] = false;    }    if( x+2>=1 && x+2<=p && y+1>=1 && y+1<=q && !step[x+2][y+1] ){        dfs(x+2,y+1,deep+1);        step[x+2][y+1] = false;    }    if( x-1>=1 && x-1<=p && y+2>=1 && y+2<=q && !step[x-1][y+2] ){        dfs(x-1,y+2,deep+1);        step[x-1][y+2] = false;    }    if( x+1>=1 && x+1<=p && y+2>=1 && y+2<=q && !step[x+1][y+2] ){        dfs(x+1,y+2,deep+1);        step[x+1][y+2] = false;    }}int main(){//    freopen("in","r",stdin);//    freopen("out","w",stdout);    int n;    int cas = 1;    scanf("%d",&n);    while( n-- ){        for(int i=0 ; i<27 ; i++)            for(int j=0 ; j<27 ; j++)                step[i][j] = false;        findit = false;        scanf("%d%d",&p,&q);        dfs(1,1,1);        if( findit ){            printf("Scenario #%d:\n",cas);            for(int i=1 ; i<=p*q ; i++)                printf("%c%d",'A'+coordinate_y[i]-1,coordinate_x[i]);            printf("\n\n");        }        else            printf("Scenario #%d:\nimpossible\n\n",cas);        cas++;    }    return 0;}

  在discuss中看到一个打表的代码,我怎么没想到呢~贴下来供以后启发思维

#include <iostream>using namespace std;int main(){    freopen("in.txt","r",stdin);    int i,n,p,q;    cin>>n;    for(i=1;i<=n;i++)    {        cin>>p>>q;        cout<<"Scenario #"<<i<<":"<<endl;        if(p==1 &&q ==1)            cout<<"A1"<<endl;        else if(p==3 && q==4)            cout<<"A1C2A3B1D2B3C1A2C3D1B2D3"<<endl;        else if(p==3 && q==7)            cout<<"A1B3D2F1G3E2G1F3E1G2E3C2A3B1C3A2C1D3B2D1F2"<<endl;        else if(p==3 && q==8)            cout<<"A1B3C1A2C3D1B2D3E1G2E3C2A3B1D2F1H2F3G1E2G3H1F2H3"<<endl;        else if(p==4 && q==3)            cout<<"A1B3C1A2B4C2A3B1C3A4B2C4"<<endl;        else if(p==4 && q==5)            cout<<"A1B3C1A2B4D3E1C2D4E2C3A4B2D1E3C4A3B1D2E4"<<endl;        else if(p==4 && q==6)            cout<<"A1B3C1A2B4C2D4E2F4D3E1F3D2B1A3C4B2A4C3E4F2D1E3F1"<<endl;        else if(p==5 && q==4)            cout<<"A1B3A5C4D2B1A3B5D4C2B4A2C1D3C5A4B2D1C3D5"<<endl;        else if(p==5 && q==5)            cout<<"A1B3A5C4A3B1D2E4C5A4B2D1C3B5D4E2C1A2B4D5E3C2E1D3E5"<<endl;        else if(p==6 && q==4)            cout<<"A1B3A5C6D4B5D6C4D2B1A3C2B4A2C1D3B2D1C3D5B6A4C5A6"<<endl;        else if(p==7 && q==3)            cout<<"A1B3C1A2C3B1A3C2B4A6C7B5A7C6A5B7C5A4B2C4B6"<<endl;        else if(p==7 && q==4)            cout<<"A1B3A5B7D6B5A7C6D4C2A3B1D2C4B2A4B6D7C5A6C7D5B4D3C1A2C3D1"<<endl;        else if(p==8 && q==3)            cout<<"A1B3C1A2B4C2A3B1C3A4B2C4A5B7C5A6B8C6A7B5C7A8B6C8"<<endl;        else            cout<<"impossible"<<endl;        cout<<endl;    }    return 0;}


0 0