搜索专题之深度优先搜索(DFS)

什么是DFS?

深度优先遍历图的方法是,从图中某顶点v出发:
(1)访问顶点v;
(2)依次从v的未被访问的邻接点出发,对图进行深度优先遍历;直至图中和v有路径相通的顶点都被访问;
(3)若此时图中尚有顶点未被访问,则从一个未被访问的顶点出发,重新进行深度优先遍历,直到图中所有顶点均被访问过为止。

基本框架

·dfs(状态) [3]
–if 状态 是 目标状态then
·dosomething
–else
·for 每个新状态
–if 新状态合法
»dfs(新状态)
·主程序:
·dfs(初始状态)

例题

Ancient Go

Yu Zhou likes to play Go with Su Lu. From the historical research, we found that there are much difference on the rules between ancient go and modern go.

Here is the rules for ancient go they were playing:

⋅The game is played on a 8×8 cell board, the chess can be put on the intersection of the board lines, so there are 9×9 different positions to put the chess.
⋅Yu Zhou always takes the black and Su Lu the white. They put the chess onto the game board alternately.
⋅The chess of the same color makes connected components(connected by the board lines), for each of the components, if it’s not connected with any of the empty cells, this component dies and will be removed from the game board.
⋅When one of the player makes his move, check the opponent’s components first. After removing the dead opponent’s components, check with the player’s components and remove the dead components.
One day, Yu Zhou was playing ancient go with Su Lu at home. It’s Yu Zhou’s move now. But they had to go for an emergency military action. Little Qiao looked at the game board and would like to know whether Yu Zhou has a move to kill at least one of Su Lu’s chess.

Input

The first line of the input gives the number of test cases, T(1≤T≤100). T test cases follow. Test cases are separated by an empty line. Each test case consist of 9 lines represent the game board. Each line consists of 9 characters. Each character represents a cell on the game board. ′.′ represents an empty cell. ′x′ represents a cell with black chess which owned by Yu Zhou. ′o′ represents a cell with white chess which owned by Su Lu.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is Can kill in one move!!! if Yu Zhou has a move to kill at least one of Su Lu’s components. Can not kill in one move!!! otherwise.

Sample Input

2

…xo


…x…
.xox…x
.o.o…xo
…o…
…xxxo
…xooo.

…ox.
…o.
…o…
…o.o…
…o…

…o.
…x…
…o

Sample Output

Case #1: Can kill in one move!!!
Case #2: Can not kill in one move!!!

Hint

In the first test case, Yu Zhou has 4 different ways to kill Su Lu’s component.

In the second test case, there is no way to kill Su Lu’s component.

题意

给你一个围棋残局,你执黑先手,问有几种杀死白棋的方式(杀死任意一片白棋都可以),其中判定棋子死亡的规则和围棋规则一样,不做过多解释。

思路

思路其实很清晰,先利用DFS求出棋盘上的白棋分成了多少个连通块,然后找到连通块后再找到“气”只有一口的联通块,其数量就是答案。

代码

#include <iostream>
#include<cstring>
using namespace std;
const int maxn=100+5;
char pic[maxn][maxn];
int m,n,idx[maxn][maxn];
int cnt_i;
int mov[4][2]= {0,-1,0,1,-1,0,1,0};
int vis[10][10];
void dfs(int r,int c,int id)
{
    if(r<0||r>=9||c<0||c>=9)
        return;
    if(idx[r][c]>0||pic[r][c]!='o')
    {
        if(pic[r][c]=='.'&&vis[r][c]==0)
        {
            cnt_i++;
            vis[r][c]=1;
        }
        return;
    }
    idx[r][c]=id;
    for(int i=0; i<4; i++)
        dfs(r+mov[i][0],c+mov[i][1],id);
}
int main()
{
    int T,p=0;
    cin>>T;
    while(T--)
    {
        for(int i=0; i<9; i++)
            for(int j=0; j<9; j++)
                cin>>pic[i][j];
        memset(idx,0,sizeof(idx));
        int flag=0;
        int cnt=0;
        for(int i=0; i<9; i++)
        {
            if(flag==1)
                break;
            for(int j=0; j<9; j++)
                if(idx[i][j]==0&&pic[i][j]=='o')
                {
                    cnt_i=0;
                    memset(vis,0,sizeof(vis));
                    dfs(i,j,++cnt);
                    if(cnt_i==1)
                    {
                        flag=1;
                        break;
                    }
                }
        }
        if(flag==1)
            cout<<"Case #"<<++p<<": Can kill in one move!!!"<<endl;
        else
            cout<<"Case #"<<++p<<": Can not kill in one move!!!"<<endl;
    }
}

例题链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值