UV 246-10-20-30 (simulation + STL)

Source: Internet
Author: User
Ultraviolet A 246-10-20-30

Question Link

Question: Give 52 poker stacks seven cards from left to right, and then send seven cards from left to right continuously, if a card stack forms the following three conditions (in order ):
1. The sum of the first two + tails is 10, 20, or 30.
2. The sum of the first and last images is 10, 20, or 30.
3. The last three parts are 10, 20, or 30.
Take the three cards away and put them at the bottom of the total card stack (this step should be continued until the conditions are no longer met or the cards are no longer available)
If a card heap is finished because of this operation, it will not be licensed in the future.
If all the last seven cards can be eliminated, win the game, and the total number of cards is used up, lose the game, or even (that is, keep repeating)
Ask the final win or loss level and output the number of steps

Idea: Simulate, use a vector to record the seven cards and the total cards, so that you can use set to record the status, and then each card heap is represented by a dual-end queue deque, in this way, you can also get it from the beginning or from the tail and simulate it continuously.

Code:

#include <cstdio>#include <cstring>#include <set>#include <queue>#include <vector>using namespace std;int num, zero[8];vector<deque<int> > piple;set<vector<deque<int> > > vis;void init() {    vis.clear();    piple.clear();    memset(zero, 0, sizeof(zero));    for (int i = 0; i < 8; i++)piple.push_back(deque<int>());    piple[7].push_back(num);    for (int i = 0; i < 51; i++) {scanf("%d", &num);piple[7].push_back(num);    }    for (int i = 0; i < 7; i++) {int now = piple[7].front();piple[7].pop_front();piple[i].push_back(now);    }}bool can(int x) {    return (x == 10 || x == 20 || x == 30);}bool tra1(int i) {    int top1 = piple[i].front();    piple[i].pop_front();    int sum = top1 + piple[i].front() + piple[i].back();    if (can(sum)) { piple[7].push_back(top1);piple[7].push_back(piple[i].front());piple[7].push_back(piple[i].back());piple[i].pop_front();piple[i].pop_back();return true;    }    piple[i].push_front(top1);    return false;}bool tra2(int i) {    int back1 = piple[i].back();    piple[i].pop_back();    int sum = back1 + piple[i].front() + piple[i].back();    if (can(sum)) { piple[7].push_back(piple[i].front());piple[7].push_back(piple[i].back());piple[7].push_back(back1);piple[i].pop_front();piple[i].pop_back();return true;    }    piple[i].push_back(back1);    return false;}bool tra3(int i) {    int back1 = piple[i].back();    piple[i].pop_back();    int back2 = piple[i].back();    piple[i].pop_back();    int sum = back1 + back2 + piple[i].back();    if (can(sum)) { piple[7].push_back(piple[i].back());piple[7].push_back(back2);piple[7].push_back(back1);piple[i].pop_back();return true;    }    piple[i].push_back(back2);    piple[i].push_back(back1);    return false;}bool tra(int i) {    if (piple[i].size() < 3) return false;    if (tra1(i)) return true;    if (tra2(i)) return true;    if (tra3(i)) return true;    return false;}void solve() {    int i = 0;    for (int t = 8; ; t++) {int now = piple[7].front();piple[7].pop_front();piple[i].push_back(now);while (tra(i));if (piple[i].size() == 0)    zero[i] = 1;i = (i + 1) % 7;if (vis.find(piple) != vis.end()) {    printf("Draw: %d\n", t);    return;}vis.insert(piple);if (piple[7].size() == 0) {    printf("Loss: %d\n", t);    return;}if (piple[7].size() == 52) {    printf("Win : %d\n", t);    return;}while (zero[i]) i = (i + 1) % 7;    }}int main() {    while (~scanf("%d", &num) && num) {init();solve();    }    return 0;}


UV 246-10-20-30 (simulation + STL)

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.