make GetProb a function

This commit is contained in:
2023-09-28 11:27:14 +08:00
parent 8221f934bc
commit 34a9278720

View File

@ -252,7 +252,7 @@ std::vector<std::vector<double> > GaussianJordanElimination(
n = equations.size(); n = equations.size();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
int pivot = i; int pivot = i;
if(i>=m-1) break; if (i >= m - 1) break;
for (int j = i + 1; j < n; j++) for (int j = i + 1; j < n; j++)
if (abs(equations[j][i]) > abs(equations[pivot][i])) pivot = j; if (abs(equations[j][i]) > abs(equations[pivot][i])) pivot = j;
std::swap(equations[i], equations[pivot]); std::swap(equations[i], equations[pivot]);
@ -421,6 +421,13 @@ std::pair<int, int> TotalRandomGuess() {
} }
return std::make_pair(row, column); return std::make_pair(row, column);
} }
inline double GetProb(double default_p,const std::vector<double> &ps)
{
if(ps.empty()) return default_p;
double res=0;
for(int i=0;i<ps.size();i++) res+=ps[i];
return res/ps.size();
}
/** /**
* @brief The definition of function SimpleGuess() * @brief The definition of function SimpleGuess()
* *
@ -441,7 +448,7 @@ std::pair<int, int> SimpleGuess() {
} }
if (total_known > 5) if (total_known > 5)
default_probability = (double)(total_known_with_mine) / (total_known); default_probability = (double)(total_known_with_mine) / (total_known);
// if((double)(total_known)/(rows*columns)<0.3) return TotalRandomGuess(); // if((double)(total_known)/(rows*columns)<0.15) return TotalRandomGuess();
for (int i = 0; i < rows; i++) for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++) for (int j = 0; j < columns; j++)
if (map_status[i][j] == 2) { if (map_status[i][j] == 2) {
@ -474,22 +481,8 @@ std::pair<int, int> SimpleGuess() {
for (int i = 0; i < rows; i++) for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++) for (int j = 0; j < columns; j++)
if (map_status[i][j] == 0) { if (map_status[i][j] == 0) {
double current_prob = default_probability; double current_prob=GetProb(default_probability,probability[best_guess.first][best_guess.second]);
if (probability[best_guess.first][best_guess.second].size() > 0) { double this_prob=GetProb(default_probability,probability[i][j]);
current_prob = 0;
for (int k = 0;
k < probability[best_guess.first][best_guess.second].size(); k++)
current_prob += probability[best_guess.first][best_guess.second][k];
current_prob /=
probability[best_guess.first][best_guess.second].size();
}
double this_prob = default_probability;
if (probability[i][j].size() > 0) {
this_prob = 0;
for (int k = 0; k < probability[i][j].size(); k++)
this_prob += probability[i][j][k];
this_prob /= probability[i][j].size();
}
if (this_prob < current_prob) { if (this_prob < current_prob) {
best_guess.first = i; best_guess.first = i;
best_guess.second = j; best_guess.second = j;