Choice between 1-dimensional and 2-dimensional array

I’m implementing the Chessboard class to represent the chessboard. I’ve to implement the transformations (reflections and rotations) on the chess board possible.

The possible transformations includes the combination of:

 1. Vertical Reflection
 2. Horizontal Reflection
 3. Diagonal Reflection

Thus, we’ve 8 possible transformations for chess board.

There are 64 squares on the Chessboard numbered [0..63].

Thus, to represent the total resulting values after the transformations is 8*64 (No.of Transformations * Chessboard_Size).

There are two fundamental ways to represent the transformed_board using Arrays:

One-Dimensional Array with transformed_board[8*64]
Two-Dimensional Array with transformed_board[8][64]

Questions:

  • Which approach is better?
  • What are the pros and cons of each approach?
  • How will effect the performance with respect to time factor?

2

Well, in your problem space the 64 squares are formed from the 8 x 8 size of your board, so the most adequate design would be to use a 3 dimensional array:

 transformed_board[8][8][8]

Performance will depend on your implementation, the operations you will do on that array and the compiler you are using. Don’t fall into the trap of premature optimization. Best approach here probably is to provide accessor functions like

 SQUARE_TYPE GetSquare(int row,int column, int symmetry)
 {
      assert(0<=row && row<MAX_ROWS);
      assert(0<=column && column <MAX_COLUMNS);
      assert(0<=symmetry&& symmetry<MAX_SYMMETRIES);
      return transformed_board[row][col][symmetry];
 }

and restrict all access to the board to the use of that accessors. Then it will be easy to change the decision about the array dimensions used for transformed_board later, just by changing your accessor functions.

2

you probable don’t need to store all possible transformations so you can just compute them as needed when you access them

If you want to get clever in accessing the boards then you can use a variant of matrix transformations:

int[6][MAX_SYMMETRIES] transforms={{ 1, 0, 0,
                                     0, 1, 0},
                                   { 0, 1, 0,
                                     1, 0, 0},
                                   {-1, 0, 7,
                                     0, 1, 0},...};

SQUARE_TYPE GetSquare(int[8][8]& board, int row, int column, int symmetry)
{
     assert(0<=row && row<MAX_ROWS);
     assert(0<=column && column <MAX_COLUMNS);
     assert(0<=symmetry&& symmetry<MAX_SYMMETRIES);

     int newRow = transforms[symmetry][0]*row+ transforms[symmetry][1]*column+ transforms[symmetry][2]
     int newCollumn = transforms[symmetry][3]*row+ transforms[symmetry][4]*column+ transforms[symmetry][5]

     return board[newRow][newCollumn];
}

you can also do this with the 64 length array if needed

1 dimension or 2 dimension arrays actually have same data structure in memory.
The elements will be in contiguous memory block. So, performance wise, it will be the same, if you use same indexing.

2 dimensions is probably easier to read, and manipulate (easier to write board[1][3] than board[1*64 + 3])

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật