dfs找环

源博客

//观察可看出几张图中圈数不一样,可以根据此来识别每张图
//先dfs整张图,填充颜色编号-----》将像素点为1的点的编号m存储起来-----》将环数存储进以m为下标的set中
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <algorithm>
 
using namespace std;
const int maxn = 200 + 10;
 
char bin[256][5], line[maxn];
int H, W, pic[maxn][maxn], color[maxn][maxn];
vector<set<int> > neighbor;
 
const int dir_row[] = {-1, 1, 0, 0};
const int dir_col[] = {0, 0, -1, 1};
 
void dfs(int row, int col, int num) {
    color[row][col] = num;
    for(int i = 0; i < 4; i++) {
        int row2 = row + dir_row[i];
        int col2 = col + dir_col[i];
        if(row2 >= 0 && row2 < H && col2 >= 0 && col2 < W && pic[row2][col2] == pic[row][col] && color[row2][col2] == 0)
            dfs(row2, col2, num);
    }
}
 
void decode(char c, int row, int col) {
    for(int i = 0; i < 4; i++)
        pic[row][col+i] = bin[c][i] - '0';
}
 
void check_neighbor(int r, int c) {
    for(int i = 0; i < 4; i++) {
        int r2 = r + dir_row[i];
        int c2 = c + dir_row[i];
        if(r2 >= 0 && r2 < H && c2 >= 0 && c2 < W && pic[r2][c2] == 0 && color[r2][c2] != 1)    //圈外的0的color是1
            neighbor[color[r][c]].insert(color[r2][c2]);
    }
}
 
const char *s = "WAKJSD";
char recognize(int num) {
    int c = neighbor[num].size();
    return s[c];
}
 
int main()
{
    strcpy(bin['0'], "0000");
    strcpy(bin['1'], "0001");
    strcpy(bin['2'], "0010");
    strcpy(bin['3'], "0011");
    strcpy(bin['4'], "0100");
    strcpy(bin['5'], "0101");
    strcpy(bin['6'], "0110");
    strcpy(bin['7'], "0111");
    strcpy(bin['8'], "1000");
    strcpy(bin['9'], "1001");
    strcpy(bin['a'], "1010");
    strcpy(bin['b'], "1011");
    strcpy(bin['c'], "1100");
    strcpy(bin['d'], "1101");
    strcpy(bin['e'], "1110");
    strcpy(bin['f'], "1111");
 
    int kase = 0;
    while(scanf("%d%d", &H, &W) == 2 && H && W) {
        memset(pic, 0, sizeof(pic));
        memset(color, 0, sizeof(color));
 
        for(int i = 0; i < H; i++) {
            scanf("%s", line);
            for(int j = 0; j < W ;j++)
                decode(line[j], i + 1, j * 4 + 1);  //将输入的数据解码到pic[i+1][j*4+1],注意行列是从下标1开始
        }
 
        H += 5;     //按理说加一就行,为什么 H += 1 会WA? 换成 H += 2 刚好可以AC ???????????????????
        W = W * 4 + 5;  //因为原来的输入是16进制,转化为10进制后每个原来的16进制代表4个数字
 
        int cnt = 0;
        vector<int> vec;
        for(int i = 0; i < H; i++)
        for(int j = 0; j < W; j++) {
            if(!color[i][j]) {
                dfs(i, j, ++cnt);
                if(pic[i][j] == 1)  vec.push_back(cnt);     //把图像中等于1的像素点的color编号都存储进vec容器
            }
        }
 
        neighbor.clear();                   //neighbor用来存储环内的点
        neighbor.resize(cnt + 5);   //其实相当于对每个成员初始化,去掉会引起RE,关于resize很容易用错,最好查下API文档
        for(int i = 0; i < H; i++)
        for(int j = 0; j < W; j++)
            if(pic[i][j] == 1)  check_neighbor(i, j);   //将像素点为1周围的空环编号存储进neighbor容器里面
 
        vector<char> ans;   //存储结果
        for(int i = 0; i < vec.size(); i++)
            ans.push_back(recognize(vec[i]));
 
        sort(ans.begin(), ans.end());//对结果进行整理排序
 
        printf("Case %d: ", ++kase);
        for(int j = 0; j < ans.size(); j++)
            printf("%c", ans[j]);
        printf("\n");
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用C++实现DFS除环的代码: ```cpp #include <iostream> #include <vector> #include <cstring> using namespace std; const int MAXN = 1e5 + 5; int n, m, num, tot; int head[MAXN], dfn[MAXN], low[MAXN], stk[MAXN]; bool vis[MAXN], ins[MAXN]; struct Edge { int to, next; } edge[MAXN << 1]; inline void add_edge(int u, int v) { edge[++tot].to = v; edge[tot].next = head[u]; head[u] = tot; } void tarjan(int u) { dfn[u] = low[u] = ++num; vis[u] = ins[u] = true; stk[++stk[0]] = u; for (int i = head[u]; i; i = edge[i].next) { int v = edge[i].to; if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else if (ins[v]) { low[u] = min(low[u], dfn[v]); } } if (dfn[u] == low[u]) { int v; do { v = stk[stk[0]--]; ins[v] = false; } while (v != u); } } int main() { cin >> n >> m; tot = 1; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; add_edge(u, v); } for (int i = 1; i <= n; i++) { if (!dfn[i]) { tarjan(i); } } return 0; } ``` 代码中使用了DFS除环来找出无向图中的所有环,具体实现过程如下: 1. 首先进行图的输入,并将边存储到邻接表中; 2. 定义一个栈,用于存储访问过的节点; 3. 对于每个未被访问过的节点u,进行如下处理: - 对u进行DFS遍历; - 在回溯时,如果u的某个邻接节点v的dfn值大于等于u,则说明(u,v)是环的一部分,将(u,v)加入栈中; - 如果u是环的起点,则将栈中的所有元素弹出,构成一个环; 4. 重复步骤3,直到所有的节点都被访问过。 希望能够帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值