Team Tic Tac Toe solution

Question:

Translation:

Some cows are playing a game:
They wrote some letters on a 3 * 3 blackboard
The first letters of these cows are A Z
If the same letters on the blackboard can be linked into a row / column / slant, then the cow with the initials of this letter wins
But it's too hard to win, so the game allows two people to work together
If there are two letters that can form a row / column / slant, then the two cows cooperate to win

Input:

Nine letters in three lines and three columns

Output:

The first line is an integer representing the number of cows that can win alone
The second line is an integer, which represents how many pairs of cattle can cooperate to win

Topic analysis:

This topic is not difficult, but the code is relatively large
At the same time, we should pay attention to several points:

  • Cows that have been able to win do not need to be recalculated
    such as
    XXX
    XXX
    XXX
    So the answer is
    1
    0
  • Cannot hold thigh, one line / one column / one slant, need to contain two letters at the same time, can be regarded as cooperation win, XXX such A line can not be regarded as X and A cooperation win

Code:

#include<stdio.h>

char b[9], ans1, ans2;
bool cow[26] ,cows[26][26];

void cmp(char c1, char c2, char c3){
    if(c1 == c2 && c2 == c3 && c3 == c1){
        if(!cow[c1-65]){
            cow[c1-65] = 1;
            ans1++;
        }
    }
    if(c1 == c2 && c1 != c3){
        if(!cows[c1-65][c3-65]){
            cows[c1-65][c3-65] = cows[c3-65][c1-65] = 1;
            ans2++;
        }
    }
    if(c1 == c2 && c1 != c3){
        if(!cows[c1-65][c3-65]){
            cows[c1-65][c3-65] = cows[c3-65][c1-65] = 1;
            ans2++;
        }
    }
    if(c1 == c3 && c1 != c2){
        if(!cows[c1-65][c2-65]){
            cows[c1-65][c2-65] = cows[c2-65][c1-65] = 1;
            ans2++;
        }
    }
    if(c3 == c2 && c1 != c3){
        if(!cows[c1-65][c3-65]){
            cows[c1-65][c3-65] = cows[c3-65][c1-65] = 1;
            ans2++;
        }
    }
}

int main(){
    for(int i = 0; i < 9; i++)  scanf(" %c", b+i);
    ans1 = 0; ans2 = 0;
    cmp(b[0],b[1],b[2]);
    cmp(b[3],b[4],b[5]);
    cmp(b[6],b[7],b[8]);

    cmp(b[0],b[3],b[6]);
    cmp(b[1],b[4],b[7]);
    cmp(b[2],b[5],b[8]);

    cmp(b[0],b[4],b[8]);
    cmp(b[2],b[4],b[6]);

    printf("%d\n%d\n", ans1, ans2);
}

Posted on Wed, 04 Dec 2019 15:35:27 -0500 by mie