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
/*
    Krzysztof Dziembała
*/
#include <iostream>
#include "message.h"
#include "krazki.h"

int main(){
    int cnode = MyNodeId();
    int height = PipeHeight();
    int discsNum = NumberOfDiscs();
    if(discsNum > height){ //if there are more discs than max height write 0 and exit
        if(cnode == 0){
            std::cout << "0\n";
        }
        return 0;
    }
    if(cnode >= discsNum) return 0; //exit if there are more nodes than discs
    int non = NumberOfNodes();
    int startLP = (cnode * discsNum) / non; //start disc for current node
    int endLP = ((cnode + 1) * discsNum) / non; //first disc for next node
    int discPos = 0; //how far last disc checked by this node can go
    for(int i = startLP; i < endLP; i++){
        long long int currDiscDiam = DiscDiameter(i+1);
        for(int a = 1; a <= height; a++){
            if(a < discPos){
                if(currDiscDiam >=  HoleDiameter(a)){
                    discPos = a; //discPos++ would be good too
                } else {
                    break;
                }
            } else {
                break;
            }
        }
    }
    int finalPos = discPos; //needed only for node 0 (but not a problem for others)
    if(cnode != 0){ //send result and number of checked discs to node number 0
        PutInt(0, discPos);
        PutInt(0, (endLP - startLP));
        Send(0);
    } else { //receive results on node 0
        for (int i = 1; i < NumberOfNodes(); ++i) {
            int fromNode = Receive(i);
            int recPos = GetInt(fromNode);
            int checked = GetInt(fromNode);
            if(recPos <= (finalPos - checked)){
                finalPos = recPos;
            } else {
                finalPos -= checked;
            }
        }
        if(finalPos < 0)
            finalPos = 0;
        std::cout << finalPos << '\n';
    }
    return 0;
}