天天看點

Codeforces Beta Round #3C. Tic-tac-toe

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string s1, s2, s3;
bool firstwin, secondwin;

bool ifwin(char c)
{
	if (count(s1.begin(), s1.end(), c) == 3 || count(s2.begin(), s2.end(), c) == 3 || count(s3.begin(), s3.end(), c) == 3) {
		return true;
	}
	if ((s1[0] == c && s2[0] == c && s3[0] == c) || (s1[1] == c && s2[1] == c && s3[1] == c) || (s1[2] == c && s2[2] == c && s3[2] == c)) {
		return true;
	}
	if ((s1[0] == c && s2[1] == c && s3[2] == c) || (s1[2] == c && s2[1] == c && s3[0] == c)) {
		return true;
	}
	return false;
}

int main()
{
	while (cin >> s1 >> s2 >> s3) {
		firstwin = false; secondwin = false;
		firstwin = ifwin('X');
		secondwin = ifwin('0');

		int cntX = count(s1.begin(), s1.end(), 'X') + count(s2.begin(), s2.end(), 'X') + count(s3.begin(), s3.end(), 'X');
		int cnt0 = count(s1.begin(), s1.end(), '0') + count(s2.begin(), s2.end(), '0') + count(s3.begin(), s3.end(), '0');
		int cntd = count(s1.begin(), s1.end(), '.') + count(s2.begin(), s2.end(), '.') + count(s3.begin(), s3.end(), '.');
		

		if ((cntX - cnt0) >= 2 || (cnt0 - cntX) >= 1 || (firstwin == true && secondwin == true) || (secondwin && cntX > cnt0) || (firstwin && cnt0 == cntX)) {
			cout << "illegal" << endl;
			continue;
		}

		if (firstwin) {
			cout << "the first player won" << endl;
			continue;
		}

		if (secondwin) {
			cout << "the second player won" << endl;
			continue;
		}

		if (!firstwin && !secondwin && (cntd == 0)) {
			cout << "draw" << endl;
			continue;
		}
		

		if (cntX == cnt0) {
			cout << "first" << endl;
			continue;
		}

		if ((cntX - cnt0) == 1) {
			cout << "second" << endl;
			continue;
		}
	}
	return 0;
}