Programozás‎ > ‎Feladatok‎ > ‎Mastermind‎ > ‎Megoldás‎ > ‎

ve_master.cpp

Letöltés: ve_master.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int tippek[4096][6];
int tippekLength = 0;
int jok[4096];
int jokLength = 0;
int eredmenyek[2];
void test(int* tipp, int* titok) {
bool occured[6] = {false, false, false, false, false, false};
int matching = 0, matching2 = 0;
for (int i = 0; i < 4; i++) {
if (tipp[i] == titok[i]) {
if (occured[tipp[i]-1]) {
continue;
} else {
occured[tipp[i]-1] = true;
}
matching++;
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (tipp[i] == titok[j] && i != j) {
if (occured[tipp[i]-1]) {
continue;
} else {
occured[tipp[i]-1] = true;
}
matching2++;
break;
}
}
}
eredmenyek[0] = matching;
eredmenyek[1] = matching2;
}

int main(int argc, char **argv) {
string line;
int tippdb;
ifstream file(argv[1]);
if (file.is_open()) {
getline(file,line);
tippdb = atoi(line.c_str());
int i = 0;
int a, b, c, d, e, f;
while (file.good() && i<tippdb) {
getline(file,line);
sscanf(line.c_str(), "%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f);
tippek[i][0] = a;
tippek[i][1] = b;
tippek[i][2] = c;
tippek[i][3] = d;
tippek[i][4] = e;
tippek[i][5] = f;
tippekLength++;
i++;
}
file.close();
} else {
return 0;
}
int lehValasz[4];
for (int a = 1; a < 7; a++) {
lehValasz[0] = a;
for (int b = 1; b < 7; b++) {
if (b == a) {
continue;
}
lehValasz[1] = b;
for (int c = 1; c < 7; c++) {
if (c == b || c == a) {
continue;
}
lehValasz[2] = c;
for (int d = 1; d < 7; d++) {
if (d == c || d == b || d == a) {
continue;
}
lehValasz[3] = d;
for (int i = 0; i < tippekLength; i++) {
test(tippek[i], lehValasz);
if (eredmenyek[0] == tippek[i][4] && eredmenyek[1] == tippek[i][5]) {
if (i+1 == tippekLength) {
jok[jokLength] =                                       
                                    lehValasz[3]+lehValasz[2]*10+
                                    lehValasz[1]*100+lehValasz[0]*1000;
jokLength++;
}
} else {
break;
}
}
}
}
}
}
ofstream oFile(argv[2]);
oFile << jokLength << endl;
for (int i = 0; i < jokLength; i++) {
oFile << jok[i] << endl;
}
oFile.close();
return 0;
}