深搜dfs

本文介绍了如何使用深度优先搜索(DFS)解决两种经典问题:判断地图上的油田数量和N皇后问题。对于油田计数,通过深搜遍历每个油田并标记已访问过的区域来避免重复计数。对于N皇后问题,同样利用DFS放置皇后并检查冲突,直至找到所有可能的解决方案。题目包括杭电HDU的1241题和842、843题,分别对应油田计数、数字排列和N皇后问题的解法。
摘要由CSDN通过智能技术生成

hdoj1241 Oil Deposits

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 67090    Accepted Submission(s): 38717


 
Problem Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
 
 
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
 
 
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
 
 
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
 
 
Sample Output

0
1
2
2

怎么可以这么笨!快哭了 才刚开始深搜就理解这么费劲了..

题意:输入行列数和字符 @表示油田 *表示空地 上下左右以及斜对角连通都表示同一块油田 输出油田数

ac代码:

#include<iostream>
char oil[105][105];//记录总地图
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,1},{1,0}};
int m,n;
void dfs(int x,int y)
{
    //遍历周围八个点
    for(int i=0;i<8;i++)
    {
        int x1=x+dir[i][0];
        int y1=y+dir[i][1];
        if(x1>=0&&x1<m&&y1>=0&&y1<n&&oil[x1][y1]=='@')
        {
            oil[x1][y1]='*';
            dfs(x1,y1);
        }
    }
}
int main()
{
    using namespace std;
    while(cin>>m>>n,m)
    {
        int num=0;
        for(int i=0;i<m;++i)
            cin>>oil[i];
        for(int i=0;i<m;++i)
            for(int j=0;j<n;++j)
            {
                if(oil[i][j]=='@')
                {
                    oil[i][j]='*';
                    num++;
                    dfs(i,j);
                }
            }
               cout<<num<<endl;
    }
}

那个遍历的过程及dir[x][y]如果看不懂的话也可以写成这样(我才不说是谁理解了半天还没懂哭泣

    dfs(x-1,y-1);
    dfs(x-1,y);
    dfs(x-1,y+1);
    dfs(x,y-1);
    dfs(x,y+1);
    dfs(x+1,y-1);
    dfs(x+1;y+1);
    dfs(x+1;y);

就是深搜周围八个点 表达意思一样啦

每经过一个油田可以把它变成空地表示自己访问过了 或者可以用一个visit[x][y]数组记录自己有没有访问过这个地方


acwing842 排列数字

原题链接

代码:

#include<iostream>
#include<cstring>
using namespace std;
const int N=10;
int path[N];//保存路径
bool st[N];//记录当前点有没有被输出过
void dfs(int u,int n)
{

    if(u==n)//如果找到最后一层了
    {
        for(int i=0;i<n;++i)
            cout<<path[i]<<' ';
        cout<<endl;
        return;
    }
    for(int i=1;i<=n;++i)
    {
        if(!st[i])//如果当前数字i没有被用过
        {
            path[u]=i;//保存数字i到路径中
            st[i]=true;//当前数字被用过了
            dfs(u+1,n);//继续往下深搜
            st[i]=false;//回溯 恢复原样
        }
    }
}


int main()
{
    int n;
    cin>>n;
    dfs(0,n);
    return 0;
}

 acwing843 n皇后问题

原题链接

代码:

#include<iostream>
#include<cstdio>
using namespace std;
const int N=20;//因为有对角线 所以开20
char g[N][N];
bool col[N]={false},dg[N]={false},udg[N]={false};
int n;
void dfs(int u)//以行搜索
{
    if(u==n)//当深搜行数等于总行数 输出每一行
    {
        for(int i=0;i<n;++i)
            puts(g[i]);//输出字符不会自动换行
        puts("");//puts函数输出字符串会自动换行
        return;
    }
    for(int i=0;i<n;++i)
    {
        if(!col[i]&&!dg[u+i]&&!udg[i-u+n])//这里dg[]和udg[]里的下标都是用截距表示该对角线
        {
            g[u][i]='Q';
            col[i]=dg[u+i]=udg[i-u+n]=true;
            dfs(u+1);
            col[i]=dg[u+i]=udg[i-u+n]=false;
            g[u][i]='.';
        }
    }
}
int main()
{
    cin>>n;
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
            g[i][j]='.';
    dfs(0);
    return 0;
}

杭电有道类似的题 只不过是输出排列个数   hdoj2553   记得打表不然会TLE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值