Informatică, întrebare adresată de cristinnav36, 8 ani în urmă

Problema colorarii hartilor in c++


cristinnav36: Doresc o rezolvare a acestei probleme

Răspunsuri la întrebare

Răspuns de andrei750238
7

Cod C++:

#include <iostream>

using namespace std;

int n, v[20], a[20][20], gasit = 0;

int solutie(int k) {

   if (k == n + 1) return 1;

   return 0;

}

void tipar() {

   int i;

   for (i = 1; i <= n; i++) {

       cout << endl;

       cout << i << " - > ";

       if (v[i] == 1) cout << "verde";

       if (v[i] == 2) cout << "albastru";

       if (v[i] == 3) cout << "mov";

       if (v[i] == 4) cout << "galben";

   }

}

void init(int k) {

   v[k] = 0;

}

int succesor(int k) {

   if (v[k] < 4) {

       v[k]++;

       return 1;

   }

   return 0;

}

int valid(int k) {

   int i, j;

   for (i = 1; i < k; i++) {

       if (a[i][k] && v[i] == v[k]) return 0;

   }

   return 1;

}

void back(int k) {

   if (solutie(k)) {

       gasit = 1;

       tipar();

   }

   else {

       while (succesor(k)) {

           if (valid(k) && !gasit) back(k + 1);

       }

   }

}

int main()

{

   int i, x, y;

   cout << "Nr tari :\n";

   cin >> n;

   cout << "Perechi de vecini :\n";

   cin >> x >> y;

   a[x][y] = 1;

   while (x != 0) {

       cin >> x >> y;

       a[x][y] = 1;

       a[y][x] = 1;

   }

   back(1);

}

Anexe:

cristinnav36: Multumesc mult
Alte întrebări interesante