Tic-Tac-Toe (eighth session of Fujian Provincial Competition)

Source: Internet
Author: User
Tags printf time limit
problem 2283 Tic-Tac-Toe Accept:2 Submit:3
Time limit:1000 mSec Memory limit:262144 KB
problem Description

Kim likes to play tic-tac-toe.

Given A current state, and now Kim was going to take his next move. Please tell Kim if he can win the game in next 2 moves if both player is clever enough.

Here "Next 2 moves" means Kim ' s 2 move. (Kim Move,opponent move, Kim Move, stop).

Game Rules:

Tic-Tac-Toe (also known as noughts and Crosses or Xs and Os) is a paper-and-pencil game for both players, X and O, who take Turns marking the spaces in a 3x3 grid. The player succeeds in placing three of their marks in a horizontal, vertical, or diagonal row wins the game. Input

First line contains an integer t (1≤t≤10), represents there is t test cases.

For each test Case:each test case contains three lines, each line three string ("O" or "X" or ".") (all lower case letters.)

X means here is a X

O means here is a O

. Means here's a blank place.

Next Line A string ("O" or "x") means Kim was ("O" or "X") and he is going to take his next move. Output

For each test case:

If Kim can win in 2 steps, output "Kim win!"

Otherwise output "Cannot win!" Sample Input 3.. .... oo x oo. XX x oxo x.. O.. . XO Sample Output cannot win! Kim win! Kim win! Source

Eighth Fujian University College student Program Design Competition-Replay (thanks to the contractor Xiamen Polytechnic Institute)


At first feel too much trouble, so did not write this problem, the results after the game, review, found that is a water problem, violent search is good, only 3*3 lattice;

Discover or lack of experience, water problems can not be found.

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <queue
> Using namespace std;
Char map[4][4];
int sum;
int flag;
int ans;
Char A, B; int isOK (char c) {for (int i=0; i<3; i++) {if (map[0][i]==c&&map[1][i]==c&&map[2][i]==c) re       Turn 1;
    Column if (map[i][0]==c&&map[i][1]==c&&map[i][2]==c) return 1;
    } if (map[0][0]==c&&map[1][1]==c&&map[2][2]==c) return 1;           if (map[2][0]==c&&map[1][1]==c&&map[0][2]==c) return 1;
Diagonal return 0;
    } void Dfs () {ans=0;
            for (int i=0, i<3; i++) {for (int j=0; j<3; J + +) {if (map[i][j]!= '. ') continue;
            Map[i][j]=a;
            if (isOK (a)) ans++;
            Map[i][j]= '. ';          if (ans>=2) return;
    There are more than two places for a to win, then a must win}} int main () {int T;
    scanf ("%d", &t);
        while (t--) {flag=0; for (int i=0;i<3;
            i++) {for (int j=0; j<3; J + +) {scanf ("%s", &map[i][j]);
        } getchar ();
        } scanf ("%c", &a);
        if (a== ' o ') b= ' x ';
        else b= ' o ';
            The situation one, already can win at the beginning; if (isOK (a)) {printf ("Kim win!\n");
        Continue }//Case two: Only one can win for (int i=0; i<3; i++) {for (int j=0; j<3; J + +) {if (MA p[i][j]!= '. ')
                Continue
                Map[i][j]=a;
                    if (isOK (a)) {flag=1;
                Break
            } map[i][j]= '. ';
        } if (flag) break;
            } if (flag) {printf ("Kim win!\n");
        Continue
        }//Case three; b only one step can win, so to block this position, and then judge whether there are more than two positions to win, there is a chance to win; ans=0;
                for (int i=0, i<3; i++) for (int j=0; j<3; J + +) {if (map[i][j]!= '. ') continue; MapI
                [J]=b;
                    if (isOK (b)) {flag=1;
                    Map[i][j]=a;
                    DFS ();
                    if (ans>=2) flag=2;

                Map[i][j]= '. ';

            } map[i][j]= '. ';
            } if (flag) {if (flag==2) {printf ("Kim win!\n");
            } else printf ("Cannot win!\n");

        Continue
                }//Is not the above special case, the direct search, see if there can be practiced into line; for (int i=0; i<3; i++) {for (int j=0; j<3; J + +) {
                ans=0;
                    if (map[i][j]!= '. ')
                Continue
                    else {map[i][j]=a;
                    DFS ();
                Map[i][j]= '. ';
            } if (ans>=2) break;
        } if (ans>=2) break;
        } if (ans>=2) printf ("Kim win!\n");
    else printf ("Cannot win!\n");
} return 0;
 }


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.