码迷,mamicode.com
首页 > 其他好文 > 详细

Gym-102411C

时间:2021-03-03 12:15:26      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ext   c++   har   strong   end   its   amp   get   %s   

C - Cross-Stitch

tag: 构造, 欧拉回路

题意

十字绣穿针引线,保证 ‘X‘ 八联通

思路

技术图片

这样构造,然后正反跑欧拉回路

#include<bits/stdc++.h>
using namespace std;

const int N = 3e5 + 10;
typedef long long ll;

struct Edge {
    int to, next, id;
};
int idcnt;
struct {
    int head[N], tot;
    int vis[N];
    Edge E[N];
    void addEdge(int from, int to) {
        //cout << from << " " << to << endl;
        int id = ++idcnt;
        E[++tot] = { to, head[from], id };
        head[from] = tot;

        E[++tot] = { from,head[to], id };
        head[to] = tot;
    }
}Edges[2];

int n, m;
char mp[200][200];
int id[200][200];
int ans[N], anscnt;

void dfs(int u, int p) {
    for (int& i = Edges[p].head[u];i;i = Edges[p].E[i].next) {
        int v = Edges[p].E[i].to;
        int id = Edges[p].E[i].id;
        if (!Edges[p].vis[id]) {
            Edges[p].vis[id] = 1;
            //cout << "now : " << u << " nxt : " << v << endl;
            dfs(v, p ^ 1);
            ans[++anscnt] = u;
        }
    }
}
    
int main() {
    scanf("%d%d", &m, &n);

    for (int i = 1;i <= n;i++) {
        scanf("%s", mp[i] + 1);
    }
    int cnt = 0;
    for (int i = 0;i <= n;i++)for (int j = 0;j <= m;j++)id[i][j] = ++cnt;
    cnt = 0;
    int idx = -1;
    for (int i = 1;i <= n;i++) {
        for (int j = 1;j <= m;j++) {
            if (mp[i][j] == ‘X‘) {
                Edges[0].addEdge(id[i][j - 1], id[i - 1][j]);
                Edges[0].addEdge(id[i - 1][j - 1], id[i][j]);
                idx = id[i - 1][j - 1];

                Edges[1].addEdge(id[i - 1][j - 1], id[i][j - 1]);
                Edges[1].addEdge(id[i - 1][j], id[i][j]);

                cnt++;
            }
        }
    }
    printf("%d\n", 4 * cnt - 1);

    dfs(idx, 0);
    ans[0] = idx;

    for (int i = anscnt;i >= 1;i--) {
        int now = ans[i];
        printf("%d %d\n", (now - 1) % (m + 1), (now - 1) / (m + 1));
    }
}

Gym-102411C

标签:ext   c++   har   strong   end   its   amp   get   %s   

原文地址:https://www.cnblogs.com/sduwh/p/14471545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!