Wednesday, July 15, 2015

#codingexercise
#interviewquestion
Given a board like Othello, flip the coins in horizontal and vertical directions on a move.
void flip (ref int[,] board, int x, int y, int color)
{
    int i = x-1;
    while (i > 0){
       if (board[i,y] == color)
          for (int k = i+1; k < x; k++)
             board[k,y] = color;
        i--;
     }

     i = x+1;
     while (i < N){
       if (board[i,y] == color)
          for (int k = x+1; k < i; k++)
             board[k,y] = color;
        i++;
     }

    int j = y-1;
    while (j > 0){
       if (board[x,j] == color)
          for (int k = j+1; k < y; k++)
             board[x,k] = color;
        j--;
     }

     j = y+1;
     while (j < N){
       if (board[x,j] == color)
          for (int k = y+1; k < j; k++)
             board[x.k] = color;
        j++;
     }

}

No comments:

Post a Comment