Find the Winning Move
总时间限制: 3000ms 内存限制: 65536kB
描述
4x4 tic-tac-toe is played on a board with four rows (numbered 0 to 3 from top to bottom) and four columns (numbered 0 to 3 from left to right). There are two players, x and o, who move alternately with x always going first. The game is won by the first player to get four of his or her pieces on the same row, column, or diagonal. If the board is full and neither player has won then the game is a draw.
Assuming that it is x’s turn to move, x is said to have a forced win if x can make a move such that no matter what moves o makes for the rest of the game, x can win. This does not necessarily mean that x will win on the very next move, although that is a possibility. It means that x has a winning strategy that will guarantee an eventual victory regardless of what o does.
Your job is to write a program that, given a partially-completed game with x to move next, will determine whether x has a forced win. You can assume that each player has made at least two moves, that the game has not already been won by either player, and that the board is not full.
输入
The input contains one or more test cases, followed by a line beginning with a dollar sign that signals the end of the file. Each test case begins with a line containing a question mark and is followed by four lines representing the board; formatting is exactly as shown in the example. The characters used in a board description are the period (representing an empty space), lowercase x, and lowercase o. For each test case, output a line containing the (row, column) position of the first forced win for x, or ‘#####’ if there is no forced win. Format the output exactly as shown in the example.
输出
For this problem, the first forced win is determined by board position, not the number of moves required for victory. Search for a forced win by examining positions (0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), …, (3, 2), (3, 3), in that order, and output the first forced win you find. In the second test case below, note that x could win immediately by playing at (0, 3) or (2, 0), but playing at (0, 1) will still ensure victory (although it unnecessarily delays it), and position (0, 1) comes first.
样例输入
?
…
.xo.
.ox.
…
?
o…
.ox.
.xxx
xooo
$
样例输出
(0,1)
#include <limits.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <cmath>
#define MAXN 10000000
using namespace std;
int step = 0;//当step == 16 终结
bool flag = 0;
char grid[4][4] = {};
struct Move{
int x;
int y;
}ans = {};
int judge(){
int col_x[4] = {}, row_x[4] = {}, slope_x[2] = {};
int col_o[4] = {}, row_o[4] = {}, slope_o[2] = {};
for(int i = 0; i < 4; ++i){//每一行
if(grid[i][i] == 'x')
++ slope_x[0];
if(slope_x[0] == 4)
return 1;//x方win
if(grid[i][i] == 'o')
++ slope_o[0];
if(slope_o[0] == 4)
return -1;//o方win
if(grid[i][3-i] == 'x')
++ slope_x[1];
if(slope_x[1] == 4)
return 1;//x方win
if(grid[i][3-i] == 'o')
++ slope_o[1];
if(slope_o[1] == 4)
return -1;//o方win
for(int j = 0; j < 4; ++j){//各列
if(grid[i][j] == 'x')
++ row_x[i];
if(grid[i][j] == 'o')
++ row_o[i];
if(grid[j][i] == 'x')
++ col_x[i];
if(grid[j][i] == 'o')
++ col_o[i];
}
if(row_x[i] == 4)
return 1;
if(row_o[i] == 4)
return -1;
}
for(int i = 0; i < 4; ++i){
if(col_x[i] == 4)
return 1;
if(col_o[i] == 4)
return -1;
}
return 0;
}//判断是否终局,0平,1赢,-1输
//必胜局面是,接下来自己下自己优势最大步骤,对方下它的优势最大步骤,最终是我获胜
//0下x,1下o
int search(int inf, int sup, int depth){
if(depth == 16)
return 0;
int result = judge();
if(result)
return result;
if(depth%2){//该o方下
for(int i = 0; i < 4; ++i){
for(int j = 0; j < 4; ++j){
if(grid[i][j] != '.')
continue;
grid[i][j] = 'o';
result = search(inf, sup, depth+1);
grid[i][j] = '.';
if(result < sup)
sup = result;
if(inf >= sup)
return inf;
}
}
return sup;
}
else{//该x方下
for(int i = 0; i < 4; ++i){
for(int j = 0; j < 4; ++j){
if(grid[i][j] != '.')
continue;
grid[i][j] = 'x';
result = search(inf, sup, depth+1);
grid[i][j] = '.';
if(result > inf){
inf = result;
ans.x = i;
ans.y = j;
}
if(inf >= sup)
return inf;
}
}
return inf;
}
}
int main(){
while(1){
char signal = 0;
cin >> signal;
if(signal == '$')
break;
step = 0;
flag = 0;
ans = {};
memset(grid, 0, sizeof(grid));
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j){
cin>>grid[i][j];
if(grid[i][j] != '.')
step ++;
}
if(step <= 4){
cout << "#####" << endl;
continue;
}
int result = search(-1, 1, step);
if(result > 0)
cout <<'('<<ans.x<<','<<ans.y<<')'<<endl;
else
cout << "#####" << endl;
}
return 0;
}