HDU 4431 Mahjong (DFS, brute force enumeration, pruning)

Source: Internet
Author: User

Test instructions: Given 13 tiles, ask if you are "listening" to the card, if it is output "listen" which one.

Analysis: This problem, very obvious violence, is on the original foundation put on a card, see is not can Hu, idea is very simple, also better realize, the result is tle, has been tle, this unscientific ah ...

Very hard to write out, unexpectedly tle ... Heartache. is the first to determine a card, and then the flush analysis, in fact, is to prune, that is, if there are 1 or two cards, and you can not constitute the time of the carving, it is necessary to return false, because this has not been solved.

This one pruning, on AC.

The code is as follows:

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < Stack>using namespace std; typedef long Long Ll;typedef pair<int, int> p;const int INF = 0x3f3f3f3f;const double inf = 0x3f3f3f3f3f3f;const Double PI = ACOs ( -1.0); const double EPS = 1e-8;const int maxn = ten + 5;const int mod = 1e9 + 7; const char *mark = "+-*"; const int dr[] = {-1, 0, 1, 0};const int dc[] = {0, 1, 0, -1};int N, m;inline bool is_in (int R, I NT c) {return R >= 0 && r < n && C >= 0 && C < m;} Const char* mahjong[] = {"1m", "2m", "3m", "4m", "5m", "6m", "7m", "8m", "9m", "1s", "2s", "3s", "4s", "5s", "6s", "7s", "8s", "9s", "1p", "2p", "3p", "4p", "5p", "6p", "7p", "8p", "9p", "1C "," 2c "," 3c "," 4c "," 5c "," 6c "," 7c "};map<string, int> mp;inline void init () {for (int i = 0; i <; ++i) Mp[string (Mahjong[i])] = i;}        int C[35];bool dfs (int d) {for (int i = 0; i <; ++i) {int cnt = 0;        if (C[i] >= 3) {if (d = = 3) return true;        ++cnt;        C[i]-= 3;  if (Dfs (d+1)) {C[i] + = 3; return true;        } C[i] + = 3; if (i <= && i% 9 <= 6 && c[i] >= 1 && c[i+1] >= 1 && c[i+2] >=            1) {++cnt;            if (d = = 3) return true;  --c[i];  --C[I+1];            --C[I+2];  if (Dfs (d+1)) {++c[i];  ++C[I+1];  ++C[I+2]; return true;  } ++c[i];  ++C[I+1];        ++C[I+2]; } if (cnt = = 0 && c[i]) return false; This card has but does not match the straight or the end of this layer search if (c[i]) break; This pruning is because the search will continue to traverse from him the next time it is traversed, so there is no need to go on.} return false;          inline bool Check () {for (int i = 0; i < ++i) {if (C[i] >= 2) {  C[i]-= 2;   if (Dfs (0)) {C[i] + = 2; return true;        } C[i] + = 2; }} return false;}    int ans[50];inline bool Solvefood () {int food[35];    memcpy (food, C, sizeof (c));    int cnt = 0;        for (int i = 0; i <; i + = 9) {--food[i], cnt + = food[i];    if (Food[i] < 0) return false;        } for (int i = 8; i <; i + = 9) {--food[i], cnt + = food[i];    if (Food[i] < 0) return false;        } for (int i = n; i < ++i) {--food[i], cnt + = food[i];    if (Food[i] < 0) return false; } return cnt = = 1;}    inline bool Solveseven () {for (int i = 0; i < ++i) if (c[i]! = 2 && c[i]! = 0) return false; return true;}    int main () {char s[10];    Init ();  int T;    scanf ("%d", &t);        while (t--) {memset (c, 0, sizeof (c));            for (int i = 0; i < ++i) {scanf ("%s", s);        ++c[mp[string (s)];        } int cnt = 0; for (int i = 0; i < ++i;) {if (C[i] >= 4) continue;            ++c[i];            if (Solvefood ()) ans[cnt++] = i;            else if (Solveseven ()) ans[cnt++] = i;            else if (check ()) ans[cnt++] = i;        --c[i];        } if (!cnt) puts ("Nooten");            else{printf ("%d", CNT);            for (int i = 0; i < cnt; ++i) printf ("%s", Mahjong[ans[i]]);        printf ("\ n"); }} return 0;}

HDU 4431 Mahjong (DFS, brute force enumeration, pruning)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.