2.1.4 Healthy Holsteins

这道题比较简单,就是bfs,不过我用了string导致了一些问题,调了我2个小时。终于好了。唉,下次不能粗心啊。
具体代码如下:(自己体会细节)
   
     
  1. /*
  2. ID: awsd1231
  3. PROG: holstein
  4. LANG: C++
  5. */
  6. #include<iostream>
  7. #include<cstdio>
  8. #include<algorithm>
  9. #include<queue>
  10. #include<string>
  11. using namespace std;
  12. int V, eveV[25], G, eveGV[15][25];
  13. //维他命种类数,每种需要的最小量,饲料的种类数,[每种饲料]含[每种维他命]的多少
  14. string ans;
  15. queue<string> may;
  16. string bfs(string x) {
  17. int tmpV[25] = {0};
  18. int len = x.length();
  19. int b = x[len - 1] - '0' + 1;
  20. for(int l = 0; l != len; ++l) {
  21. for(int i = 0; i != V; ++i) {
  22. tmpV[i] += eveGV[x[l] - '0'][i];
  23. }
  24. }
  25. for (int i = b; i != G; ++i) {
  26. for (int j = 0; j != V; ++j) {
  27. if (tmpV[j] + eveGV[i][j] < eveV[j]) {
  28. char c = i + '0';
  29. may.push(x + c);
  30. break;
  31. }
  32. else if (j == V - 1) {
  33. char c = i + '0';
  34. return x + c;
  35. }
  36. }
  37. }
  38. may.pop();
  39. return bfs(may.front());
  40. }
  41. int main () {
  42. freopen("holstein.in", "r", stdin);
  43. freopen("holstein.out", "w", stdout);
  44. scanf("%d", &V);
  45. for (int i = 0; i != V; ++i) {
  46. scanf("%d", &eveV[i]);
  47. }
  48. scanf("%d", &G);
  49. for (int i = 0; i != G; ++i) {
  50. for (int j = 0; j != V; ++j) {
  51. scanf("%d", &eveGV[i][j]);
  52. }
  53. }
  54. for (int i = 0; i != G; ++i) {
  55. for (int j = 0; j != V; ++j) {
  56. if(eveGV[i][j] < eveV[j]) break;
  57. else if(j == V - 1) {
  58. cout << "1" << " " << i + 1 << endl;
  59. return 0;
  60. }
  61. }
  62. }
  63. if(G != 1) {
  64. for(int i = 0; i != G; ++i) {
  65. char c = i + '0';
  66. string t;
  67. may.push(t + c);
  68. }
  69. ans = bfs(may.front());
  70. int len = ans.length();
  71. cout << len << " ";
  72. for (int i = 0; i != len - 1; ++i) {
  73. cout << ans[i] + 1 - '0' << " ";
  74. }
  75. cout << ans[len - 1] + 1 - '0' << endl;
  76. } else cout << "1 1" << endl;
  77. return 0;
  78. }





你可能感兴趣的:(health)