2017.1.19【初中部 】普及组模拟赛C组 小x的游戏 题解

原题:

http://172.16.0.132/junior/#contest/show/1369/0

题目描述:

Tac游戏在一个4*4的方格上进行。起先可能会在16个方格中出现一个标记‘T’,其余的方格是空着的。
游戏有两个玩家,小x和小o。小x先开始,然后游戏轮流进行。每一步玩家可以将他的标记放入一个空的方格中。小x的标记是‘X’,小o的标记是‘O’。
在一个玩家结束操作后,如果出现一行、一列或者对角线都是该玩家的标记,或者有3个该玩家的标记以及标记‘T’,那么他获得胜利,游戏结束,否则游戏继续,进入另一个玩家的回合。如果所有的方格都被填满,并且没人获得胜利,那么游戏结束,为平局。
给出一个4*4的方格,包括‘X’,‘O’,‘T’和‘.’(‘.’表示空的方格),输出现在游戏的状态,游戏的状态包括:
• “X won”(游戏结束,小x获胜)
• “O won”(游戏结束,小o获胜)
• “Draw”(游戏结束,平局)
• “Game has not completed”(游戏还未结束)
如果有空格,并且游戏还未结束,你应该打印 “Game has not completed”,即使最后的结果是一定的。

输入:

第一行一个整数T,表示测试数据的组数。
每组测试数据包括4行,每行4个字符,如题所述。每组测试数据后会有一行空行。

输出:

对于每组测试数据,输出“Case #x: y”,x表示第x组测试数据(组号从1开始),y即上述的状态之一。注意‘O’不是‘0’。

样例输入:

6
XXXT
….
OO..
….

XOXT
XXOO
OXOX
XXOO

XOX.
OX..
….
….

OOXX
OXXX
OX.T
O..O

XXXO
..O.
.O..
T…

OXXX
XO..
..O.
…O

样例输出:

Case #1: X won
Case #2: Draw
Case #3: Game has not completed
Case #4: O won
Case #5: O won
Case #6: O won

数据范围限制:

对于50%的数据:1<=T<=10
对于100%的数据:1<=T<=110

分析:

枚举判断每一行,每一列,两条对角线上的字符符不符合题意即可

实现:

const
    p1:array[1..4,1..2]of longint=((1,1),(2,2),(3,3),(4,4));
    p2:Array[1..4,1..2]of longint=((1,4),(2,3),(3,2),(4,1));
var
    t,w,i,j,xt,ot:longint;
    s:Array[0..5,0..5]of char;
    bz,bx,bo:boolean;
begin
    assign(input,'game.in');reset(input);
    assign(output,'game.out');rewrite(output);
    readln(t);
    w:=1;
    while w<=t do
    begin
        bz:=true; bx:=false; bo:=false;
        for i:=1 to 4 do
        begin
            xt:=0; ot:=0;
            for j:=1 to 4 do
            begin
                read(s[i,j]);
                if s[i,j]='.' then bz:=false;
                if s[i,j]='X' then inc(xt);
                if s[i,j]='O' then inc(ot);
                if s[i,j]='T' then begin inc(xt); inc(ot); end;
            end;
            if xt=4 then bx:=true;
            if ot=4 then bo:=true;
            readln;
        end;
        for j:=1 to 4 do
        begin
            xt:=0; ot:=0;
            for i:=1 to 4 do
            begin
                if s[i,j]='X' then inc(xt);
                if s[i,j]='O' then inc(ot);
                if s[i,j]='T' then begin inc(xt); inc(ot); end;
            end;
            if xt=4 then bx:=true;
            if ot=4 then bo:=true;
        end;
        xt:=0; ot:=0;
        for i:=1 to 4 do
        begin
            if s[p1[i,1],p1[i,2]]='X' then inc(xt);
            if s[p1[i,1],p1[i,2]]='O' then inc(ot);
            if s[p1[i,1],p1[i,2]]='T' then begin inc(xt); inc(ot); end;
        end;
        if xt=4 then bx:=true;
        if ot=4 then bo:=true;
        xt:=0; ot:=0;
        for i:=1 to 4 do
        begin
            if s[p2[i,1],p2[i,2]]='X' then inc(xt);
            if s[p2[i,1],p2[i,2]]='O' then inc(ot);
            if s[p2[i,1],p2[i,2]]='T' then begin inc(xt); inc(ot); end;
        end;
        if xt=4 then bx:=true;
        if ot=4 then bo:=true;
        write('Case #',w,': ');
        if bx then writeln('X won');
        if bo then writeln('O won');
        if (bx=false)and(bo=false) then
            if bz then writeln('Draw')
            else writeln('Game has not completed');
                readln;
        inc(w);
    end;
    close(input);close(output);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值