简体   繁体   English

Python TicTacToe 函数的时间复杂度

[英]Time Complexity of Python TicTacToe function

def TicTacToe():
    board = [['S' for i in range(3)] for j in range(3)]
    BoardState(board)

    for i in range(8):
        if(i % 2 == 0):
            print("Enter the position player 1: ")
            a = int(input())
            b = int(input())
            if(board[a - 1][b - 1] == 'S'):
                board[a - 1][b - 1] = 1
            else:
                print("Invalid Input, game over")
                return
        else:
            print("Enter the position player 2: ")
            a = int(input())
            b = int(input())
            if(board[a - 1][b - 1] == 'S'):
                board[a - 1][b - 1] = 0
            else:
                print("Invalid input, game over")
                return
                
        BoardState(board)
        
        if(board[0][0] == 1 and board[1][1] == 1 and board[2][2] == 1):
            return "Player 1 wins"
        if(board[0][0] == 0 and board[1][1] == 0 and board[2][2] == 0):
            return "Player 2 wins"
        if(board[0][2] == 1 and board[1][1] == 1 and board[2][0] == 1):
            return "Player 1 wins"
        if(board[0][2] == 0 and board[1][1] == 0 and board[2][0] == 0):
            return "Player 2 wins"
        if(CheckState(board) == 0):
            return "game over"
        
    return "Draw"

def CheckState(board):        
    for x in range(3):
        if board[x][0] == board[x][1] == board[x][2] != 'S' \
        or board[0][x] == board[1][x] == board[2][x] != 'S':
            return 0
        
def BoardState(board):
    for i in range(3):
        for j in range(3):
            print(board[i][j], end = ' ')
        print("\n")    


TicTacToe();

This is a not-so-clean code I wrote for the game of tictactoe.这是我为 tictactoe 游戏编写的不太干净的代码。 What would be its time complexity?它的时间复杂度是多少?

The program consists of 3 functions and I wish to calculate the complexity for the same.该程序由 3 个函数组成,我希望计算相同的复杂度。 Would it be O(1) as the input size is already fixed it doesn't vary with whatever the user inputs?它会是 O(1) 因为输入大小已经固定它不会随用户输入而变化吗?

Would it be O(1) as the input size is already fixed it doesn't vary with whatever the user inputs?它会是 O(1) 因为输入大小已经固定它不会随用户输入而变化吗?

Yes, that is exactly right.是的,完全正确。 All of the loops have a fixed number of iterations.所有的循环都有固定的迭代次数。

I think you're actually talking about two different problems: The first is a program to act as a "referee", similar to your program above.我认为您实际上在谈论两个不同的问题:第一个是充当“裁判”的程序,类似于您上面的程序。 The second is a program to play against a human player (or other program).第二个是与人类玩家(或其他程序)对战的程序。

For complexity, think about the case of an N x N tic tac toe board.对于复杂性,请考虑 N x N 井字棋盘的情况。 In that case, I think the complexity of the first problem is O(N), while for the second, it's exponential in N.在那种情况下,我认为第一个问题的复杂性是 O(N),而对于第二个问题,它是 N 的指数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM