24h購物| | PChome| 登入
2012-12-20 18:56:44| 人氣2,155| 回應0 | 上一篇 | 下一篇

[UVA][井字遊戲盤面判斷] 10363 - Tic Tac Toe

推薦 0 收藏 0 轉貼0 訂閱站台

Problem B: Tic Tac Toe

Tic Tac Toe is a child's game played on a 3 by 3 grid. One player, X, starts by placing an X at an unoccupied grid position. Then the other player, O, places an O at an unoccupied grid position. Play alternates between X and O until the grid is filled or one player's symbols occupy an entire line (vertical, horizontal, or diagonal) in the grid.

We will denote the initial empty Tic Tac Toe grid with nine dots. Whenever X or O plays we fill in an X or an O in the appropriate position. The example below illustrates each grid configuration from the beginning to the end of a game in which X wins.

...  X..  X.O  X.O  X.O  X.O  X.O  X.O
...  ...  ...  ...  .O.  .O.  OO.  OO.
...  ...  ...  ..X  ..X  X.X  X.X  XXX
Your job is to read a grid and to determine whether or not it could possibly be part of a valid Tic Tac Toe game. That is, is there a series of plays that can yield this grid somewhere between the start and end of the game?

The first line of input contains N, the number of test cases. 4N-1 lines follow, specifying N grid configurations separated by empty lines. For each case print "yes" or "no" on a line by itself, indicating whether or not the configuration could be part of a Tic Tac Toe game.

Sample Input

2
X.O
OO.
XXX

O.X
XX.
OOO

Output for Sample Input

yes
no

一定由 X 先下,然後換 O。
no 的情況
1. 不可能 X win, O win
2. X win 時 x == o
3. O win 時 x == o+1

#include <stdio.h>
int check(char g[][4]) {
int i, j, o = 0, x = 0;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
if(g[i][j] == 'O')
o++;
else if(g[i][j] == 'X')
x++;
if(!(x == o || x == o+1))
return 0;
int oflag = 0, xflag = 0;
for(i = 0; i < 3; i++) {
if(g[i][0] == 'O' && g[i][0] == g[i][1] && g[i][1] == g[i][2])
oflag = 1;
if(g[0][i] == 'O' && g[0][i] == g[1][i] && g[1][i] == g[2][i])
oflag = 1;
if(g[i][0] == 'X' && g[i][0] == g[i][1] && g[i][1] == g[i][2])
xflag = 1;
if(g[0][i] == 'X' && g[0][i] == g[1][i] && g[1][i] == g[2][i])
xflag = 1;
}
if(g[0][0] == 'O' && g[0][0] == g[1][1] && g[1][1] == g[2][2])
oflag = 1;
if(g[2][0] == 'O' && g[2][0] == g[1][1] && g[1][1] == g[0][2])
oflag = 1;
if(g[0][0] == 'X' && g[0][0] == g[1][1] && g[1][1] == g[2][2])
xflag = 1;
if(g[2][0] == 'X' && g[2][0] == g[1][1] && g[1][1] == g[0][2])
xflag = 1;
if(oflag && xflag)
return 0;
if(!oflag && !xflag)
return 1;
if(xflag && x == o+1)
return 1;
if(oflag && x == o)
return 1;
return 0;
}
int main() {
int t;
char g[4][4];
scanf("%d", &t);
while(t--) {
scanf("%s%s%s", g[0], g[1], g[2]);
if(check(g))
puts("yes");
else
puts("no");
}
return 0;
}
 

台長: Morris
人氣(2,155) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][樹走訪] 372 - WhatFix Notation
此分類上一篇:[UVA][floyd] 1056 - Degrees of Separation

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文