zoukankan      html  css  js  c++  java
  • 4月12日

    我做过的dfs大致分为两种:(1)回溯(2)图上几个方向进行搜索

    hdu1016(打表+dfs)(回溯)

    题意:一个环里面有m个数,要求两两相加的和为质数,打印出所有排列方案

    分析:这题需要用打表+dfs,40以内的素数先求出,然后在直接dfs回溯

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <string>
     5 #include <vector>
     6 #include <algorithm>
     7 #include <set>
     8 #include <map>
     9 #include <bitset>
    10 #include <cmath>
    11 #include <queue>
    12 #include <stack>
    13 using namespace std;
    14 const int maxn=22;
    15 int a[maxn],vis[maxn];
    16 int prime[]={3,5,7,11,13,17,19,23,29,31,37};  //40以内的素数打表
    17 int n;
    18 bool judge(int k)
    19 {
    20     int flag=0;
    21     for(int i=0;i<11;i++)
    22     {
    23         if(k==prime[i])
    24         {
    25             flag=1; break;
    26         }
    27     }
    28     if(flag)  return true;
    29     else  return false;
    30 }
    31 void dfs(int step)
    32 {
    33     if(step==n&&judge(a[step-1]+a[0]))
    34     {
    35         for(int i=0;i<step-1;i++)
    36             cout<<a[i]<<" ";
    37         cout<<a[step-1]<<endl;
    38     }
    39     else{
    40         for(int i=2;i<=n;i++)
    41         {
    42             if(!vis[i]&&judge(i+a[step-1]))
    43             {
    44                 vis[i]=1;
    45                 a[step]=i;
    46                 dfs(step+1);
    47                 vis[i]=0;   //回溯
    48             }
    49         }
    50     }
    51 }
    52 int main()
    53 {
    54     int cas=0;
    55     while(cin>>n)
    56     {
    57         memset(vis,0,sizeof(vis));
    58         printf("Case %d:
    ",++cas);
    59         a[0]=1;
    60         vis[1]=1;
    61         dfs(1);
    62         cout<<endl;
    63     }
    64     return 0;
    65 }
    View Code

     hdu1312(图上几个方向进行搜索)

    题意:从某个位置开始,统计走过的非#并且相连的有多少

    分析:水题

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <string>
     5 #include <vector>
     6 #include <algorithm>
     7 #include <set>
     8 #include <map>
     9 #include <bitset>
    10 #include <cmath>
    11 #include <queue>
    12 #include <stack>
    13 using namespace std;
    14 const int maxn=30;
    15 char s[maxn][maxn];
    16 int n,m;
    17 int cnt;
    18 int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
    19 void dfs(int x,int y)
    20 {
    21     int nx,ny;
    22     if(x>n||x<0||y>m||y<0) return;
    23     s[x][y]='#';
    24     for(int i=0;i<4;i++)
    25     {
    26         nx=x+dx[i],ny=y+dy[i];
    27         if(nx>=0&&nx<n&&ny>=0&&ny<m&&s[nx][ny]!='#')
    28         {
    29             cnt++;
    30             dfs(nx,ny);
    31         }
    32     }
    33 }
    34 int main()
    35 {
    36     while(cin>>m>>n)
    37     {
    38         if(n+m==0)  break;
    39         for(int i=0;i<n;i++)
    40             cin>>s[i];
    41          cnt=0;
    42         int sx,sy;
    43         for(int i=0;i<n;i++)
    44             for(int j=0;j<m;j++)
    45             {
    46                 if(s[i][j]=='@')
    47                 {
    48                     sx=i,sy=j; break;
    49                 }
    50             }
    51         dfs(sx,sy);
    52         cnt++;
    53         cout<<cnt<<endl;
    54     }
    55     return 0;
    56 }
    View Code
  • 相关阅读:
    [zz]libvirt中CPU和内存的细粒度管理机制
    SAP 模块中文解释
    邪恶的Php一句话木马 assert函数制作简单木马
    PHP开发中三维数组的应用
    返回本机时间或服务器时间
    向SQL中插入数据
    Word的字体
    人生如锅
    打开指定的文件
    计算最大序号
  • 原文地址:https://www.cnblogs.com/wolf940509/p/5382821.html
Copyright © 2011-2022 走看看