FOJ Problem 2283 Tic-Tac-Toe(暴力枚举)——第八届福建省大学生程序设计竞赛-重现赛
2017-07-24 19:41
423 查看
传送门
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!”
题目大意:
有两个人要玩游戏,游戏规则如下:
一个人用 X , 另一个人用 O, 如果一个人有一行或者一列或者对角线都是相同元素,那么这个人就胜出了,现在给定一个状态,让你求在这个状态下,你在走两步,能不能赢(俩个人都是采用的最优策略)
解题思路:
暴力枚举。首先枚举一遍,观察能不能只走一步就胜出。如果能胜出,那么直接输出结果;否则就在枚举把当前为 . 的换成需要替换的元素,枚举另一个人的最优选择,如果没有最优选择随机一个为 . 的元素,然后在进行判断就OK了,就是代码稍微麻烦了一点,不过还是很好想的。
代码:
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!”
题目大意:
有两个人要玩游戏,游戏规则如下:
一个人用 X , 另一个人用 O, 如果一个人有一行或者一列或者对角线都是相同元素,那么这个人就胜出了,现在给定一个状态,让你求在这个状态下,你在走两步,能不能赢(俩个人都是采用的最优策略)
解题思路:
暴力枚举。首先枚举一遍,观察能不能只走一步就胜出。如果能胜出,那么直接输出结果;否则就在枚举把当前为 . 的换成需要替换的元素,枚举另一个人的最优选择,如果没有最优选择随机一个为 . 的元素,然后在进行判断就OK了,就是代码稍微麻烦了一点,不过还是很好想的。
代码:
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <math.h> #include <algorithm> using namespace std; const int MAXN = 10; char g[MAXN][MAXN]; int check(char ch){ if(g[0][0]==ch && g[1][1]==ch && g[2][2]==ch) return 1; if(g[0][3]==ch && g[1][1]==ch && g[3][0]==ch) return 1; if(g[0][0]==ch && g[0][1]==ch && g[0][2]==ch) return 1; if(g[1][0]==ch && g[1][1]==ch && g[1][2]==ch) return 1; if(g[2][0]==ch && g[2][1]==ch && g[2][2]==ch) return 1; if(g[0][0]==ch && g[1][0]==ch && g[2][0]==ch) return 1; if(g[0][1]==ch && g[1][1]==ch && g[2][1]==ch) return 1; if(g[0][2]==ch && g[1][2]==ch && g[2][2]==ch) return 1; return 0; } int main() { int T; scanf("%d", &T); while(T--){ char ch; for(int i=0; i<3; i++) for(int j=0; j<3; j++) cin>>ch, g[i][j] = ch; cin>>ch; int ok = 0; for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ if(g[i][j] == '.'){ g[i][j] = ch; if(check(ch)){ ok = 1; goto A; } g[i][j] = '.'; } } } A:; if(ok) puts("Kim win!"); else { ok = 0; for(int i=0; i<3; i++){ for(int j=0; j<3; j++){ if(g[i][j] == '.'){ g[i][j] = ch; int ta = -1, tb = -1; for(int ii=0; ii<3; ii++){ for(int jj=0; jj<3; jj++){ if(g[ii][jj] == '.'){ g[ii][jj] = ch; if(check(ch)){ ta = ii, tb = jj; goto B; } g[ii][jj] = '.'; } } } for(int ii=0; ii<3; ii++){ for(int jj=0; jj<3; jj++){ if(g[ii][jj] == '.'){ ta = ii, tb = jj; goto B; } } } B:; if(ch == 'o') g[ta][tb] = 'x'; else if(ch == 'x') g[ta][tb] = 'o'; for(int iii=0; iii<3; iii++){ for(int jjj=0; jjj<3; jjj++){ if(g[iii][jjj] == '.'){ g[iii][jjj] = ch; if(check(ch)){ ok = 1; goto C; } g[iii][jjj] = '.'; } } } g[ta][tb] = '.'; g[i][j] = '.'; } } } C:; if(ok) puts("Kim win!"); else puts("Cannot win!"); } } return 0; } /** _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . __ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / / ======`-.____`-.___\_____/___.-`____.-'====== `=---=' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 佛祖保佑 每次AC **/
相关文章推荐
- 第八届福建省大学生程序设计竞赛 L.Tic-Tac-Toe【思维+暴力枚举】
- 第八届福建省大学生程序设计竞赛-重现赛 L Tic-Tac-Toe
- 第八届福建省大学生程序设计竞赛-重现赛I Magic
- 第八届福建省大学生程序设计竞赛-重现赛 A Frog
- 第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)
- 7.22.17 第八届福建省大学生程序设计竞赛-重现赛
- 第八届福建省大学生程序设计竞赛-重现赛(F Problem 2277 Change)DFS序+树状数组
- 第八届福建省大学生程序设计竞赛-重现赛个人题解
- 第八届福建大学生程序设计竞赛-L Tic-Tac-Toe
- 第八届福建省大学生程序设计竞赛-重现赛(A+D+K+L)
- FZU2272+Frog+签到题+第八届福建省大学生程序设计竞赛
- 第八届福建省大学生程序设计竞赛 省赛回忆
- 【HDU5928 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 G】【计算几何 凸包思想 枚举底点做DP】Birthday Gift 给定绳长最多围住多少个点
- 第八届福建省大学生程序设计竞赛 FZU 2273 Triangles (计算几何)
- 第八届福建省大学生程序设计竞赛 FZU 2277 Change (dfs序+树状数组)
- FZU Problem 2221 RunningMan(思维考查)——第六届福建省大学生程序设计竞赛-重现赛
- 第八届福建省大学生程序设计竞赛
- 第八届福建省大学生程序设计竞赛-K Wand(错位+组合数)
- FZU Problem 2213 Common Tangents(简单几何,分类讨论)——第六届福建省大学生程序设计竞赛-重现赛
- 第八届福建省大学生程序设计竞赛 D.Game【思维+KMP】