codeforces:Team Tic Tac Toe

C. Team Tic Tac Toe

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

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.

Example

input

COW
XXO
ABC

output

0
2

Note

In this example, no single cow can claim victory. However, if cows C and X team up, they can win via the C-X-C diagonal. Also, if cows X and O team up, they can win via the middle row.

分析

题目很简单,就是让你找出九宫格里能连成一条直线的单个字母或两个字母;
需要注意,如九宫格全为一种字母的情况只算一种,即不作重复统计。坑点就在像 C-X-CX-X-CC-C-X 这些都算是一种情况,那按字典序记录一下,在下一次符合情况的时候就把记录扫一遍看看有没有重复就没问题了。

AC代码:

自己写的垃圾代码,过于难看了

#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<algorithm>
#define MS(X) memset(X,0,sizeof(X))
#define MSC(X) memset(X,-1,sizeof(X))
typedef long long LL;
using namespace std;
char sav[3][4];
bool flag;
int mark[3][3];
int chk[8],chk1[8],chk2[8][3],pt1,pt2;
int save[8][27];
int main(){
    int a1=0,a2=0;pt1=0,pt2=0;
    int v,t,j;
    for(int i=0;i<3;i++)
        scanf("%s",sav[i]);
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
            mark[i][j]=sav[i][j]-'A'+1;
    for(int t=0;t<3;t++){
        for(int i=0;i<3;i++)
            save[t][mark[t][i]]=1;
        for(int i=0;i<3;i++)
            save[t+3][mark[i][t]]=1;
        save[6][mark[t][t]]=1;
        save[7][mark[2-t][t]]=1;
    }
    for(int i=0;i<2;i++){
        for(int j=i+1;j<3;j++){
            if(mark[0][i]==mark[0][j])
                chk[0]++;
            if(mark[1][i]==mark[1][j])
                chk[1]++;
            if(mark[2][i]==mark[2][j])
                chk[2]++;
            if(mark[i][0]==mark[j][0])
                chk[3]++;
            if(mark[i][1]==mark[j][1])
                chk[4]++;
            if(mark[i][2]==mark[j][2])
                chk[5]++;
            if(mark[i][i]==mark[j][j])
                chk[6]++;
            if(mark[2-i][i]==mark[2-j][j])
                chk[7]++;
        }
    }
    for(int i=0;i<8;i++){
        if(chk[i]==3){
            flag=true;
            for(int j=1;j<=26;j++){
                if(save[i][j]==1){
                    for(int t=0;t<pt1;t++)
                        if(chk1[t]==j) flag=false;
                    if(flag){
                        chk1[pt1++]=j;
                        break;
                    }
                }
            }
            if(flag)
                a1++;
        }
        else if(chk[i]==1){
            flag=true;
            int m=0;
            for(j=1;j<=26;j++){
                if(save[i][j]==1&&m!=0){
                    for(int t=0;t<pt2;t++){
                        if(chk2[t][0]==m)
                            if(chk2[t][1]==j){
                                flag=false;
                                break;
                            }
                    }
                    break;
                }
                if(save[i][j]==1&&m==0){
                    m=j;
                }
            }
            if(flag){
                chk2[pt2][0]=m;
                chk2[pt2++][1]=j;
                a2++;
            }
        }
    }
    printf("%d\n%d\n",a1,a2);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43515064/article/details/88026369