Created
March 6, 2011 17:16
-
-
Save lw/857413 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
ifstream lettura ("input.txt"); | |
ofstream scrittura ("output.txt"); | |
vector<int> stati; | |
vector<int> scelte; | |
void debug_v (const vector<int>& target) | |
{ | |
for (int i = 0; i < 7; i += 1) | |
{ | |
cout << target[i] << " "; | |
} | |
cout << endl; | |
} | |
vector<int> decode_pos (int code) | |
{ | |
vector<int> result (7, 0); | |
for (int i = 0; i < 7; i += 1) | |
{ | |
result[6-i] = code % 6; | |
code /= 6; | |
} | |
return result; | |
} | |
int encode_pos (const vector<int>& stato) | |
{ | |
int result = 0; | |
for (int i = 0; i < 7; i += 1) | |
{ | |
result *= 6; | |
result += stato[i]; | |
} | |
return result; | |
} | |
int take (vector<int>& tavola, int inizio) | |
{ | |
// cout << "--- called take" << endl; | |
int mano = tavola[inizio]; | |
tavola[inizio] = 0; | |
int posizione = inizio+1; | |
int guadagno = 0; | |
while (mano > 1) | |
{ | |
if (tavola[posizione%7] == 5) | |
{ | |
tavola[posizione%7] -= 1; | |
guadagno += 1; | |
} | |
else | |
{ | |
tavola[posizione%7] += 1; | |
mano -= 1; | |
} | |
posizione += 1; | |
} | |
if (tavola[posizione%7] == 0 || tavola[posizione%7] == 5) | |
{ | |
guadagno -= 1; | |
mano = 0; | |
} | |
else | |
{ | |
guadagno += tavola[posizione%7] + 1; | |
tavola[posizione%7] = 0; | |
mano = 0; | |
} | |
return guadagno; | |
} | |
int valuta (int stato, bool primo) | |
{ | |
// cout << "+++ called valuta " << primo << " " << stato << endl; | |
if (stati[stato] == -22) | |
{ | |
vector<int> tavola = decode_pos (stato); | |
int massimo; | |
if (primo) | |
massimo = -22; | |
else | |
massimo = 22; | |
// cout << " + "; | |
// debug_v (tavola); | |
for (int i = 0; i < 7; i += 1) | |
{ | |
if (tavola[i] != 0) | |
{ | |
vector<int> mossa (tavola); | |
int guadagno = take (mossa, i); | |
int next = valuta (encode_pos (mossa), !primo); | |
if ((primo && guadagno + next > massimo) || (!primo && -guadagno + next < massimo)) | |
{ | |
if (primo) | |
massimo = guadagno + next; | |
else | |
massimo = -guadagno + next; | |
scelte[stato] = i; | |
} | |
} | |
} | |
stati[stato] = massimo; | |
} | |
return stati[stato]; | |
} | |
int main () | |
{ | |
vector<int> start (7, 0); | |
stati.assign (279936, -22); | |
stati[0] = 0; | |
scelte.assign (279936, -1); | |
for (int i = 0; ; i += 1) | |
{ | |
for (int j = 0; j < 7; j += 1) | |
{ | |
cin >> start[j]; | |
} | |
int guadagno = valuta (encode_pos (start), true); | |
cout << scelte[encode_pos (start)]+1 << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment