Guest User

Untitled

a guest
Oct 13th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | Software | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // A graf csucsainak a szama
  5. #define V 3
  6.  
  7. void printMegoldas(int szin[]);
  8.  
  9. // Megnezi ha a graph okes vagy sem
  10. bool ezBiztonsagos(int v, bool graph[V][V], int szin[], int c)
  11. {
  12. for (int i = 0; i < V; i++)
  13. if (graph[v][i] && c == szin[i])
  14. return false;
  15.  
  16. return true;
  17. }
  18.  
  19. //Segedfunkcio a szinezes megoldasara
  20. bool graphSzinezes(bool graph[V][V], int m, int szin[],
  21. int v)
  22. {
  23.  
  24. //Ha minden csucs szint rendel akkor igazat kapunk
  25. if (v == V)
  26. return true;
  27.  
  28. //tekintsuk a csucsot v-nek es probaljunk ki mas szineket
  29. for (int c = 1; c <= m; c++) {
  30.  
  31. //Megvizsgalja c-tol v ig a szineket hogy rendben van
  32. if (ezBiztonsagos(v, graph, szin, c)) {
  33. szin[v] = c;
  34.  
  35. //ugyan ugy a tobbi csucsra is
  36. if (graphSzinezes(graph, m, szin, v + 1)
  37. == true)
  38. return true;
  39.  
  40. //Ha a c-hez nem kap megoldast akkor tavolitsa el
  41. szin[v] = 0;
  42. }
  43. }
  44.  
  45. //Ha nem lehet szint hozza rendelni hamis erteket terit vissza
  46. return false;
  47. }
  48.  
  49. //
  50. bool graphSzin(bool graph[V][V], int m)
  51. {
  52.  
  53. // Alapbol minden szin erteke 0
  54. // Szukseges ahhoz hogy az ezbiztonsagos reszlet helyesen mukodjon
  55. int szin[V];
  56. for (int i = 0; i < V; i++)
  57. szin[i] = 0;
  58.  
  59. // Meghivja a graph szinezest az elso elre
  60. if (graphSzinezes(graph, m, szin, 0) == false) {
  61. cout << "Nincs megoldas";
  62. return false;
  63. }
  64.  
  65. // Kiirja a megoldast
  66. printMegoldas(szin);
  67. return true;
  68. }
  69.  
  70. //Kiiratas
  71. void printMegoldas(int szin[])
  72. {
  73. cout << "Talalt megoldast: \n "
  74. << "A szinezes a kovetkezo:"
  75. << "\n";
  76. for (int i = 0; i < V; i++)
  77. cout << " " << szin[i] << " ";
  78.  
  79. cout << "\n";
  80. }
  81.  
  82.  
  83. int main()
  84. {
  85.  
  86. bool graph[V][V] = {
  87. { 0, 1, 1,},
  88. { 1, 0, 1,},
  89. { 1, 1, 0,},
  90.  
  91. };
  92.  
  93. // A szinek szama
  94. int m = 4;
  95. graphSzin(graph, m);
  96. return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment