其他分享
首页 > 其他分享> > 3C - Tic-tac-toe

3C - Tic-tac-toe

作者:互联网

原题链接https://codeforces.com/problemset/problem/3/C

题目本身不难,但是细节很多

题意:给你一个井字棋盘,要求你判断其状态

思路:按照规则判断即可,难的是不合法状态考虑不全,先手和后手赢的时候,两个人棋盘上的棋子关系是固定的。

代码如下

char g[5][5];
bool flag = false;	//判断是否合法

void judge(char c, int &win)
{
	if(c == 'X')
	{
		if(win == 2) flag = true;
		win = 1;
		return;
	}
	if(c == '0')
	{
		if(win == 1) flag = true;
		win = 2;
	}	
}

int main()
{
	IOS;
	for(int i = 0 ; i < 3 ; i ++)	cin >> g[i];
	int x = 0, z = 0; 
	int win = 0;
	for(int i = 0 ; i < 3 ; i ++)
		if(g[i][0] == g[i][1] && g[i][1] == g[i][2])
			judge(g[i][0], win);
			
	for(int i = 0 ; i < 3 ; i ++)
		if(g[0][i] == g[1][i] && g[1][i] == g[2][i])
			judge(g[0][i], win);

	if(g[0][0] == g[1][1] && g[1][1] == g[2][2])
		judge(g[0][0], win);
	if(g[0][2] == g[1][1] && g[1][1] == g[2][0])
		judge(g[0][2], win);
	
	for(int i = 0 ; i < 3 ; i ++)
		for(int j = 0 ; j < 3 ; j ++)
		{
			if(g[i][j] == 'X') x ++;
			else if(g[i][j] == '0') z ++;
		}
	if(abs(z - x) > 1 || z > x) flag = true;
	if(win == 2 && z != x) flag = true;
	if(win == 1 && x != z + 1) flag = true;
	
	if(flag) cout << "illegal" << endl;
	else if(win)
	{
		if(win == 1) cout << "the first player won" << endl;
		else cout << "the second player won" << endl;
	}
	else if(z + x == 9) cout << "draw" << endl;
	else
	{
		if(x > z) cout << "second" << endl;
		else cout << "first" << endl;
	}
	
	return 0;
}

标签:judge,int,win,++,flag,&&,tac,3C,toe
来源: https://www.cnblogs.com/luoyicong/p/14613011.html