Source file: | win.{c, cpp, java, pas} |
Input file: | win.in |
Output file: | win.out |
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 file 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.
Example input:
? .... .xo. .ox. .... ? o... .ox. .xxx xooo $
Example output:
##### (0,1)4子棋,DP可以搞定+回溯#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <string> using namespace std; char mat[4][4]; int nowx(); bool nowo(); char check(){ for(int i = 0; i < 4; i++){ if(mat[i][0] != ‘.‘ ){ bool flag = 0; for(int j = 0; j < 3; j++){ if(mat[i][j] != mat[i][j+1]){ flag = 1; break; } } if(!flag) return mat[i][0]; } } for(int i = 0; i < 4; i++){ if(mat[0][i] != ‘.‘ ){ bool flag = 0; for(int j = 0; j < 3; j++){ if(mat[j][i] != mat[j+1][i]){ flag = 1; break; } } if(!flag) return mat[0][i]; } } if(mat[0][0] != ‘.‘){ bool flag = 0; for(int i = 0; i < 3; i++){ if(mat[i][i] != mat[i+1][i+1]){ flag = 1; break; } } if(!flag) return mat[0][0]; } if(mat[3][0] != ‘.‘){ if(mat[3][0]== mat[2][1] && mat[2][1] == mat[1][2] && mat[1][2] ==mat[0][3])return mat[3][0]; } return 0; } bool nowo(){ for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++){ if(mat[i][j] == ‘.‘){ mat[i][j] = ‘o‘; if(check()==‘o‘|| nowx()<0){ mat[i][j] = ‘.‘; return 1; } mat[i][j] = ‘.‘; } } return 0; } int nowx(){ for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++){ if(mat[i][j] == ‘.‘){ mat[i][j] = ‘x‘; if(check() == ‘x‘ || !nowo()){ mat[i][j] = ‘.‘; return i<<8|j; } mat[i][j] = ‘.‘; } } return -1; } int main(){ char t; while(cin >> t && t != ‘$‘){ for(int i = 0; i < 4; i++) for(int j = 0; j < 4; j++) cin >> mat[i][j]; int ans = nowx(); if(ans==-1){ cout<<"#####"<<endl; }else{ printf("(%d,%d)\n",ans >> 8 ,ans&0xff); } } return 0; }
UVa10111 - Find the Winning Move,布布扣,bubuko.com
UVa10111 - Find the Winning Move
原文:http://blog.csdn.net/mowayao/article/details/22725371