3 C Tic-Tac-Toe

Source: Internet
Author: User

Main topic:

Two contestants take turns marking on the matrix of 3*3, a contestant always draws an ' X ', the other player always draws a ' 0 ', who first on the horizontal line or on the vertical lines, or on the diagonal, first complete three points connected in one piece, who wins. The player who draws ' X ' is the first painting, and if the picture is full, it is a draw. Each cell is empty '. ', or ' 0 ', or ' X '. We need to find the next step. The rules are as follows: 1. If this situation is unlikely to occur, it is "illegal" 2. If this is the first player to win, the output is "won" 3. If this is the second player to win, output "the second player Won "4. If the situation is a draw, output" draw ". 5. If the next step is to draw ' X ' down, output ' first '. 6. If next the ' 0 ' is down, output ' second '. ==================================================================== Analysis: There are several illegal cases: 1. The number of x is greater than 0 of the number of more than 12.0 of the number of x than the number of 03.X and 0 while winning the 4.X victory, x than the number of 0 is not 15.0 victory, x than the number of more than 0 is not 0==================================== ==================================================
#include <iostream>#include<cmath>#include<algorithm>#include<string>#include<cstring>#include<cstdio>#include<vector>#include<cstdlib>using namespacestd;typedef __int64 LL;ConstLL INF =0xFFFFFF;Const intMAXN =100005;ConstLL MOD = 1e9+7;#defineIllegal 0///Judging the situation is illegal#defineFirst 1///The first person.#defineSecond 2///The second person.#defineDraw 3///Draw#defineTFPW 4///This situation just came out F won#defineTSPW 5///This situation just came out S wonCharmaps[5][5];BOOLOk (intXinty) {    returnx>=0&& x<3&& y>=0&& y <3;}BOOLWin (Charch) {     for(intI=0; i<3; i++)    {        if(maps[0][i] = = ch && maps[1][i] = = ch && maps[2][i] = =ch)return true; if(maps[i][0] = = ch && maps[i][1] = = ch && maps[i][2] ==ch)return true; }    if(maps[0][0] = = ch && maps[1][1] = = ch && maps[2][2] ==ch)return true; if(maps[0][2] = = ch && maps[1][1] = = ch && maps[2][0] ==ch)return true; return false;}intsolve () {intNUMX =0, NUM0 =0;  for(intI=0; i<3; i++)     for(intj=0; j<3; J + +)    {        if(Maps[i][j] = ='X') Numx++; if(Maps[i][j] = ='0') NUM0++; }    if( ! (NUMX-NUM0 = =0|| NUMX-NUM0 = =1) || (Win ('X') && Win ('0') ) || (Win ('X') && NUMX-NUM0! =1) || (Win ('0') && NUMX-NUM0! =0))        returnillegal; if(Win ('X'))        returnTfpw; if(Win ('0') && NUMX-NUM0 = =0)        returnTSPW; if(NUM0 = =4&& NUMX = =5)        returnDraw; if(NUMX-NUM0 = =0)        returnFirst ; if(NUMX-NUM0 = =1)        returnSecond; return 0;}intMain () { for(intI=0; i<3; i++) scanf ("%s", Maps[i]); intAns =solve (); if(ans = =illegal) puts ("illegal"); Else if(ans = =First ) puts (" First"); Else if(ans = =Draw) puts ("Draw"); Else if(ans = =Second) puts ("Second"); Else if(ans = =Tfpw) puts ("The first player won"); Else if(ans = =TSPW) puts ("The second player won"); return 0;}

3 C Tic-Tac-Toe

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.