foj Problem 2283 Tic-Tac-Toe
Accept: 60 Submit: 92
Time Limit: 1000 mSec Memory Limit : 262144
KB
Problem Description
Kim likes to play Tic-Tac-Toe.
Given a current state, and now Kim is going to take his next move. Please tell Kim if he can win the game in next 2 moves if both player are clever enough.
Here “next 2 moves” means Kim’s 2 move. (Kim move,opponent move, Kim move, stop).
Game rules:
Tic-tac-toe (also known as noughts and crosses or Xs and Os) is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game.
Input
First line contains an integer T (1 ≤ T ≤ 10), represents there are T test cases.
For each test case: Each test case contains three lines, each line three string(“o” or “x” or “.”)(All lower case letters.)
x means here is a x
o means here is a o
. means here is a blank place.
Next line a string (“o” or “x”) means Kim is (“o” or “x”) and he is going to take his next move.
Output
For each test case:
If Kim can win in 2 steps, output “Kim win!”
Otherwise output “Cannot win!”
Sample Input
3 . . . . . . . . . o o x o o . x x x o x o x . . o . . . x oSample Output
Cannot win! Kim win! Kim win! 題意:kim兩手棋之內能否勝利。 思路:直接暴力搜索即可: 討論這兩手棋,窮搜每一個還未下任何棋的點,若下第一手棋就能形成三子一線,就直接贏了,否則,判斷第二手棋能否贏。 這時換個想法,因為兩方都足夠聰明,如果kim下的第二手棋有不止一種贏的下法,那麽kim一定能贏,因為對手再厲害也只能擋住一種能贏的下法而已(可以忽略對手的下的那手棋,直接討論kim贏的可能數即可) AC代碼:#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<queue> #include<set> #include<vector> #include<cstring> #include<string> #include<bitset> using namespace std; #define INF 0x3f3f3f3f const int N_MAX = 3; char field[N_MAX][N_MAX]; char p; bool judge() { bool flag = 1; if (field[0][0] == p && field[1][1] == p && field[2][2] == p)return true; if (field[2][0] == p && field[1][1] == p && field[0][2] == p)return true; for (int i = 0; i < N_MAX;i++) { for (int j = 0; j < N_MAX;j++) { if (field[i][j] != p) { flag = 0; break; } } if (flag)return true; else flag = 1; } for (int i = 0; i < N_MAX; i++) { for (int j = 0; j < N_MAX; j++) { if (field[j][i] != p) { flag = 0; break; } } if (flag)return true; else flag = 1; } return false; } int main() { int t; scanf("%d",&t); while(t--){ bool flag1 = 0,flag2=0; for (int i = 0; i < N_MAX;i++) { for (int j = 0; j < N_MAX;j++) { scanf(" %c",&field[i][j]); } } scanf(" %c",&p); for (int i = 0; i < N_MAX; i++) { for (int j = 0; j < N_MAX;j++) { if(field[i][j]==‘.‘){ field[i][j] = p; if (judge()) { flag1 = 1; break; }//直接下一步就搞定了 int num = 0; for (int k = 0; k < N_MAX;k++) {//否則下兩步,看看能否贏 for (int l = 0; l < N_MAX;l++) { if (field[k][l] == ‘.‘) { field[k][l] = p; if (judge())num++;//找到一種可行的贏法,贏的可能數加1 if (num >= 2) { flag2 = 1; break; } field[k][l] = ‘.‘; } } if (flag2)break; } field[i][j] = ‘.‘; if (flag2)break; } } if (flag1||flag2)break; } if (flag1 || flag2)printf("Kim win!\n"); else printf("Cannot win!\n"); } return 0; }
foj Problem 2283 Tic-Tac-Toe