首页 > 试题广场 >

井字棋

[编程题]井字棋
  • 热度指数:21447 时间限制: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!
alist = list(input().split() for i in range(3))
direction = ['line', 'col', 'left', 'right']

def getResult():
    for item in direction:
        for i in range(3):
            res_list = []
            for j in range(3):
                if item == 'line':
                    res_list.append(alist[i][j])
                elif item == 'col':
                    res_list.append(alist[j][i])
                elif item == 'left':
                    res_list.append(alist[j][j])
                else:
                    res_list.append(alist[j][2-j])
            if res_list == ['K', 'K', 'K']:
                return 'KiKi wins!'
            elif res_list == ['B', 'B', 'B']:
                return 'BoBo wins!'
    return 'No winner!'

print(getResult())

发表于 2022-07-17 10:16:29 回复(0)
l = []
for i in range(3):
    l.append(input().split())
    
winner = False
# 检查行
for i in range(3):
    if l[i] == ['K']*3:
        print('KiKi wins!')
        winner = True
    elif l[i] == ['B']*3:
        print('BoBo wins!')
        winner = True
# 检查列
for j in range(3):
    if l[0][j] == l[1][j] == l[2][j] == 'K':
        print('KiKi wins!')
        winner = True
    elif l[0][j] == l[1][j] == l[2][j] == 'B':
        print('BoBo wins!')
        winner = True
# 检查对角线
if l[0][0] == l[1][1] == l[2][2]:
    if l[0][0] == 'K':
        print('KiKi wins!')
        winner = True
    elif l[0][0] == 'B':
        print('BoBo wins!')
        winner = True
if l[2][0] == l[1][1] == l[0][2]:
    if l[1][1] == 'K':
        print('KiKi wins!')
        winner = True
    elif l[1][1] == 'B':
        print('BoBo wins!')
        winner = True
if not winner:
    print('No winner!')
        

发表于 2022-04-03 17:59:27 回复(0)
l = []
for _ in range(3):
    l.append(input().split(' '))
win = 0
for i in range(3):
    if l[i] == ['K', 'K', 'K']:
        print('KiKi wins!')
        win = 1
        break
    if l[i] == ['B', 'B', 'B']:
        print('BoBo wins!')
        win = 1
        break
for i in range(3):
    if l[0][i]=='K' and l[1][i]=='K' and l[2][i]=='K':
        print('KiKi wins!')
        win = 1
        break
    if l[0][i]=='B' and l[1][i]=='B' and l[2][i]=='B':
        print('BoBo wins!')
        win = 1
        break
if l[0][0]=='K' and l[1][1]=='K' and l[2][2]=='K':
    print('KiKi wins!')
    win = 1
if l[0][0]=='B' and l[1][1]=='B' and l[2][2]=='B':
    print('BoBo wins!')
    win = 1
if l[0][2]=='K' and l[1][1]=='K' and l[2][0]=='K':
    print('KiKi wins!')
    win = 1
if l[0][2]=='B' and l[1][1]=='B' and l[2][0]=='B':
    print('BoBo wins!')
    win = 1
if win==0:
    print('No winner!')
我还以为有什么聪明取巧的方法呢,没想到大家都一样哈哈哈,简单粗暴
发表于 2021-10-27 21:58:06 回复(0)
#将3*3的二维数组转化成3*8的二维数组,分别是原数组、以列划分的数组和两个对角线数组,这样
#就将问题转化成判断新数组的每一行是不是包含三个相同的字符,通过使用count函数判定是否包含
#了3个相同字符,从而到达题目的目的
li = []
for i in range(3):
    li.append(list(map(str, input().split(' '))))
li = li + list(zip(*li))
li.append(list([li[0][0],li[1][1],li[2][2]]))
li.append(list([li[2][0],li[1][1],li[0][2]]))
for i in range(8):
    if li[i].count('B') == 3:
        re = 'BoBo wins!'
        break
    elif li[i].count('K') == 3:
        re = 'KiKi wins!'
        break
    else:
        re = 'No winner!'
print(re)

发表于 2021-09-24 14:42:26 回复(0)