UVa 127

Hi, here I've coded the solution to UVa 127: https://uva.onlinejudge.org/index.php?option=onlinejudge&Itemid=99999999&page=show_problem&category=&problem=63&mosmsg=Submission+received+with+ID+19874302

Though it works for the test cases, it does not work for the input with the UDebug cases.
Could someone tell me what is wrong with this solution?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <vector>
#include <list>


using namespace std;

string temp;
bool atLeast1Swap, atLeast1Additional;
int compTo=0, curSize;

int main()
{
    while (1) {
        vector< list<string> > stacks(52);
        for (int loopIn=0; loopIn<52; loopIn++) {
            cin >> temp;
            if (temp=="#") return 0;
            (stacks[loopIn]).push_front(temp);
        }
        while (1) {
            curSize=stacks.size();
            if (compTo-3>=0&&compTo<curSize) {
                if ((((stacks[compTo]).front())[0]==((stacks[compTo-3]).front())[0]) ||
                    (((stacks[compTo]).front())[1]==((stacks[compTo-3]).front())[1])) {
                    temp=(stacks[compTo]).front();
                    (stacks[compTo]).pop_front();
                    (stacks[compTo-3]).push_front(temp);
                    atLeast1Swap=true;
                    if ((stacks[compTo]).empty()) {
                        stacks.erase(stacks.begin()+compTo);
                    }
                    compTo=1;
                    continue;
                }
            }
            if (compTo-1>=0&&compTo<curSize) {
                if ((((stacks[compTo]).front())[0]==((stacks[compTo-1]).front())[0]) ||
                    (((stacks[compTo]).front())[1]==((stacks[compTo-1]).front())[1])) {
                    temp=(stacks[compTo]).front();
                    (stacks[compTo]).pop_front();
                    (stacks[compTo-1]).push_front(temp);
                    if ((stacks[compTo]).empty()) {
                        stacks.erase(stacks.begin()+compTo);
                    }
                    compTo=1;
                    atLeast1Swap=true;
                    continue;
                }
            }
            curSize=stacks.size();
            compTo++;
            if (curSize==1) {
                cout << "1 pile remaining: 52" << endl;
                break;
            }
            else if ((compTo>=curSize)) {
                cout << curSize << " piles remaining: ";
                for (int loopListOut=0; loopListOut<curSize; loopListOut++) {
                    cout << " " << (stacks[loopListOut]).size();
                }
                cout << endl;
                break;
            }
        }
    }
    return 0;
}
Last edited on
If you want others to read your code you need to space it out better and stop massively overusing parentheses, which just produces clutter.

Anyway, the problem is a harder than you think. If you move a card over 3 positions then you need to check not only if it can move again from that position leftwards, but also if the card to its right might be able to move now, etc., etc.

You're not using the atLeast1Swap and atLeast1Additional variables. You set atLeast1Swap but don't actually use it anywhere.

Don't use global variables unless there's a good reason to.

temp is a meaningless turd of a name that should be avoided whenever possible. In your case, you could call it card instead.
Hi, in the parts of the code (detection if card has a move)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while(1) {
curSize=stacks.size();
if (compTo-3>=0&&compTo<curSize) {
                if ((((stacks[compTo]).front())[0]==((stacks[compTo-3]).front())[0]) ||
                    (((stacks[compTo]).front())[1]==((stacks[compTo-3]).front())[1])) {
                    temp=(stacks[compTo]).front();
                    (stacks[compTo]).pop_front();
                    (stacks[compTo-3]).push_front(temp);
                    atLeast1Swap=true;
                    if ((stacks[compTo]).empty()) {
                        stacks.erase(stacks.begin()+compTo);
                    }
                    compTo=1;
                    continue; // here I restart the search for moves from 1 if a card swaps
                }
            }
//...
compTo++;
}


is there something I'm doing wrong when restarting the check for moves?
Topic archived. No new replies allowed.