Team Tic Tac Toe

题目

  • Problem Description
    Farmer John owns 26 cows, which by happenstance all have names starting with different letters of the alphabet, so Farmer John typically refers to each cow using her first initial – a character in the range A…Z.

    The cows have recently become fascinated by the game of tic-tac-toe, but since they don’t like the fact that only two cows can play at a time, they have invented a variant where multiple cows can play at once! Just like with regular tic-tac-toe, the game is played on a 3×3 board, only instead of just Xs and Os, each square is marked with a single character in the range A…Z to indicate the initial of the cow who claims that square.

    An example of a gameboard might be:
                COW
                XXO
                ABC

    The cows fill in each of the nine squares before they become confused about how to figure out who has won the game. Clearly, just like with regular tic-tac-toe, if any single cow has claimed an entire row, column, or diagonal, that cow could claim victory by herself. However, since the cows think this might not be likely given the larger number of players, they decide to allow cows to form teams of two, where a team of two cows can claim victory if any row, column, or diagonal consists only of characters belonging to the two cows on the team, and moreover if characters from both cows (not just one) are used in this row, column, or diagonal.

    Please help the cows figure out how many individuals or two-cow teams can claim victory. Note that the same square on the game board might possibly be usable in several different claims to victory.

  • Input
    The input consists of three lines, each of which is three characters in the range A…Z.

  • Output
    Output should consist of two lines. On the first line, output the number of individual cows who can claim victory. On the second line, output the number of two-cow teams that could claim victory.

  • input
    COW
    XXO
    ABC

  • output
    0
    2

题意

井字棋乱斗,不同的牛下的棋子用不同的字母表示.
获胜条件:
三个相同的字母连成了三个(行,列,对角线),则视为个人获胜。两个不同种的字母连成了三个,则视为组队获胜.

分别求出获胜的个人和队伍数.

思路

列举遍历所有情况。。
需要把已经获胜的人或队伍标记起来,防止重复计算.

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    char m[6][6];
    scanf("%s",&m[1][1]);
    scanf("%s",&m[2][1]);
    scanf("%s",&m[3][1]);

    int win_one=0;
    int vis[256]= {0};
    int win_two=0;
    int vis2[66000]= {0};
//个人队伍
    for(int i=1; i<=3; ++i)
        if(m[i][1]==m[i][2] and m[i][2]==m[i][3])
            if(vis[m[i][1]]==0)
                win_one++,vis[m[i][1]]=m[i][1];
    for(int i=1; i<=3; ++i)
        if(m[1][i]==m[2][i] and m[2][i]==m[3][i])
            if(vis[m[1][i]]==0)
                win_one++,vis[m[1][i]]=m[1][i];
    if(m[1][1]==m[2][2] and m[2][2]==m[3][3])
        if(vis[m[1][1]]==0)
            win_one++,vis[m[1][1]]=m[1][1];
    if(m[1][3]==m[2][2] and m[2][2]==m[3][1])
        if(vis[m[1][3]]==0)
            win_one++,vis[m[1][3]]=m[1][3];
//组队获胜
    int t;
    for(int i=1; i<=3; ++i) {
        if(m[i][1]==m[i][2] and m[i][1]!=m[i][3]) {
            t=m[i][1]*m[i][3];
            if(vis2[ t ] == 0)
                win_two++,vis2[ t ] = t;
        }
        if(m[i][2]==m[i][3] and m[i][1]!=m[i][2]) {
            t=m[i][1]*m[i][2];
            if(vis2[ t ] == 0)
                win_two++,vis2[ t ] = t;
        }
        if(m[i][1]==m[i][3] and m[i][1]!=m[i][2]) {
            t=m[i][1]*m[i][2];
            if(vis2[ t ] == 0)
                win_two++,vis2[ t ] = t;
        }
    }
    for(int i=1; i<=3; ++i) {
        if(m[1][i]==m[2][i] and m[1][i]!=m[3][i]) {
            t=m[1][i]*m[3][i];
            if(vis2[ t ] == 0)
                win_two++,vis2[ t ] = t;
        }
        if(m[2][i]==m[3][i]and m[1][i]!=m[2][i]) {
            t=m[1][i]*m[2][i];
            if(vis2[ t ] == 0)
                win_two++,vis2[ t ] = t;
        }
        if(m[1][i]==m[3][i]and m[1][i]!=m[2][i]) {
            t=m[1][i]*m[2][i];
            if(vis2[ t ] == 0)
                win_two++,vis2[ t ] = t;
        }
    }
    if( m[1][1] == m[2][2] and m[1][1]!=m[3][3]) {
        t=m[1][1]*m[3][3];
        if(vis2[t]==0)
            win_two++,vis2[t]=t;
    }
    if( m[1][1] == m[3][3] and m[1][1]!=m[2][2]) {
        t=m[1][1]*m[2][2];
        if(vis2[t]==0)
            win_two++,vis2[t]=t;
    }
    if( m[2][2] == m[3][3] and m[1][1]!=m[3][3]) {
        t=m[1][1]*m[3][3];
        if(vis2[t]==0)
            win_two++,vis2[t]=t;
    }
    if( m[1][3] == m[2][2] and m[1][3]!=m[3][1]) {
        t=m[1][3]*m[3][1];
        if(vis2[t]==0)
            win_two++,vis2[t]=t;
    }
    if( m[3][1] == m[2][2] and m[3][1]!=m[1][3]) {
        t=m[3][1]*m[1][3];
        if(vis2[t]==0)
            win_two++,vis2[t]=t;
    }
    if( m[1][3] == m[3][1] and m[2][2]!=m[3][1]) {
        t=m[2][2]*m[3][1];
        if(vis2[t]==0)
            win_two++,vis2[t]=t;
    }
    cout << win_one<<endl<<win_two<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值