Advertisement
dowolf

gofish.cpp

May 7th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. //sandbox file
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stddef.h>
  5. #include <string>
  6. #include <vector>
  7. #include <map>
  8. #include <unordered_map>
  9. #include <iostream>
  10. #include <sstream>
  11. #include <algorithm>
  12.  
  13. using std::string;
  14. using std::vector;
  15. using std::unordered_map;
  16. using std::ostringstream;
  17.  
  18. typedef unordered_map<string, double> dictType;
  19. #define DEFAULT_NUM_SCHOOLS 2
  20. #define SCHOOL_SIZE 4
  21.  
  22. string toString(vector<int>& currentData) {
  23. ostringstream oss;
  24. for (int i = 0; i < currentData.size(); i++) {
  25. oss << currentData[i];
  26. }
  27. return oss.str();
  28. }
  29.  
  30. bool isSet(vector<int>& currentData, int i) {
  31. if (currentData[i / SCHOOL_SIZE] > (i % SCHOOL_SIZE)) {
  32. return true;
  33. }
  34. return false;
  35. }
  36.  
  37. void setValue(vector<int>& currentData, int i) {
  38. currentData[i / SCHOOL_SIZE]++;
  39. if (currentData[i / SCHOOL_SIZE] > SCHOOL_SIZE) {
  40. std::cout << "ERROR: SCHOOL CHOSEN TOO OFTEN" << std::endl;
  41. string temp;
  42. std::cin >> temp;
  43. }
  44. }
  45.  
  46. double recursiveCall(vector<int>& currentData, dictType& savedValues) {
  47. //sort in descending order for canonical version of currentData
  48. std::sort(currentData.begin(), currentData.end(), std::greater<int>());
  49. //see if we've encountered it before
  50. string hash = toString(currentData);
  51. if (savedValues.count(hash)) {
  52. return savedValues[hash];
  53. }
  54.  
  55. double result = 0;
  56.  
  57. int firstUnsetNode = -1;
  58. for (int i = 0; i < currentData.size() * SCHOOL_SIZE; i++) {
  59. if (!isSet(currentData, i)) {
  60. firstUnsetNode = i;
  61. break;
  62. }
  63. }
  64.  
  65. if (firstUnsetNode == -1) {
  66. //all nodes chosen, so result is 1
  67. result = 1;
  68. } else {
  69. setValue(currentData, firstUnsetNode);
  70.  
  71. int i = firstUnsetNode + 1;
  72. while (i % SCHOOL_SIZE != 0) i++; //can't match w/i school
  73. for ( ; i < currentData.size() * SCHOOL_SIZE; i++) {
  74. if (!isSet(currentData, i)) {
  75. vector<int> workingData = currentData;
  76. setValue(workingData, i);
  77. result += recursiveCall(workingData, savedValues);
  78. }
  79. }
  80. //assert: if no legal assignments, result is 0
  81. }
  82.  
  83. // std::cout << "Assigning value for " << hash << " of " << result << std::endl;
  84. savedValues[hash] = result;
  85. return result;
  86. }
  87.  
  88. int main(int argc, char** argv) {
  89. dictType savedValues;
  90. int numSchools;
  91. if (argc == 2) {
  92. numSchools = atoi(argv[1]);
  93. } else {
  94. numSchools = DEFAULT_NUM_SCHOOLS;
  95. }
  96.  
  97. vector<int> currentData(numSchools, 0);
  98. double solutionCount = recursiveCall(currentData, savedValues);
  99.  
  100. std::cout << "Numerator: " << solutionCount << std::endl;
  101. double divisor = 1;
  102. for (int i = SCHOOL_SIZE * numSchools - 1; i > 0; i-=2) {
  103. divisor *= i;
  104. }
  105. double probability = solutionCount / divisor;
  106. std::cout << "Probability: " << 1 - probability << std::endl;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement