首页 > 试题广场 >

井字棋

[编程题]井字棋
  • 热度指数:22438 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

KiKiBoBo玩 “井”字棋。也就是在九宫格中,只要任意行、列,或者任意对角线上面出现三个连续相同的棋子,就能获胜。请根据棋盘状态,判断当前输赢。


输入描述:
三行三列的字符元素,代表棋盘状态,字符元素用空格分开,代表当前棋盘,其中元素为K代表KiKi玩家的棋子,为O表示没有棋子,为B代表BoBo玩家的棋子。


输出描述:
如果KiKi获胜,输出“KiKi wins!”;
如果BoBo获胜,输出“BoBo wins!”;
如果没有获胜,输出“No winner!”。
示例1

输入

K O B
O K B
B O K

输出

KiKi wins!
qipan = []
for i in range(3):
    a = list(map(str, input().split()))
    for j in a:
        qipan.append(j)
wins = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
]
if qipan.count("O") < 5:
    for win in wins:
        if qipan[win[0]] == "K" and qipan[win[1]] == "K" and qipan[win[2]] == "K":
            print("KiKi wins!")
            break
        elif qipan[win[0]] == "B" and qipan[win[1]] == "B" and qipan[win[2]] == "B":
            print("BoBo wins!")
            break
        else:
            continue
    else:
        print("No winner!")
else:
    print("No winner!")
发表于 2021-01-25 20:53:19 回复(1)
zl=[input().split() for i in range(3)]
options=[[zl[i][j] for i in range(3) ] for j in range(3)]
options.extend([[zl[i][j] for j in range(3) ] for i in range(3)])
options.extend([[zl[i][i] for i in range(3)]])
options.extend([[zl[i][2-i] for i in range(3)]])
c=0
for i in options:
    tmp=list(set(i))
    if len(tmp)==1 and tmp[0]=='B':
        print("BoBo wins!")
    elif len(tmp)==1 and tmp[0]=='K':
        print("KiKi wins!")
    else:
        c+=1
if c==8:
    print("No winner!")

发表于 2020-12-24 15:10:32 回复(0)
n=3
a=[]
b=0
for i in range(0,n):
    a.append(list(map(str, input().split())))
if (a[0][0]==a[0][1] and a[0][0]==a[0][2] )&nbs***bsp;(a[0][0]==a[1][1] and a[0][0]==a[2][2] )&nbs***bsp;(a[0][0]==a[1][0] and a[0][0]==a[2][0]):
    b=1
elif (a[0][2]==a[1][1] and a[0][2]==a[2][0])&nbs***bsp;(a[0][2]==a[1][2] and a[0][2]==a[2][2]):
    b=2
elif (a[2][0]==a[2][1] and a[2][0]==a[2][2])&nbs***bsp;(a[0][1]==a[1][1] and a[1][1]==a[2][1]):
    b=3
elif (a[1][0]==a[1][1] and a[1][1]==a[1][2]):
    b=4
if b==1:
    if str(a[0][0])=="K":
        print("KiKi wins!")
    elif str(a[0][0])=="B":
        print("BoBo wins!")
    else:
        print("No winner!")
if b==2:
    if str(a[0][2])=="K":
        print("KiKi wins!")
    elif str(a[0][2])=="B":
        print("BoBo wins!")
    else:
        print("No winner!")
if b==3:
    if str(a[2][1])=="K":
        print("KiKi wins!")
    elif str(a[2][1])=="B":
        print("BoBo wins!")
    else:
        print("No winner!")
if b==4:
    if str(a[1][1])=="K":
        print("KiKi wins!")
    elif str(a[1][1])=="B":
        print("BoBo wins!")
    else:
        print("No winner!")

编辑于 2020-09-08 11:08:38 回复(0)