Algorithm...

closed account (10oTURfi)
The problem I am trying to solve is following (poor translation in 3..2..)

Your task is to spread as much as klingons possible in a hotel of size X*Y such that no klingon can hear each others snoring. Snoring can penetrate C walls (diagonal is 2 walls).
Input: X Y C
Output: Max klingons that can fit in hotel


Idea behind my solution was following:
1) Pick the room which will cause lowest number of other rooms to be affected by noise and put a klingon in it.
2) Repeat 1) untill all rooms are affected by noise

Problem is: Works only on some inputs.

Sample I/O:
I: 5 5 2, O: 6
I: 4 7 3, O: 5
I: 5 7 1, O: 18

What did I do wrong?

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;

#define DEBUG

enum Boja
{
    BIJELA  = 0, // Nije pronađeno
    SIVA    = 1, // Pronađeno
    CRNA    = 2 // Buka
};

struct Vector2i
{
    Vector2i() { }
    Vector2i(int x, int y) : x(x), y(y) { }
    int x;
    int y;
};

struct Node
{
    Node()
    {
        B = BIJELA;
    }
    Boja B;
    int C;
    Vector2i P;
};

struct CompareNode : public binary_function<Node*, Node*, bool>
{
    bool operator()(const Node* pFirst, const Node* pSecond) const
    {
        return pFirst->C > pSecond->C;
    }
};

// Dirty implementation of heap: TODO: replace with a proper one
class Heap : public priority_queue<Node*, vector<Node*>, CompareNode>
{
public:
    void decrease_key(Node* pNode)
    {
        for (int i = 0; i < this->c.size(); ++i)
        {
            if (this->c[i] == pNode)
            {
                push_heap(this->c.begin(), this->c.begin() + i + 1);
                break;
            }
        }
    }
};

class Hotel
{
public:
    Hotel();
    void Rasporedi();
    void NapraviBuku(Node* pIzvor);
    void Relax(Node* pIzvor, Node* pTrenutna);
    void IzracunajCijene();
    int Cijena(Node* pNode);

private:
    vector<vector<Node> > G;
    vector<Node*> L;
    Heap O;
    int X, Y, B, R, LI;
};

Hotel::Hotel() : R(0)
{
    cin >> X >> Y >> B;
    G.resize(Y, vector<Node>(X, Node()));
    L.resize(Y * X);

    for (int y = 0; y < Y; ++y)
        for (int x = 0; x < X; ++x)
            G[y][x].P = Vector2i(x, y);
}

void Hotel::Rasporedi()
{
    Node* pIzvor;

    // Kutevi imaju najmanju cijenu na pocetku
    G[0][0].C = Cijena(&G[0][0]);
    G[Y-1][0].C = Cijena(&G[Y-1][0]);
    G[0][X-1].C = Cijena(&G[0][X-1]);
    G[Y-1][X-1].C = Cijena(&G[Y-1][X-1]);
    O.push(&G[0][0]);
    O.push(&G[Y-1][0]);
    O.push(&G[0][X-1]);
    O.push(&G[Y-1][X-1]);

    while (!O.empty())
    {
        pIzvor = O.top();
        O.pop();
        if (pIzvor->B != CRNA)
        {
            LI = 0;
            NapraviBuku(pIzvor);
            IzracunajCijene();
#ifdef DEBUG
            cout << "Klingonac u sobi: " << pIzvor->P.x << " " << pIzvor->P.y << endl;
#endif
            ++R;
        }
    }

    cout << R << endl;
}

void Hotel::IzracunajCijene()
{
    for (int i = 0; i < LI; ++i)
    {
        L[i]->C = Cijena(L[i]);
        O.push(L[i]);
    }
}

int Hotel::Cijena(Node* pNode)
{
    int Cijena = 0;
    for (int y = pNode->P.y - B; y < pNode->P.y + B; ++y)
        for (int x = pNode->P.x - B; x < pNode->P.x + B; ++x)
            if (x >= 0 && y >= 0 && x < X && y < Y)
                if (G[y][x].B != CRNA)
                    ++Cijena;
    return Cijena;
}

void Hotel::Relax(Node* pIzvor, Node* pTrenutna)
{
    switch (pTrenutna->B)
    {
        case BIJELA:
            pTrenutna->B = SIVA;
            L[LI++] = pTrenutna;
            break;
        case SIVA:
            pTrenutna->C = Cijena(pTrenutna);
            O.decrease_key(pTrenutna);
            break;
        default:
            break;
    }
}

void Hotel::NapraviBuku(Node* pIzvor)
{
    for (int y = pIzvor->P.y - 2 * B; y < pIzvor->P.y + B * 2; ++y)
    {
        for (int x = pIzvor->P.x - 2 * B; x < pIzvor->P.x + 2 * B; ++x)
        {
            if (x >= 0 && y >= 0 && x < X && y < Y)
            {
            	int Udaljenost = abs(x - pIzvor->P.x) + abs(y - pIzvor->P.y);
                if (Udaljenost <= B)
                    G[y][x].B = CRNA;
                else if (Udaljenost <= B * 2)
                	Relax(pIzvor, &G[y][x]);
            }
        }
    }
}

int main()
{
    Hotel Klingonci;
    Klingonci.Rasporedi();
    return 0;
}
I am not sure how others see it, but for me it would help if you could translate the words: Boja, BIJELA , SIVA, CRNA, Rasporedi, NapraviBuku, IzracunajCijene, Cijena .
i will: Boja = Color BIJELA = White SIVA = Grey CRNA = Black Rasporedi = Arange (i dont know if i translated that quite right) NapraviBuku = MakeNoise
IzracunajCijene = CalculatePrices Cijena = Price
closed account (10oTURfi)
That is correct, Program Programmer.
Also: pIzvor = pSource , pTrenutna = pCurrent , Udaljenost = Manhattan Distance (Altho these are obvious from the context)
I would say look at:

int Hotel::Cijena(Node* pNode)
{
int Cijena = 0;
for (int y = pNode->P.y - B; y < pNode->P.y + B; ++y)
for (int x = pNode->P.x - B; x < pNode->P.x + B; ++x)
if (x >= 0 && y >= 0 && x < X && y < Y)
if (G[y][x].B != CRNA)
++Cijena;
//cout << pNode->P.x << " " << pNode->P.y << " " << Cijena << "\n";
return Cijena;
}


In the for loops you need a smaller or equal than sign instead of an equal sign.

int Hotel::Cijena(Node* pNode)
{
int Cijena = 0;
for (int y = pNode->P.y - B; y <= pNode->P.y + B; ++y)
for (int x = pNode->P.x - B; x <= pNode->P.x + B; ++x)
if (x >= 0 && y >= 0 && x < X && y < Y)
if (G[y][x].B != CRNA)
++Cijena;
//cout << pNode->P.x << " " << pNode->P.y << " " << Cijena << "\n";
return Cijena;
}
Topic archived. No new replies allowed.