1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <iostream>
#include <utility>
#include <vector>
class Board
{
public:
typedef enum {s_empty=0, s_isx=1, s_iso=2} Square;
private:
int board_;
Square nextPlayer() const {
int played = 0;
int temp = board_;
while(temp>0) {
if(temp%3 != 0) ++played;
temp /= 3;
}
if(played%2 == 0) return s_isx;
return s_iso;
}
public:
Board() : board_(0) {}
Board(const std::string &s) {
Board b;
std::vector<std::pair<int,int> > x,o;
for(int row=0;row!=3;++row) {
for(int col=0;col!=3;++col) {
int idx = row*3+col;
if(s[idx] == 'x') {
x.push_back(std::pair<int,int>(row,col));
}
if(s[idx] == 'o') {
o.push_back(std::pair<int,int>(row,col));
}
}
}
while(!x.empty()) {
b.move(x.back().first, x.back().second);
x.pop_back();
if(o.empty()) continue;
b.move(o.back().first, o.back().second);
o.pop_back();
}
*this = b;
}
Square operator()(int row, int col) const {
int idx = row*3 + col;
int mask = 1;
while(--idx >= 0) mask *= 3;
return Square((board_/mask)%3);
}
bool lost() {
Square nm = nextPlayer();
nm = Square(3 - int(nm));
Board &b = *this;
if(b(0,0) == nm && b(0,1) == nm && b(0,2) == nm) return true;
if(b(1,0) == nm && b(1,1) == nm && b(1,2) == nm) return true;
if(b(2,0) == nm && b(2,1) == nm && b(2,2) == nm) return true;
if(b(0,0) == nm && b(1,0) == nm && b(2,0) == nm) return true;
if(b(0,1) == nm && b(1,1) == nm && b(2,1) == nm) return true;
if(b(0,2) == nm && b(1,2) == nm && b(2,2) == nm) return true;
if(b(0,0) == nm && b(1,1) == nm && b(2,2) == nm) return true;
if(b(2,0) == nm && b(1,1) == nm && b(0,2) == nm) return true;
return false;
}
void move(int row, int col) {
int nm = nextPlayer();
int idx = row*3 + col;
while(--idx >= 0) nm *= 3;
board_ += nm;
}
operator int () const {
return board_;
}
static std::pair<int, int> findMove(Board b) {
static bool calculated[20000];
static bool canWin[20000];
static std::pair<int,int> move[20000];
bool init = false;
if(!init) {
for(int i=0;i!=20000;++i) calculated[i] = false;
init = true;
}
if(calculated[b]) return move[b];
std::pair<int,int> &m = move[b];
if(b.lost()) {
m.first = -1;
m.second = -1;
canWin[b] = false;
calculated[b] = true;
return m;
}
for(int row=0;row!=3;++row) {
for(int col=0;col!=3;++col) {
if(b(row,col) == Board::s_empty) {
Board temp = b;
temp.move(row, col);
findMove(temp);
if(canWin[temp] == false) {
m.first = row;
m.second = col;
canWin[b] = true;
calculated[b] = true;
return m;
}
}
}
}
m.first = -1;
m.second = -1;
canWin[b] = false;
calculated[b] = true;
return m;
}
};
int main() {
std::string board;
std::getline(std::cin, board);
Board b(board);
std::pair<int, int> move = Board::findMove(b);
std::cout << move.first << " " << move.second << std::endl;
return 0;
}
|