Guest User

Untitled

a guest
May 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <array>
  4. #include <queue>
  5. #include <algorithm>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. vector<array<int, 7>> expandir(array<int, 7> no){
  11. vector<array<int, 7>> sucessores;
  12. array<int, 7> aux;
  13. aux = no;
  14.  
  15. if (aux[0] == 0 && aux[1] == 0 && aux[2] == 0 && aux[3] == 0 && aux[4] == 3 && aux[5] == 3 && aux[6] == -1){ //Se 4 n�o tiver cheio, pode encher
  16. aux = {0,0,1,1,2,2,0};
  17. sucessores.push_back(aux);
  18. return sucessores;
  19. }
  20.  
  21. if (aux[0] == 0 && aux[1] == 1 && aux[2] == 1 && aux[3] == 0 && aux[4] == 2 && aux[5] == 2 && aux[6] == 1){ //Se 4 n�o tiver cheio, pode encher
  22. aux = {0,1,0,2,3,0,0};
  23. sucessores.push_back(aux);
  24. return sucessores;
  25. }
  26.  
  27. if (aux[0] == 0 && aux[1] == 2 && aux[2] == 0 && aux[3] == 1 && aux[4] == 3 && aux[5] == 0 && aux[6] == 1){ //Se 4 n�o tiver cheio, pode encher
  28. aux = {0,2,2,0,1,1,0};
  29. sucessores.push_back(aux);
  30. return sucessores;
  31. }
  32.  
  33. if (aux[0] == 1 && aux[1] == 1 && aux[2] == 1 && aux[3] == 1 && aux[4] == 1 && aux[5] == 1 && aux[6] == 1){ //Se 4 n�o tiver cheio, pode encher
  34. aux = {1,1,2,0,0,2,0};
  35. sucessores.push_back(aux);
  36. return sucessores;
  37. }
  38.  
  39. if (aux[0] == 3 && aux[1] == 0 && aux[2] == 0 && aux[3] == 1 && aux[4] == 0 && aux[5] == 2 && aux[6] == 1){ //Se 4 n�o tiver cheio, pode encher
  40. aux = {3,0,0,2,0,1,0};
  41. sucessores.push_back(aux);
  42. return sucessores;
  43. }
  44.  
  45. if (aux[0] == 3 && aux[1] == 1 && aux[2] == 0 && aux[3] == 1 && aux[4] == 0 && aux[5] == 1 && aux[6] == 1){ //Se 4 n�o tiver cheio, pode encher
  46. aux = {3,1,0,2,0,0,0};
  47. sucessores.push_back(aux);
  48. return sucessores;
  49. }
  50.  
  51. if (aux[0] == 0 && aux[1] == 0 && aux[2] == 1 && aux[3] == 1 && aux[4] == 2 && aux[5] == 2 && aux[6] == 0){ //Se 4 n�o tiver cheio, pode encher
  52. aux = {0,1,1,0,2,2,1};
  53. sucessores.push_back(aux);
  54. return sucessores;
  55. }
  56.  
  57. if (aux[0] == 0 && aux[1] == 1 && aux[2] == 0 && aux[3] == 2 && aux[4] == 3 && aux[5] == 0 && aux[6] == 0){ //Se 4 n�o tiver cheio, pode encher
  58. aux = {0,2,0,1,3,0,1};
  59. sucessores.push_back(aux);
  60. return sucessores;
  61. }
  62.  
  63. if (aux[0] == 0 && aux[1] == 2 && aux[2] == 2 && aux[3] == 0 && aux[4] == 1 && aux[5] == 1 && aux[6] == 0){ //Se 4 n�o tiver cheio, pode encher
  64. aux = {1,1,1,1,1,1,1};
  65. sucessores.push_back(aux);
  66. return sucessores;
  67. }
  68.  
  69. if (aux[0] == 1 && aux[1] == 1 && aux[2] == 2 && aux[3] == 0 && aux[4] == 0 && aux[5] == 2 && aux[6] == 0){ //Se 4 n�o tiver cheio, pode encher
  70. aux = {3,0,0,1,0,2,1};
  71. sucessores.push_back(aux);
  72. return sucessores;
  73. }
  74.  
  75. if (aux[0] == 3 && aux[1] == 0 && aux[2] == 0 && aux[3] == 2 && aux[4] == 0 && aux[5] == 1 && aux[6] == 0){ //Se 4 n�o tiver cheio, pode encher
  76. aux = {3,1,0,1,0,1,1};
  77. sucessores.push_back(aux);
  78. return sucessores;
  79. }
  80.  
  81. if (aux[0] == 3 && aux[1] == 1 && aux[2] == 0 && aux[3] == 2 && aux[4] == 0 && aux[5] == 0 && aux[6] == 0){ //Se 4 n�o tiver cheio, pode encher
  82. aux = {3,3,0,0,0,0,-1};
  83. sucessores.push_back(aux);
  84. return sucessores;
  85. }
  86.  
  87. return sucessores;
  88. }
  89.  
  90. bool testarObjetivo(vector<array<int, 7>> solucao){
  91.  
  92. for(vector<array<int, 7>>::size_type i = 0; i != solucao.size(); i++){
  93. if(solucao[i][0] == 3 && solucao[i][1] == 3 && solucao[i][2] == 0 && solucao[i][3] == 0 && solucao[i][4] == 0 && solucao[i][5] == 0){
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99.  
  100. vector<array<int, 7>> buscaEmArvore(array<int, 7> estadoInicial){
  101. queue<array<int, 7>> borda;
  102. vector<array<int, 7>> solucao,resposta;
  103. array<int, 7> no;
  104.  
  105. borda.push(estadoInicial);
  106. resposta.push_back(estadoInicial);
  107.  
  108. while (!borda.empty()){
  109. no = borda.front();
  110. borda.pop();
  111. solucao = expandir(no);
  112. resposta.insert(resposta.end(), solucao.begin(), solucao.end());
  113. if(testarObjetivo(solucao)){
  114. return resposta;
  115. }
  116.  
  117. for(vector<array<int, 7>>::size_type i = 0; i != solucao.size(); i++){
  118. borda.push(solucao[i]);
  119. }
  120. }
  121. solucao.clear();
  122. return resposta;
  123. }
  124.  
  125. struct target_less
  126. {
  127. template<class It>
  128. bool operator()(It const &a, It const &b) const { return *a < *b; }
  129. };
  130. struct target_equal
  131. {
  132. template<class It>
  133. bool operator()(It const &a, It const &b) const { return *a == *b; }
  134. };
  135. template<class It> It uniquify(It begin, It const end)
  136. {
  137. std::vector<It> v;
  138. v.reserve(static_cast<size_t>(std::distance(begin, end)));
  139. for (It i = begin; i != end; ++i)
  140. { v.push_back(i); }
  141. std::sort(v.begin(), v.end(), target_less());
  142. v.erase(std::unique(v.begin(), v.end(), target_equal()), v.end());
  143. std::sort(v.begin(), v.end());
  144. size_t j = 0;
  145. for (It i = begin; i != end && j != v.size(); ++i)
  146. {
  147. if (i == v[j])
  148. {
  149. using std::iter_swap; iter_swap(i, begin);
  150. ++j;
  151. ++begin;
  152. }
  153. }
  154. return begin;
  155. }
  156.  
  157. int main()
  158. {
  159. array<int, 7> problema = {0,0,0,0,3,3,-1};
  160. vector<array<int, 7>> resposta = buscaEmArvore(problema);
  161. resposta.erase(uniquify(resposta.begin(), resposta.end()), resposta.end());
  162. for(vector<array<int, 7>>::size_type i = 0; i != resposta.size(); i++){
  163. switch(resposta[i][6])
  164. {
  165. case -1:
  166. cout<<"Estao "<<resposta[i][0]<<" missionarios e "<<resposta[i][1]<<" canibais na margem esquerda e "<<resposta[i][4]<<" missionarios e "<<resposta[i][5]<<" canibais na margem direita."<<endl;
  167. break;
  168. case 0:
  169. cout<<"MARGEM ESQUERDA: "<<resposta[i][0]<<" missionarios e "<<resposta[i][1]<<" canibais || "<<"<------ BARCO: "<<resposta[i][2]<<" missionarios e "<<resposta[i][3]<<" canibais || "<<"MARGEM DIREITA: "<<resposta[i][4]<<" missionarios e "<<resposta[i][5]<<" canibais"<<endl;
  170. break;
  171. case 1:
  172. cout<<"MARGEM ESQUERDA: "<<resposta[i][0]<<" missionarios e "<<resposta[i][1]<<" canibais || "<<"------> BARCO: "<<resposta[i][2]<<" missionarios e "<<resposta[i][3]<<" canibais || "<<"MARGEM DIREITA: "<<resposta[i][4]<<" missionarios e "<<resposta[i][5]<<" canibais"<<endl;
  173. break;
  174. default:
  175. cout<<"Nao foi poss�vel encontrar resposta"<<endl;
  176. break;
  177. }
  178. cout<<'\n';
  179. }
  180. system("pause");
  181. return 0;
  182. }
Add Comment
Please, Sign In to add comment