uva 646 - The Gourmet Club(暴力)

來源:互聯網
上載者:User

標籤:style   http   color   os   io   for   

題目連結:uva 646 - The Gourmet Club

題目大意:有16個人蔘加聚會,聚會一共5天,每天有4桌,每桌4個人,一起吃飯的4個人會互相認識。現在要安排座位使得16個任意兩個人都互相認識。給出前三天的安排,求後兩天的安排。

解題思路:任意兩個人之間肯定只能同桌一次。所以根據這個條件,只要枚舉出第4天的第1桌的情況,就可推匯出所有的,或者是矛盾。

在Poj和Zoj上都過了,uva上過不了,求大神指教。

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int N = 20;char s[N][N];bool flag;int g[N][N], ans[8][4];int c[N], can[N][N], v[N];bool init () {    flag = false;    char str[N];    memset(g, 0, sizeof(g));    memset(c, 0, sizeof(c));    for (int i = 0; i < 3; i++) {        memset(v, 0, sizeof(v));        for (int j = 0; j < 4; j++) {            if (scanf("%s", str) == 1) {                for (int t = 0; t < 4; t++) {                    if (v[str[t]-‘A‘]) flag = true;                    v[str[t]-‘A‘] = 1;                    for (int k = 0; k < 4; k++) {                        if (t == k) continue;                        g[str[t]-‘A‘][str[k]-‘A‘]++;                    }                    g[str[t]-‘A‘][str[t]-‘A‘] = 1;                }            } else                 return false;            memcpy(s[i*4+j], str, sizeof(str));        }    }    for (int i = 0; i < 16; i++) {        for (int j = 0; j < 16; j++) {            if (g[i][j] == 0) {                can[i][c[i]++] = j;            }        }    }    memset(v, 0, sizeof(v));    return true;}int getX (int* x) {    for (int i = 0; i < 4; i++)        if (v[x[i]] == 1) return x[i];    return -1;}void putAns() {    for (int i = 0; i < 3; i++) {        for (int j = 0; j < 4; j++) {            if (j) printf("    ");            printf("%s", s[i*4+j]);        }        printf("\n");    }    for (int k = 0; k < 2; k++) {        for (int i = k; i < 8; i += 2) {            if (i/2) printf("    ");            for (int j = 0; j < 4; j++)                printf("%c", ‘A‘ + ans[i][j]);        }        printf("\n");    }}bool judge (int x, int* y) {    for (int i = 0; i < 4; i++)        if (y[i] == x) return false;    return true;}bool DFS (int d) {    if (d == 8) {        putAns();        return true;    }    if (d == 0) {        do {            ans[d][0] = 0;            memcpy (ans[d]+1, can[0], sizeof(int)*3);            for (int i = 0; i < 4; i++) v[ans[d][i]]++;            if (DFS(d+1)) return true;            for (int i = 0; i < 4; i++) v[ans[d][i]]--;        } while (next_permutation (can[0], can[0] + c[0]));    } else {        int x = getX(ans[d-1]);        if (x == -1) return false;        int cnt = 1;        ans[d][0] = x;        for (int i = 0; i < c[x]; i++) {            int& u = can[x][i];            if (v[u] < 2 && judge (u, ans[d-1])) {                ans[d][cnt++] = can[x][i];            }        }        if (cnt != 4) return false;        for (int i = 0; i < 4; i++) v[ans[d][i]]++;        if (DFS(d+1)) return true;        for (int i = 0; i < 4; i++) v[ans[d][i]]--;    }    return false;}int main () {    while (init ()) {        if (flag || !DFS (0))            printf("It is not possible to complete this schedule.\n");        printf("\n");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.