Codeforces 3C. Tic-tac-toe
2014-12-07 14:14
190 查看
C. Tic-tac-toe
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always
draws crosses, the other — noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor
0s form the required line, a draw is announced.
You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print
one of the verdicts below:
illegal — if the given board layout can't appear during a valid game;
the first player won — if in the given board layout the first player has just won;
the second player won — if in the given board layout the second player has just won;
draw — if the given board layout has just let to a draw.
Input
The input consists of three lines, each of the lines contains characters ".", "X"
or "0" (a period, a capital letter X, or a digit zero).
Output
Print one of the six verdicts: first, second, illegal, the
first player won, the second player won or draw.
Sample test(s)
input
output
代码:
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 100005;
int chessBoard[4][4];
int Test(int x1, int x2, int x3)
{
if(x2 == x1 && x2 == x3)
return x2;
return 0;
}
int main()
{
#ifdef _LOCAL
freopen("F://input.txt", "r", stdin);
#endif
int countX = 0, countO = 0;
for(int i = 1; i <= 3; ++i)
{
for(int j = 1; j <= 3; ++j)
{
char ch;
scanf("%c", &ch);
if(ch == 'X')
{
countX++;
chessBoard[i][j] = 1;
}
else if(ch == '0')
{
countO++;
chessBoard[i][j] = 2;
}
}
getchar();
}
if(countO > countX || countX - countO > 1)
{
printf("illegal\n");
return 0;
}
vector<int> ans;
for(int i = 1; i <= 3; ++i)
{
ans.push_back(Test(chessBoard[i][1], chessBoard[i][2], chessBoard[i][3]));
ans.push_back(Test(chessBoard[1][i], chessBoard[2][i], chessBoard[3][i]));
}
ans.push_back(Test(chessBoard[1][1], chessBoard[2][2], chessBoard[3][3]));
ans.push_back(Test(chessBoard[1][3], chessBoard[2][2], chessBoard[3][1]));
sort(ans.begin(), ans.end());
int last = ans.size() - 1;
if(ans[last - 1] == 1 && ans[last] == 2 || ans[last] == 2 && countX > countO || ans[last] == 1 && countX == countO)
{
printf("illegal\n");
return 0;
}
if(ans[last] == 1)
printf("the first player won\n");
else if(ans[last] == 2)
printf("the second player won\n");
else if(countX + countO == 9)
printf("draw\n");
else if(countX == countO)
printf("first\n");
else
printf("second\n");
return 0;
}
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always
draws crosses, the other — noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor
0s form the required line, a draw is announced.
You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print
one of the verdicts below:
illegal — if the given board layout can't appear during a valid game;
the first player won — if in the given board layout the first player has just won;
the second player won — if in the given board layout the second player has just won;
draw — if the given board layout has just let to a draw.
Input
The input consists of three lines, each of the lines contains characters ".", "X"
or "0" (a period, a capital letter X, or a digit zero).
Output
Print one of the six verdicts: first, second, illegal, the
first player won, the second player won or draw.
Sample test(s)
input
X0X .0. .X.
output
second
代码:
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN = 100005;
int chessBoard[4][4];
int Test(int x1, int x2, int x3)
{
if(x2 == x1 && x2 == x3)
return x2;
return 0;
}
int main()
{
#ifdef _LOCAL
freopen("F://input.txt", "r", stdin);
#endif
int countX = 0, countO = 0;
for(int i = 1; i <= 3; ++i)
{
for(int j = 1; j <= 3; ++j)
{
char ch;
scanf("%c", &ch);
if(ch == 'X')
{
countX++;
chessBoard[i][j] = 1;
}
else if(ch == '0')
{
countO++;
chessBoard[i][j] = 2;
}
}
getchar();
}
if(countO > countX || countX - countO > 1)
{
printf("illegal\n");
return 0;
}
vector<int> ans;
for(int i = 1; i <= 3; ++i)
{
ans.push_back(Test(chessBoard[i][1], chessBoard[i][2], chessBoard[i][3]));
ans.push_back(Test(chessBoard[1][i], chessBoard[2][i], chessBoard[3][i]));
}
ans.push_back(Test(chessBoard[1][1], chessBoard[2][2], chessBoard[3][3]));
ans.push_back(Test(chessBoard[1][3], chessBoard[2][2], chessBoard[3][1]));
sort(ans.begin(), ans.end());
int last = ans.size() - 1;
if(ans[last - 1] == 1 && ans[last] == 2 || ans[last] == 2 && countX > countO || ans[last] == 1 && countX == countO)
{
printf("illegal\n");
return 0;
}
if(ans[last] == 1)
printf("the first player won\n");
else if(ans[last] == 2)
printf("the second player won\n");
else if(countX + countO == 9)
printf("draw\n");
else if(countX == countO)
printf("first\n");
else
printf("second\n");
return 0;
}
相关文章推荐
- CodeForces 3C---Tic-tac-toe--思维题
- codeforces 3C. Tic-tac-toe
- CodeForces 3C-Tic-tac-toe
- codeforces - 3C - Tic-tac-toe
- CodeForces - 907B Tic-Tac-Toe(模拟题)
- [Codeforces]C. Tic-tac-toe
- Codeforces 3 C. Tic-tac-toe
- CF_3C_Tic-tac-toe
- 第八届福建省大学生程序设计竞赛 L.Tic-Tac-Toe【思维+暴力枚举】
- poj 2361 Tic Tac Toe
- 【FZU - 2283 Tic-Tac-Toe】 模拟
- [cf]Tic-tac-toe
- Tic-Tac-Toe(三子连)(总结规律)
- Analysis On Tic-Tac-Toe
- 纯C++游戏编程: Tic-Tac-Toe(三连棋游戏)的实现
- POJ 2361 Tic Tac Toe
- checkio (tic-tac-toe)
- uva 11623 - Tic Tac Toe(game)
- UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)
- uva 11534 - Say Goodbye to Tic-Tac-Toe(Nim和)