Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

数量,一个赢家,平局时棋盘已满。五子棋的话还要判断赢的方式是不是只有一种

Posted by uuuouou at 2014-12-24 22:46:01 on Problem 3075
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int way[8][3] = {
	//horizonal
	{0, 1, 2},
	{3, 4, 5},
	{6, 7, 8},
	//vertical
	{0, 3, 6},
	{1, 4, 7},
	{2, 5, 8},
	//diagonal
	{0, 4, 8},
	{2, 4, 6}
};
char s[10];
bool check(char c)
{
	int i, j;
	for(i = 0; i < 8; ++i){
		for(j = 0; j < 3; ++j){
			if(s[way[i][j]] != c) break;
		}
		if(j == 3) return true;
	}
	return false;
}

int main()
{
	int x, o;
	while(strcmp(gets(s), "end")){
		x = count(s, s + 9, 'X');
		o = count(s, s + 9, 'O');
		if(x < o || o+1 < x || x < 3) puts("invalid");//invalid count
		else if(x > o && check('X') && !check('O')) puts("valid");//x win
		else if(x == o && check('O') && !check('X')) puts("valid");//o win
		else if(x + o == 9 && !check('X') && !check('O')) puts("valid");//draw
		else puts("invalid");
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator