zoukankan      html  css  js  c++  java
  • Union Find and search

    1.POJ2488  A Knight's Journey

    search

     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<string>
     5 using namespace std;
     6 
     7 #define N 27
     8 #define INF 0x7FFFFFFF
     9 int p,q,_count=0;
    10 int _dec[N][N];
    11 int routine[2*N];
    12 int t1,t2;
    13 int flag,flag2;
    14 
    15 void path(int x,int y,int k)
    16 {
    17     switch (k)
    18     {
    19     case 0:{t1 = x-1;t2 = y-2;break;}
    20     case 1:{t1 = x+1;t2 = y-2;break;}
    21     case 2:{t1 = x-2;t2 = y-1;break;}
    22     case 3:{t1 = x+2;t2 = y-1;break;}
    23     case 4:{t1 = x-2;t2 = y+1;break;}
    24     case 5:{t1 = x+2;t2 = y+1;break;}
    25     case 6:{t1 = x-1;t2 = y+2;break;}
    26     case 7:{t1 = x+1;t2 = y+2;break;}
    27     }
    28 }
    29 void OUTPUT()
    30 {
    31     for (int i=0;i<_count;i++)
    32     {
    33         cout<<(char)('A'+routine[2*i+1])<<routine[2*i]+1;
    34         if (i==_count-1) cout<<endl;
    35     }
    36 }
    37 bool DFS(int x,int y)
    38 {
    39     if (flag2 == 1) return 0;
    40     _dec[x][y]=1;
    41     routine[2*_count]=x;
    42     routine[2*_count+1]=y;
    43     _count++;
    44     if (_count>p*q)
    45     {
    46         flag2 = 1;
    47         return 0;
    48     }
    49     if ((_count == p*q)&&(flag == 0))
    50     {
    51         OUTPUT();
    52         flag = 1;
    53         return 1;
    54     }
    55     for (int k=0;k<8;k++)
    56     {
    57         path(x,y,k);
    58         int xx=t1,yy = t2;
    59         if ((xx>=0)&&(xx<p)&&(yy>=0)&&(yy<q)&&(_dec[xx][yy]==0))    
    60             if (DFS(xx,yy)) return 1;
    61     }
    62     _dec[x][y]=0;
    63     _count--;
    64     return 0;
    65 }
    66 int main()
    67 {
    68     int n;
    69     cin>>n;
    70     for (int m=1;m<=n;m++)
    71     {
    72         for (int i=0;i<N;i++)
    73             for (int j=0;j<N;j++)
    74                 _dec[i][j]=0;
    75         memset(routine,0,sizeof(routine));
    76         cin>>p>>q;
    77         cout<<"Scenario #"<<m<<":"<<endl;
    78         if ((p==1)&&(q==1))
    79         {
    80             cout<<"A1"<<endl<<endl;
    81             continue;
    82         }
    83         if (p*q>26 || p>=9 || q>=9 || p<=2 || q<=2)
    84         {
    85             cout<<"impossible"<<endl<<endl;
    86             continue;
    87         }
    88         _count = 0;
    89         flag = 0;
    90         flag2 = 0;
    91         DFS(0,0);
    92         if ((flag2 == 1)||(flag==0)) cout<<"impossible"<<endl;
    93         cout<<endl;
    94     }
    95     return 0;
    96 }
    View Code

     2.POJ 1077 Eight

    http://blog.csdn.net/whyorwhnt/article/details/10555989

  • 相关阅读:
    SpringBoot第五篇:整合Mybatis
    SpringBoot第四篇:整合JDBCTemplate
    SpringBoot第三篇:配置文件详解二
    分享一篇去年的项目总结
    Oracle生成多表触发器sql
    Oracle 设置用户密码永不过期
    Oracle建表提示SQL 错误: ORA-00904: : 标识符无效
    MySql数据备份
    ETL全量多表同步简述
    ETL全量单表同步简述
  • 原文地址:https://www.cnblogs.com/giddens/p/4469029.html
Copyright © 2011-2022 走看看