1

What is the time complexity of examining all possible lines of length l on game board of n x m?

For instance, a tic-tac-toe board is 3x3 and lines are defined as length 3; there are 8 possible lines. We could also imagine a board that is 9x9 and a rule that you need a line of length 5 in order to win. How would you characterize the complexity of examining every possible line with different values of n, m and l?

Note this is not considering traversing the game tree into future game states, just examining the current state of the board to see if there is a winner or not in the present state.

ʞɔıu
  • 47,148
  • 35
  • 106
  • 149
  • Why check the entire board in the first place? – phant0m Sep 26 '12 at 20:37
  • @phant0m presumably because you want to know if someone has won - and you won't know unless you check everywhere (until you find one). – corsiKa Sep 26 '12 at 21:44
  • 1
    @corsiKa In a game where every player places a piece on the gameboard somewhere per move, it's not required to reexamine the entire board, at least not with simple games, because a single piece does not tend to affect the entire board. For instance, in a connect 5 game, you need only check at most 28 fields, even if it's a 1000x1000 field. – phant0m Sep 26 '12 at 21:51
  • @phant0m I see your point, in particular the note in the question specifically stating it is not considering traversing the game tree. I could see a wide-area search being required for determining if there is a winning move to be made, for merely checking the board state, and the invariant that if a move was made, then it was made on a board not already in a winning state, and the only possibility of a winning state means the most recent move must be part of the winning combination. – corsiKa Sep 26 '12 at 22:07
  • @corsiKa I don't think I understand what you mean. Why traverse the game tree? That's not what I meant, neither did I mean to look for a winning move. All I meant to say is, after a move has been made, you don't mostly don't need to evaluate the entire board to determine if someone has won. – phant0m Sep 26 '12 at 22:20
  • @phant0m tl;dr - you were right. See my edited answer. – corsiKa Sep 26 '12 at 22:58

1 Answers1

1

Clearly, you need to check horizontal, vertical, and diagonal lines.

Let us assume that the board is laid out with n always being the bigger number if the two are not equal, and that it is laying on its side" (lego style, not skyscraper style). So it is n across and m tall.

The horizontal lines will be n * (m - l) in number.

The vertical lines will be m * (n - l) in number.

The diagonal lines will be (m - l) * (n - l), or m*n - l*m - l*n + l*l

If we assume n >= m > l then we can safely say that it is within O(n^2), as we would expect for a two dimensional board.

We know that l > n >= m will have no results. If n = m = l we have a constant number (2*n + 2). If n = l > m we are left with an even better case, as we don't have to check the diagonals or the verticals, and you only have to check m lines. If n > l > m, then we can again exclude the verticals, but must consider the diagonals. In any event, it will be less than doing the diagonals, verticals, and horizontals.

There is an optimization that can be made, however, based on the phant0m's comment. It requires that you know what the last move made was.

You can safely assume that if a move was made, it was made at a time that there was no winner on the board. Therefore, if there is a win condition on the board after the move, it must have been formed as a result of the most recent move. Therefore, the worst case scenario given this information is that the winning line is formed with the most recent move on the end. You would therefore need to check the 4 line segments that extend l - 1 in each direction (horizontal, vertical, forward diagonal, and backward diagonal). This is a total of 4 * (2*l - 1), putting it safely in O(l). Considering you only need to store one additional coordinate (most recent move) this is a most wise optimization to make.

Community
  • 1
  • 1
corsiKa
  • 81,495
  • 25
  • 153
  • 204