Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include<cstdio>
  2. #include<iostream>
  3. #include<vector>
  4. #include<set>
  5. #include<map>
  6. #include<string>
  7. #include<stack>
  8. #include<queue>
  9. #include<cstdlib>
  10. #include<cmath>
  11. #include<utility>
  12. #include<algorithm>
  13. using namespace std;
  14. #define lli long long int
  15. #define endl "\n"
  16. int mv[]={0,0,1,-1};
  17. int mh[]={1,-1,0,0};
  18. int graph[21][21];
  19. int ds[11][11];
  20. int caminhos[10];
  21. void bfs(int suj1, map<int,int> &sujeira, int n, int m, int inicio)
  22. {
  23. vector<bool> usados(n*m,false);
  24. int mv[]={0,0,1,-1};
  25. int mh[]={1,-1,0,0};
  26. queue<int> filona;
  27. filona.push(suj1);
  28. vector<int> distancia(n*m,0);
  29. while(!filona.empty())
  30. {
  31. int at=filona.front();
  32. usados[at]=true;
  33. filona.pop();
  34. int x=at%n;
  35. int y=at/n;
  36. for(int i=0;i<4;i++)
  37. {
  38. int atx=x+mh[i];
  39. int aty=y+mv[i];
  40. if(atx >= 0 and atx < n and aty >= 0 and aty < m)
  41. {
  42. int f= atx + aty*n;
  43. if(graph[aty][atx]==1) continue;
  44. if(!usados[f] and graph[aty][atx]==0)
  45. {
  46. filona.push(f);
  47. distancia[f]=distancia[at]+1;
  48. if(sujeira.count(f)!=0)
  49. {
  50. ds[sujeira[suj1]][sujeira[f]]=distancia[f];
  51. ds[sujeira[f]][sujeira[suj1]]=distancia[f];
  52. }
  53. if(f==inicio)
  54. {
  55. ds[sujeira[suj1]][0]=ds[0][sujeira[suj1]]=distancia[f];
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. int fb(int n)
  63. {
  64. int menor=10000;
  65. do {
  66. int ini=0;
  67. int aux=0;
  68. int prox=0;
  69. for(int i=0;i<n;i++)
  70. {
  71. prox=ds[ini][caminhos[i]];
  72. aux+=prox;
  73. ini=caminhos[i];
  74. }
  75. menor=min(menor,aux);
  76. }while(next_permutation(caminhos,caminhos+n));
  77. return menor;
  78. }
  79. int main()
  80. {
  81. ios::sync_with_stdio(false);
  82. cin.tie(0); cout.tie(0);
  83. int n,m;
  84. while(1)
  85. {
  86. cin>>n>>m;
  87. if(n==0 and m==0) break;
  88. int inicio=0;
  89. map<int,int> sujeira;
  90. int k=1;
  91. for(int i=0;i<m;i++)
  92. {
  93. for(int j=0;j<n;j++)
  94. {
  95. char p;
  96. cin>>p;
  97. if(p=='o')
  98. {
  99. graph[i][j]=0;
  100. inicio= i*n+j;
  101. }
  102. if(p=='*')
  103. {
  104. int a=0;
  105. graph[i][j]=0;
  106. a= i*n + j;
  107. sujeira.insert(make_pair(a,k));
  108. k++;
  109. }
  110. if(p=='x')
  111. {
  112. graph[i][j]=1;
  113. }
  114. if(p=='.')
  115. graph[i][j]=0;
  116. }
  117. }
  118. bool taerrado=false;
  119. if(sujeira.size()==0)
  120. {
  121. cout<<"0"<<endl;
  122. continue;
  123. }
  124. map<int,int>::iterator it;
  125. for(int i=0;i<=sujeira.size();i++)
  126. for(int j=0;j<=sujeira.size();j++) ds[i][j]=0;
  127. for(it= sujeira.begin() ; it!= sujeira.end() ; ++it)
  128. {
  129. bfs(it->first, sujeira, n,m,inicio);
  130. }
  131. /* for(int i=0;i<=sujeira.size();i++)
  132. {
  133. for(int j=0;j<=sujeira.size();j++)
  134. {
  135. cout<<ds[i][j]<<" ";
  136. }
  137. cout<<endl;
  138. }*/
  139.  
  140. int v=0;
  141. for(int i=0;i<=sujeira.size();i++)
  142. {
  143. v=0;
  144. for(int j=0;j<=sujeira.size();j++)
  145. {
  146. if(ds[i][j]==0)
  147. v++;
  148. }
  149. if(v>1)
  150. {
  151. taerrado=true;
  152. break;
  153. }
  154. }
  155. if(!taerrado)
  156. {
  157. for(int i=0;i<sujeira.size();i++) caminhos[i]=i+1;
  158. cout<<fb(sujeira.size())<<endl;
  159. }
  160. else cout<<"-1"<<endl;
  161. }
  162.  
  163. return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement