UVA - 806 Spatial Structures - DFS

紫书数据结构那章的题,今天个人赛开了出来,纸上模拟实现了一遍,因为格式问题 PE 了好几次,中间还有两次 忘记删除 文件测试那两行

写完了 看一下感觉有点丑,但好歹思路还算清晰,贴一哈

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>


using namespace std;
const int maxn = 100 + 7;
int n, a[maxn][maxn];  // shuzu ji daxiao
set<int> st, stans;
int cnt;
char s[1000];

void init() {
    st.clear();
    stans.clear();
    cnt = 0;
    memset(a, 0, sizeof a);
    if(n > 0) {
        for(int i = 0; i < n; ++i) {
            scanf("%s", s);
            for(int j = 0; j < n; ++j) {
                a[i][j] = s[j] - '0';
            }
                //scanf("%d", &a[i][j]);// >> a[i][j]
        }
    }
    else {
        //n = -1*n;
        int x;
        while(scanf("%d", &x) && x != -1) {
            st.insert(x);   //  mingling jihe
        }
    }
}

void dfs(int x, int y, int d, int id) {
    int num = 0;
    for(int i = x; i < x+d; ++i) {
        for(int j = y; j < y+d; ++j) {
            if(a[i][j] == 1) num++;    // heidian geshu
        }
    }
    if(num == d*d) {
        cnt++;             // hei jiedian geshu
        st.insert(id);
    }
    else if(num == 0) {
        return;
    }
    else {
        dfs(x, y, d/2, id*10+1);  // NO. x hang, y lie
        dfs(x, y+d/2, d/2, id*10+2);
        dfs(x+d/2, y, d/2, id*10+3);
        dfs(x+d/2, y+d/2, d/2, id*10+4);
    }
}

void dfs2(int x, int y, int d, int id) {
    if(id == 0) {
        for(int i = x; i < x+d; ++i) {
            for(int j = y; j < y+d; ++j) {
                a[i][j] = 1;
            }
        }
    }
    else {
        int t = id % 5;
        if(t == 1) dfs2(x, y, d/2, id/5);  // NO. x hang, y lie
        if(t == 2) dfs2(x, y+d/2, d/2, id/5);
        if(t == 3) dfs2(x+d/2, y, d/2, id/5);
        if(t == 4) dfs2(x+d/2, y+d/2, d/2, id/5);
    }
}

void solve() {
    if(n > 0) {
        dfs(0, 0, n, 0);
        if(cnt == 0) {
            printf("Total number of black nodes = 0\n");  // ans1
        }
        else {
            set<int>::iterator it = st.begin();
            for( ; it != st.end(); ++it) {
                int t = *it;
                int sum = 0;
                while(t) {
                    sum = sum*5 + t%10;
                    t /= 10;
                }
                stans.insert(sum);
            }
            int tt = 0;
            for(it = stans.begin(); it != stans.end(); ++it) {
                if(tt) cout << ' ';
                cout << *it;
                tt++;

                if(tt >= 12) { tt = 0; cout << endl; }
            }
            if(tt != 0) cout << endl;
            printf("Total number of black nodes = %d\n", cnt);  // nas2
        }
    }
    else {
        n = -1*n;
        set<int>::iterator it = st.begin();
        for( ; it != st.end(); ++it) {
            int t = *it;
            if(t == 0) {
                for(int i = 0; i < n; ++i) {
                    for(int j = 0; j < n; ++j) {
                        //if(j) cout << " ";
                        cout << '*' ;
                    }
                    cout << endl;
                }
                //cout << endl;
                return;
            }
            else {
                dfs2(0, 0, n, t);
            }
        }
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j < n; ++j)
                if(a[i][j] == 0) cout << '.';
                else if(a[i][j] == 1) cout << '*';
            cout << endl;
        }
        //cout << endl;
    }
}

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int kase = 1;
    while(scanf("%d", &n) != EOF && n) {
        init();
        if(kase != 1) cout << endl;
        printf("Image %d\n", kase++);
        solve();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值