zoukankan      html  css  js  c++  java
  • poj 2488 A Knight's Journey(dfs)

    题目:http://poj.org/problem?id=2488

    没什么难度,,,就是字典序,要注意一下,一开始没看见。。。唉。。。

    View Code
     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4 int a[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
     5 char str[2500];
     6 int are;
     7 int flag;
     8 int m,n;
     9 int map[27][27];
    10 void dfs(int i,int j,int depth)
    11 {
    12     int t;
    13     int x,y;
    14     if(depth==are)
    15     {
    16         for(int i=0;i<2*depth;i++)
    17         {
    18             cout<<str[i];
    19         }
    20         cout<<endl<<endl;
    21         flag=1;
    22         return ;
    23     }
    24     for(t=0;t<8&&flag==0;t++)
    25     {
    26         x=i+a[t][0];
    27         y=j+a[t][1];
    28         if(x>=1&&x<=n&&y>=1&&y<=m&&map[x][y]==0)
    29         {
    30             map[x][y]=1;
    31             str[depth*2]=x+'A'-1;
    32             str[depth*2+1]=y+'0';
    33             dfs(x,y,depth+1);
    34             map[x][y]=0;
    35         }
    36     }
    37     return ;
    38 }
    39 int main()
    40 {
    41     int t;
    42     int k=0;
    43     cin>>t;
    44     while(t--)
    45     {
    46         k++;
    47         cin>>m>>n;
    48         cout<<"Scenario #"<<k<<":"<<endl;
    49         flag=0;
    50         memset(map,0,sizeof(map));
    51         flag=0;
    52         are=m*n;
    53         map[1][1]=1;
    54         str[0]='A';
    55         str[1]='1';
    56         dfs(1,1,1);
    57 
    58         if(!flag)
    59             cout<<"impossible"<<endl<<endl;
    60     }
    61 }
  • 相关阅读:
    SpringBoot整合阿里云OSS
    UVALive
    HDU 5794 A Simple Chess dp+Lucas
    数论
    UVALive
    UVALive
    HDU 5792 World is Exploding 树状数组+枚举
    UVALive
    UVALive
    UVALive
  • 原文地址:https://www.cnblogs.com/wanglin2011/p/2872082.html
Copyright © 2011-2022 走看看