zoukankan      html  css  js  c++  java
  • 基础深搜小结

    目前看来,简单深搜题大致分为三类题型:

    1是连通块问题,求连通块大小和数量。

    2是棋盘问题,问地图内放几个坐标,有几个放法。

    3是输出路径问题。

    1.这个问题的经典例题是计算水塘(pku-2386 lake counting)

    例题:

    Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors. 

    Given a diagram of Farmer John's field, determine how many ponds he has.

    Input

    * Line 1: Two space-separated integers: N and M 

    * Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

    Output

    * Line 1: The number of ponds in Farmer John's field.

    Sample Input

    10 12
    W........WW.
    .WWW.....WWW
    ....WW...WW.
    .........WW.
    .........W..
    ..W......W..
    .W.W.....WW.
    W.W.W.....W.
    .W.W......W.
    ..W.......W.

    Sample Output

    3

    Hint

    OUTPUT DETAILS: 

    There are three ponds: one in the upper left, one in the lower left,and one along the right side.
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    const int M = 111;
    char map[M][M];
    int d1[M]={1,1, 1,-1,-1,-1,0, 0};
    int d2[M]={0,1,-1, 0,-1, 1,1,-1};
    int m,n;
    void dfs(int x,int y){
    	map[x][y]='.';
    	for(int i=0;i<=7;i++){
    		int tx=x+d1[i];
    		int ty=y+d2[i];
    		if(tx>=0&&tx<=m&&tx>=0&&ty<=n&&map[tx][ty]=='W'){
    			dfs(tx,ty);
    		}
    	}
    	return ;
    }
    int main(){
    	while(cin>>m>>n){
    		int res=0;
    		for(int i=1;i<=m;i++){
    			for(int j=1;j<=n;j++){
    				cin>>map[i][j];
    			}
    		}
    		for(int i=1;i<=m;i++){
    			for(int j=1;j<=n;j++){
    				if(map[i][j]=='W'){
    				dfs(i,j); res++;
    			}
    			}
    		}
    		cout<<res<<endl;
    	}
    	return 0;
    }
    

      这题的思路就是在主函数写个for循环,先找到满足条件的点,以此为起点,对所有接下来的点进行深搜,搜索过的点

    要记得计数和标记(因为只是计数,不是记录可能性,所以不需要回溯)。

     2.棋盘类问题一般都是用几个数组记录条件,每走一遍就判断数组中是否已经标记过这行或这列。 

    例题:   

    在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

    Input

    输入含有多组测试数据。 
    每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n 
    当为-1 -1时表示输入结束。 
    随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。 

    Output

    对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

    Sample Input

    2 1
    #.
    .#
    4 4
    ...#
    ..#.
    .#..
    #...
    -1 -1
    

    Sample Output

    2
    1
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    const int M = 11;
    char map[M][M];
    int vis[M];
    int res=0,flag=0;
    int n,k;
    void dfs(int row){
        if(flag==k) {res++; return ;}
        if(row>n) return;
        for(int i=1;i<=n;i++){
            if(map[row][i]=='#'&&!vis[i]){
                vis[i]=1;
                flag++;
                dfs(row+1);
                vis[i]=0;
                flag--;
            }
        }
        dfs(row+1);
    }
    int main(){
        while(cin>>n>>k){
            memset(vis,0,sizeof(vis));
            if(n==-1&&k==-1) break;
            res=0;flag=0;
            for(int i=1;i<=n;i++){
                for(int k=1;k<=n;k++){
                    cin>>map[i][k];
                }
            }
            dfs(1);  //从第一行开始
            cout<<res<<endl;
        }
        return 0;
    }
    

      3.简单的路径输出其实就是每深搜一步,就用数组记录一下当前值,判断搜到最后一步后,将数组遍历输出即可。

    例题:hdu-2181 哈密顿绕行世界问题

    一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市。 

    Input前20行的第i行有3个数,表示与第i个城市相邻的3个城市.第20行以后每行有1个数m,m<=20,m>=1.m=0退出. 
    Output输出从第m个城市出发经过每个城市1次又回到m的所有路线,如有多条路线,按字典序输出,每行1条路线.每行首先输出是第几条路线.然后个一个: 后列出经过的城市.参看Sample output 
    Sample Input

    2 5 20
    1 3 12
    2 4 10
    3 5 8
    1 4 6
    5 7 19
    6 8 17
    4 7 9
    8 10 16
    3 9 11
    10 12 15
    2 11 13
    12 14 20
    13 15 18
    11 14 16
    9 15 17
    7 16 18
    14 17 19
    6 18 20
    1 13 19
    5
    0

    Sample Output

    1:  5 1 2 3 4 8 7 17 18 14 15 16 9 10 11 12 13 20 19 6 5
    2:  5 1 2 3 4 8 9 10 11 12 13 20 19 18 14 15 16 17 7 6 5
    3:  5 1 2 3 10 9 16 17 18 14 15 11 12 13 20 19 6 7 8 4 5
    4:  5 1 2 3 10 11 12 13 20 19 6 7 17 18 14 15 16 9 8 4 5
    5:  5 1 2 12 11 10 3 4 8 9 16 15 14 13 20 19 18 17 7 6 5
    6:  5 1 2 12 11 15 14 13 20 19 18 17 16 9 10 3 4 8 7 6 5
    7:  5 1 2 12 11 15 16 9 10 3 4 8 7 17 18 14 13 20 19 6 5
    8:  5 1 2 12 11 15 16 17 18 14 13 20 19 6 7 8 9 10 3 4 5
    9:  5 1 2 12 13 20 19 6 7 8 9 16 17 18 14 15 11 10 3 4 5
    10:  5 1 2 12 13 20 19 18 14 15 11 10 3 4 8 9 16 17 7 6 5
    11:  5 1 20 13 12 2 3 4 8 7 17 16 9 10 11 15 14 18 19 6 5
    12:  5 1 20 13 12 2 3 10 11 15 14 18 19 6 7 17 16 9 8 4 5
    13:  5 1 20 13 14 15 11 12 2 3 10 9 16 17 18 19 6 7 8 4 5
    14:  5 1 20 13 14 15 16 9 10 11 12 2 3 4 8 7 17 18 19 6 5
    15:  5 1 20 13 14 15 16 17 18 19 6 7 8 9 10 11 12 2 3 4 5
    16:  5 1 20 13 14 18 19 6 7 17 16 15 11 12 2 3 10 9 8 4 5
    17:  5 1 20 19 6 7 8 9 10 11 15 16 17 18 14 13 12 2 3 4 5
    18:  5 1 20 19 6 7 17 18 14 13 12 2 3 10 11 15 16 9 8 4 5
    19:  5 1 20 19 18 14 13 12 2 3 4 8 9 10 11 15 16 17 7 6 5
    20:  5 1 20 19 18 17 16 9 10 11 15 14 13 12 2 3 4 8 7 6 5
    21:  5 4 3 2 1 20 13 12 11 10 9 8 7 17 16 15 14 18 19 6 5
    22:  5 4 3 2 1 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5
    23:  5 4 3 2 12 11 10 9 8 7 6 19 18 17 16 15 14 13 20 1 5
    24:  5 4 3 2 12 13 14 18 17 16 15 11 10 9 8 7 6 19 20 1 5
    25:  5 4 3 10 9 8 7 6 19 20 13 14 18 17 16 15 11 12 2 1 5
    26:  5 4 3 10 9 8 7 17 16 15 11 12 2 1 20 13 14 18 19 6 5
    27:  5 4 3 10 11 12 2 1 20 13 14 15 16 9 8 7 17 18 19 6 5
    28:  5 4 3 10 11 15 14 13 12 2 1 20 19 18 17 16 9 8 7 6 5
    29:  5 4 3 10 11 15 14 18 17 16 9 8 7 6 19 20 13 12 2 1 5
    30:  5 4 3 10 11 15 16 9 8 7 17 18 14 13 12 2 1 20 19 6 5
    31:  5 4 8 7 6 19 18 17 16 9 10 3 2 12 11 15 14 13 20 1 5
    32:  5 4 8 7 6 19 20 13 12 11 15 14 18 17 16 9 10 3 2 1 5
    33:  5 4 8 7 17 16 9 10 3 2 1 20 13 12 11 15 14 18 19 6 5
    34:  5 4 8 7 17 18 14 13 12 11 15 16 9 10 3 2 1 20 19 6 5
    35:  5 4 8 9 10 3 2 1 20 19 18 14 13 12 11 15 16 17 7 6 5
    36:  5 4 8 9 10 3 2 12 11 15 16 17 7 6 19 18 14 13 20 1 5
    37:  5 4 8 9 16 15 11 10 3 2 12 13 14 18 17 7 6 19 20 1 5
    38:  5 4 8 9 16 15 14 13 12 11 10 3 2 1 20 19 18 17 7 6 5
    39:  5 4 8 9 16 15 14 18 17 7 6 19 20 13 12 11 10 3 2 1 5
    40:  5 4 8 9 16 17 7 6 19 18 14 15 11 10 3 2 12 13 20 1 5
    41:  5 6 7 8 4 3 2 12 13 14 15 11 10 9 16 17 18 19 20 1 5
    42:  5 6 7 8 4 3 10 9 16 17 18 19 20 13 14 15 11 12 2 1 5
    43:  5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5
    44:  5 6 7 8 9 16 17 18 19 20 1 2 12 13 14 15 11 10 3 4 5
    45:  5 6 7 17 16 9 8 4 3 10 11 15 14 18 19 20 13 12 2 1 5
    46:  5 6 7 17 16 15 11 10 9 8 4 3 2 12 13 14 18 19 20 1 5
    47:  5 6 7 17 16 15 11 12 13 14 18 19 20 1 2 3 10 9 8 4 5
    48:  5 6 7 17 16 15 14 18 19 20 13 12 11 10 9 8 4 3 2 1 5
    49:  5 6 7 17 18 19 20 1 2 3 10 11 12 13 14 15 16 9 8 4 5
    50:  5 6 7 17 18 19 20 13 14 15 16 9 8 4 3 10 11 12 2 1 5
    51:  5 6 19 18 14 13 20 1 2 12 11 15 16 17 7 8 9 10 3 4 5
    52:  5 6 19 18 14 15 11 10 9 16 17 7 8 4 3 2 12 13 20 1 5
    53:  5 6 19 18 14 15 11 12 13 20 1 2 3 10 9 16 17 7 8 4 5
    54:  5 6 19 18 14 15 16 17 7 8 9 10 11 12 13 20 1 2 3 4 5
    55:  5 6 19 18 17 7 8 4 3 2 12 11 10 9 16 15 14 13 20 1 5
    56:  5 6 19 18 17 7 8 9 16 15 14 13 20 1 2 12 11 10 3 4 5
    57:  5 6 19 20 1 2 3 10 9 16 15 11 12 13 14 18 17 7 8 4 5
    58:  5 6 19 20 1 2 12 13 14 18 17 7 8 9 16 15 11 10 3 4 5
    59:  5 6 19 20 13 12 11 10 9 16 15 14 18 17 7 8 4 3 2 1 5
    60:  5 6 19 20 13 14 18 17 7 8 4 3 10 9 16 15 11 12 2 1 5

     

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    const int Max = 111;
    int map[Max][Max];
    int vis[Max];
    int way[Max];
    int m,cas;
    void dfs(int x,int len){
        for(int j=1;j<=3;j++){
            int u=map[x][j];
            if(u==m&&len==20)
            {
                cout<<cas<<":  "<<m<<" ";
                cas++;
                for(int k=1;k<20;k++)
                cout<<way[k]<<" ";
                cout<<m<<endl;
            }
            if(!vis[u]){
            vis[u]=1;
            way[len]=u;
            dfs(u,len+1);
            vis[u]=0;
            }  
        }
    }
         
    int main(){
        for(int i=1;i<=20;i++){
            for(int j=1;j<=3;j++){
                cin>>map[i][j];
            }
        }
        while((cin>>m),m){
            cas=1;
            memset(vis,0,sizeof(vis));
            vis[m]=1;
            dfs(m,1);
        }
        return 0;
    }
    

      

  • 相关阅读:
    USTC 软硕讯飞班参考资源
    nginx 负载均衡
    Meidawiki 配置
    10 款实用的jquery插件
    Mongodb 定时释放内存
    互联网产品精神解读
    简单的缓冲区溢出实验
    fatal error C1902: 程序数据库管理器不匹配;请检查安装解决
    C#的override、new、vitutal一例
    SQL Server 2008导入、导出数据库
  • 原文地址:https://www.cnblogs.com/zmin/p/7340464.html
Copyright © 2011-2022 走看看