FOJ Problem 2283 Tic-Tac-Toe(暴力枚举)——第八届福建省大学生程序设计竞赛-重现赛
来源:互联网 发布:蓝牙单片机 电路图 编辑:程序博客网 时间:2024/05/14 13:15
传送门
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!”
题目大意:
有两个人要玩游戏,游戏规则如下:
一个人用
解题思路:
暴力枚举。首先枚举一遍,观察能不能只走一步就胜出。如果能胜出,那么直接输出结果;否则就在枚举把当前为
代码:
#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**/
- FOJ Problem 2283 Tic-Tac-Toe(暴力枚举)——第八届福建省大学生程序设计竞赛-重现赛
- 第八届福建省大学生程序设计竞赛 L.Tic-Tac-Toe【思维+暴力枚举】
- FZU2283+Tic-Tac-Toe+【思维+暴力枚举】+ 第八届福建省大学生程序设计竞赛
- 第八届福建省大学生程序设计竞赛-重现赛 L Tic-Tac-Toe
- 第八届福建大学生程序设计竞赛-L Tic-Tac-Toe
- Tic-Tac-Toe (第八届福建省赛)
- 第八届福建省大学生程序设计竞赛-重现赛 A Frog
- 第八届福建省大学生程序设计竞赛-重现赛I Magic
- 7.22.17 第八届福建省大学生程序设计竞赛-重现赛
- FZU Problem 2221 RunningMan(思维考查)——第六届福建省大学生程序设计竞赛-重现赛
- 第八届福建省大学生程序设计竞赛-重现赛(F Problem 2277 Change)DFS序+树状数组
- 第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院)
- FZU Problem 2214 Knapsack problem(01背包,超大背包)——第六届福建省大学生程序设计竞赛-重现赛
- FZU Problem 2213 Common Tangents(简单几何,分类讨论)——第六届福建省大学生程序设计竞赛-重现赛
- FZU Problem 2212 Super Mobile Charger(贪心,排序)——第六届福建省大学生程序设计竞赛-重现赛
- FZU Problem 2216 The Longest Straight(二分答案+树状数组)——第六届福建省大学生程序设计竞赛-重现赛
- 第六届福建省大学生程序设计竞赛-重现赛,Problem C Knapsack problem【大背包】
- 第八届福建省大学生程序设计竞赛-重现赛(A+D+K+L)
- table的建立与赋值
- FILE_FLAG_WRITE_THROUGH 和 FILE_FLAG_NO_BUFFERING的区别
- 计算1+2+3……+n要求不能使用乘除法,循环,条件判断,选择相关的关键字
- socket网络编程实现远程备份
- HTML5第十课时,计算个人所得税
- FOJ Problem 2283 Tic-Tac-Toe(暴力枚举)——第八届福建省大学生程序设计竞赛-重现赛
- ApacheMonitor.exe start按钮变暗
- 前端常见的一些算法
- python不是内部命令或外部命令,也不是可执行程序解决方案
- HEX SDUT 3896 17年山东省赛D题
- acm省赛K题--01背包
- HTML5第十课时,计算并显示用户应付的钱数
- jvm查看类的引用及创建
- leetcode --26. Remove Duplicates from Sorted Array