联通块缩点(图的BFS)—— ZOJ 3781

  • 题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781

  • 题意: 给出一个N*M的棋盘,每个格子有白色或者黑色的棋子,若2个同色棋子相邻,则称他们是联通的。每次可以选择一大块联通的棋子进行翻转,使整块变成另一种颜色,求最少要操作多少次才能使得整个棋盘的颜色一致

  • 分析: 题中很明显地提到了联通这个概念,我们首先会想到将相同颜色相邻的棋子即一个联通块缩成一个点,然后不同点之间开始建边。每一次翻转相当于从一个点开始进行BFS,这个点所能抵达的其它点和当前点都会变成同一个颜色,而若要将整个棋盘变成同一种颜色,我们需要讲BFS进行到底,即搜索到不能再搜索,这个深度就是从BFS起点开始到整个棋盘颜色一致所需要的操作数,所以我们可以从每个点开始进行一次BFS,最后取最小值即可

  • AC代码:

/*************************************************************************
    > File Name: test.cpp
    > Author: Akira 
    > Mail: qaq.febr2.qaq@gmail.com 
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <ctime>
#include <climits>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
using namespace std;

#define MaxN 100000
#define MaxM MaxN*10
#define INF 0x3f3f3f3f
#define bug cout<<88888888<<endl;
#define MIN(x,y) (x<y?x:y)
#define MAX(x,y) (x>y?x:y)

template<typename _> inline void scan(_& t)
{
    int c;
    while((c = getchar()) < '0' || c > '9');
    t = c - '0';
    while((c = getchar()) >= '0' && c <= '9') t = t * 10 + c - '0';
}
template<typename _> inline void print(_ x)
{
    int len = 0, p[20];
    if(x < 0) putchar('-'), x = -x;
    while(x) p[++len] = x % 10, x /= 10;
    if(!len) p[++len] = 0;
    while(len) putchar(p[len--] + '0');
}

int T;
int N,M;
char Map[41][41];
int id[41][41];
int Num;
vector<int> V[1700];
set<int> S[1700];
int dir[4][2] = {{1,0}, {0,1}, {-1,0}, {0,-1}};
bool check(int x, int y)
{
    if(x<0||x>=N||y<0||y>=M) return false;
    else return true;
}
void DFS(int x, int y)
{
    id[x][y] = Num;
    for(int i=0;i<4;i++)
    {
        int xx = x + dir[i][0];
        int yy = y + dir[i][1];
        if(check(xx,yy))
        {
            if( Map[xx][yy] == Map[x][y] && id[xx][yy]==0)
            {  
                DFS(xx,yy);
            }
            else if( Map[xx][yy] != Map[x][y] && id[xx][yy]!=0)
            {
                int t = id[xx][yy];
                if (!S[Num].count(t))
                {
                    S[Num].insert(t);
                    V[t].push_back(Num);
                    V[Num].push_back(t);
                }
            }
        }
    }
}
int vis[1700];
int BFS(int s)
{
    CLR(vis);
    vis[s] = 1;
    queue<int> Q;
    queue<int> D;
    Q.push(s);
    D.push(0);
    int ans = 0;
    while(!Q.empty())
    {
        int tmp = Q.front();Q.pop();
        int d = D.front();D.pop();
        ans = MAX(ans, d);
        for(int i=0;i<V[tmp].size();i++)
        {
            int v = V[tmp][i];
            if(!vis[v])
            {
                vis[v] = 1;
                Q.push(v);
                D.push(d+1);
            }
        }
    }
    return ans;
}

void init()
{
    CLR(id);
    Num = 0;
}
int main()
{
    scanf("%d", &T);
    while(T--)
    {
        init();
        scanf("%d%d", &N, &M);
        for(int i=0;i<N;i++)
        {
            scanf("%s", Map[i]);
        }
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<M;j++)
            {
                if(!id[i][j])
                {
                    id[i][j] = ++Num;
                    V[Num].clear();
                    S[Num].clear();
                    DFS(i,j);
                }
            }
        }
        int ans = INF;
        for(int i=1;i<=Num;i++)
        {
            int tmp = BFS(i);
            ans = MIN(ans, tmp);
        }
        printf("%d\n", ans);
    }
    //system("pause");
}
/*

1345
2 2
OX
OX
3 3
XOX
OXO
XOX

*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DFS算法是一种用于遍历或树遍历的算法。其核心思想是从起点开始递归地深入每一个可能的分支,直到无法继续为止,然后回溯到上一个节点,继续尝试其他分支。 DFS算法有两种实现方式:递归实现和非递归实现(使用栈)。 递归实现的DFS算法伪代码: ``` DFS(node): if node is None: return visit(node) for child in node.children: DFS(child) ``` 非递归实现的DFS算法伪代码: ``` DFS(node): stack = [node] while stack: node = stack.pop() if node.visited: continue visit(node) node.visited = True for child in node.children: stack.append(child) ``` 其中,visit(node)表示对节点node进行操作,例如打印节点值、记录路径等。 DFS算法的时间复杂度为O(V+E),其中V为节点数,E为边数。因为每个节点和每条边都只会被访问一次。 下面是一个例题,用DFS算法求解从起点到终点的路径(leetcode 79题): 给定一个二维网格和一个单词,找出该单词是否存在于网格中。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。 示例: ``` board = [ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ] 给定 word = "ABCCED", 返回 true. 给定 word = "SEE", 返回 true. 给定 word = "ABCB", 返回 false. ``` 实现代码: ```python class Solution: def exist(self, board: List[List[str]], word: str) -> bool: if not board or not board[0]: return False m, n = len(board), len(board[0]) visited = [[False] * n for _ in range(m)] directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] def dfs(i, j, k): if k == len(word): return True if i < 0 or i >= m or j < 0 or j >= n or visited[i][j] or board[i][j] != word[k]: return False visited[i][j] = True for dx, dy in directions: if dfs(i+dx, j+dy, k+1): return True visited[i][j] = False return False for i in range(m): for j in range(n): if dfs(i, j, 0): return True return False ``` 时间复杂度为O(m*n*3^k),其中m、n为网格大小,k为单词长度。因为每个格子都有可能走,所以是O(m*n),而每次调用dfs函数会向四个方向递归,每个方向都不能重复走,所以是3^k。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值