zoukankan      html  css  js  c++  java
  • dfs整理:

    1,棋盘问题:poj1321

    思路:显然,是一个深度搜索问题,对列进行标记一下,题中显示要同一行,同一列不能有两个,那么标记之后,就从下一行开始搜索,然后就一直递归下去;

    code:

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<queue>
     6 #include<vector>
     7 #include<map>
     8 #include<math.h>
     9 using namespace std;
    10 typedef long long ll;
    11 const int mod=1e9+7;
    12 const int PI=acos(-1.0);
    13 const int MAXN=1e5+7;
    14 char a[10][10];
    15 int vis[10];
    16 int n,k;
    17 int sum;
    18 void DFS(int x,int y)
    19 {
    20    if(y==k)
    21       sum++;
    22    else
    23    {
    24    for(int i=x; i<n; i++)
    25    {
    26       for(int j=0; j<n; j++)
    27       {
    28          if(a[i][j]=='#'&&!vis[j])
    29          {
    30             vis[j]=1;
    31             DFS(i+1,y+1);
    32             vis[j]=0;
    33          }
    34       }
    35    }
    36    }
    37 }
    38 int main()
    39 {
    40    while(cin>>n>>k)
    41    {
    42       memset(vis,0,sizeof(vis));
    43       sum=0;
    44       if(n==-1&&k==-1)
    45       {
    46          break;
    47       }
    48       for(int i=0; i<n; i++)
    49          for(int j=0; j<n; j++)
    50            cin>>a[i][j];
    51       DFS(0,0);
    52       cout<<sum<<endl;
    53    }
    54    //system("pause");
    55 }
    View Code

    2,Dungeon Master poj2251

    思路:广度搜索裸题,判断一下方向和边界搜索即可,

    code:

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<queue>
     6 #include<vector>
     7 #include<map>
     8 #include<math.h>
     9 using namespace std;
    10 typedef long long ll;
    11 const int mod=1e9+7;
    12 const int PI=acos(-1.0);
    13 const int MAXN=1e5+7;
    14 int dir[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
    15 int vis[40][40][40];
    16 char a[40][40][40];
    17 int x,y,z;
    18 struct node{
    19    int x,y,z;
    20    int step;
    21 }t1,t2;
    22 queue<node> q;
    23 int sx,sy,sz;
    24 int ex,ey,ez;
    25 int main()
    26 {
    27    while(cin>>x>>y>>z)
    28    {
    29       memset(vis,0,sizeof(vis));
    30       if(!x&&!y&&!z)
    31        break;
    32       for(int i=0; i<x; i++)
    33       {
    34          for(int j=0; j<y; j++)
    35          {
    36             for(int k=0; k<z; k++)
    37             {
    38                cin>>a[i][j][k];
    39                if(a[i][j][k]=='S') sx=i,sy=j,sz=k;
    40                if(a[i][j][k]=='E') ex=i,ey=j,ez=k;
    41             }
    42          }
    43       }
    44       int flag=0,step;
    45       while(!q.empty())
    46         q.pop();
    47       //t1.x=sx,t1.y=sy,t1.z=sz,t1.step=0;
    48       //vis[t1.x][t1.y][t1.z]=1;
    49       vis[sx][sy][sz]=1;
    50       q.push({sx,sy,sz,0});
    51       while(!q.empty())
    52       {
    53          t1=q.front();
    54          q.pop();
    55          for(int i=0; i<6; i++)
    56          {
    57             t2.x=t1.x+dir[i][0];
    58             t2.y=t1.y+dir[i][1];
    59             t2.z=t1.z+dir[i][2];
    60             if(t2.x >=0 && t2.x < x && t2.y >= 0 && t2.y < y&& t2.z >= 0 && t2.z < z && !vis[t2.x][t2.y][t2.z] && a[t2.x][t2.y][t2.z] != '#')
    61               {
    62                  vis[t2.x][t2.y][t2.z]=1;
    63                  t2.step=t1.step+1;
    64                  q.push(t2);
    65               }
    66          }
    67          if(vis[ex][ey][ez])
    68          {
    69             flag=1;
    70             step=t2.step;
    71             break;
    72          }
    73       }
    74       if(flag)
    75        cout<<"Escaped in "<<step<<" minute(s)."<<endl;
    76      else
    77       cout<<"Trapped!"<<endl;
    78    }
    79 }
    View Code

    3,Catch That Cow poj3278

    思路:简单BFS,搜索一下;

    code:

      1 #include<cstdio>
      2 #include<algorithm>
      3 #include<iostream>
      4 #include<cstring>
      5 #include<queue>
      6 #include<vector>
      7 #include<map>
      8 #include<math.h>
      9 using namespace std;
     10 typedef long long ll;
     11 const int mod=1e9+7;
     12 const int PI=acos(-1.0);
     13 const int maxn=100010;
     14 //int a[maxn];
     15 int vis[maxn];
     16 int n,k;
     17 struct step
     18 {
     19     int x;
     20     int b;
     21     step (int xx,int s): x(xx),b(s){ }
     22 };
     23 queue<step> q;
     24 int main()
     25 {
     26     cin>>n>>k;
     27     memset(vis,0,sizeof(vis));
     28     q.push(step(n,0));
     29     vis[n]=1;
     30     while(!q.empty())
     31     {
     32         step s=q.front();
     33         if(s.x==k)
     34           {
     35           cout<<s.b<<endl;
     36           break;
     37     }
     38         else
     39          {
     40              if(s.x-1>=0&&!vis[s.x-1])
     41                {
     42                    q.push(step(s.x-1,s.b+1));
     43                    vis[s.x-1]=1;
     44                }
     45             if(s.x+1<maxn&&!vis[s.x+1])
     46             {
     47                 q.push(step(s.x+1,s.b+1));
     48                 vis[s.x+1]=1;
     49             }
     50             if(s.x*2<maxn&&!vis[s.x*2])
     51             {
     52                 q.push(step(s.x*2,s.b+1));
     53             }
     54             q.pop();
     55          }
     56     }    
     57  }
     58 另:
     59 #include<cstdio>
     60 #include<algorithm>
     61 #include<iostream>
     62 #include<cstring>
     63 #include<queue>
     64 #include<vector>
     65 #include<map>
     66 #include<math.h>
     67 using namespace std;
     68 typedef long long ll;
     69 const int mod=1e9+7;
     70 const int PI=acos(-1.0);
     71 const int maxn=100010;
     72 //int a[maxn];
     73 int step[maxn],vis[maxn];
     74 queue<int> q;
     75 int bfs(int n,int k){
     76     int now,next;
     77     step[n]=0;
     78     vis[n]=1;
     79     q.push(n);
     80     while(!q.empty()){
     81         now=q.front();
     82         q.pop();
     83         for(int i=0;i<3;i++){
     84             if(i==0) next=now-1;
     85             else if(i==1) next=now+1;
     86             else if(i==2) next=now*2;
     87             if(next<0||next>maxn)
     88               continue;//剪枝 
     89             if(!vis[next])
     90             {
     91                 vis[next]=1;
     92                 q.push(next);
     93                 step[next]=step[now]+1; 
     94             }
     95             if(next==k) 
     96              return step[next];
     97         }
     98     }
     99 }
    100 int main(){
    101     int n,k;
    102     scanf("%d%d",&n,&k);
    103     if(n>=k)
    104      printf("%d
    ",n-k);  //直接往回走 
    105     else 
    106       printf("%d
    ",bfs(n,k));//搜索,找最短路径 
    107     return 0;
    108 } 
    View Code
  • 相关阅读:
    js函数对象
    jQuery选择器
    js数组
    js知识点
    正则|数字|Format
    Ajax基础
    MVC 打包压缩
    JS(正则|JSON)
    CLR via C#
    Exists/In/Any/All/Contains操作符
  • 原文地址:https://www.cnblogs.com/sweetlittlebaby/p/12805395.html
Copyright © 2011-2022 走看看