# include # include # include # include # include # include using namespace std; # include "pentominoes.hpp" //****************************************************************************80 int *i4rows_copy_new ( int m, int n, int a1[] ) //****************************************************************************80 // // Purpose: // // I4MROWS_COPY_NEW copies an I4ROWS to a "new" I4MAT. // // Discussion: // // An I4ROWS is an MxN array of I4's, stored by (I,J) -> [I*N+J]. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 22 April 2018 // // Author: // // John Burkardt // // Parameters: // // Input, int M, N, the number of rows and columns. // // Input, int A1[M*N], the matrix to be copied. // // Output, int I4ROWS_COPY_NEW[M*N], the copy of A1. // { int *a2; int i; int j; a2 = new int[m*n]; for ( i = 0; i < m; i++ ) { for ( j = 0; j < n; j++ ) { a2[i*n+j] = a1[i*n+j]; } } return a2; } //****************************************************************************80 int *i4rows_zeros_new ( int m, int n ) //****************************************************************************80 // // Purpose: // // I4ROWS_ZEROS_NEW returns a new zeroed I4ROWS. // // Discussion: // // An I4ROWS is a doubly dimensioned array of I4 values, stored as a vector // in row-major order. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 22 April 2018 // // Author: // // John Burkardt // // Parameters: // // Input, int M, N, the number of rows and columns. // // Output, int I4ROWS_ZEROS_NEW[M*N], the new zeroed matrix. // { int *a; int i; int j; a = new int[m*n]; for ( i = 0; i < m; i++ ) { for ( j = 0; j < n; j++ ) { a[i*n+j] = 0; } } return a; } //*****************************************************************************/ void pentomino_matrix ( char name, int &p_m, int &p_n, int **p ) //*****************************************************************************/ // // Purpose: // // PENTOMINO_MATRIX returns a 0/1 matrix defining a particular pentomino. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 22 April 2018 // // Author: // // John Burkardt // // Parameters: // // Input, char NAME, is 'f', 'i', 'l', 'n', 'p', 't', 'u', 'v', 'w', 'x', // 'y' or 'z'. // // Output, int &P_M, &P_N, the number of rows and columns of the // representation. // // Output, int P[P_M*P_N], a matrix of 0's and 1's that indicates // the shape of the pentomino. // { int f_mat[3*3] = { 0, 1, 1, 1, 1, 0, 0, 1, 0 }; int i_mat[5] = { 1, 1, 1, 1, 1 }; int l_mat[4*2] = { 1, 0, 1, 0, 1, 0, 1, 1 }; int n_mat[2*4] = { 1, 1, 0, 0, 0, 1, 1, 1 }; int p_mat[3*2] = { 1, 1, 1, 1, 1, 0 }; int t_mat[3*3] = { 1, 1, 1, 0, 1, 0, 0, 1, 0 }; int u_mat[2*3] = { 1, 0, 1, 1, 1, 1 }; int v_mat[3*3] = { 1, 0, 0, 1, 0, 0, 1, 1, 1 }; int w_mat[3*3] = { 1, 0, 0, 1, 1, 0, 0, 1, 1 }; int x_mat[3*3] = { 0, 1, 0, 1, 1, 1, 0, 1, 0 }; int y_mat[2*4] = { 0, 0, 1, 0, 1, 1, 1, 1 }; int z_mat[3*3] = { 1, 1, 0, 0, 1, 0, 0, 1, 1 }; if ( name == 'f' || name == 'F' ) { p_m = 3; p_n = 3; *p = i4rows_copy_new ( p_m, p_n, f_mat ); } else if ( name == 'i' || name == 'I' ) { p_m = 5; p_n = 1; *p = i4rows_copy_new ( p_m, p_n, i_mat ); } else if ( name == 'l' || name == 'L' ) { p_m = 4; p_n = 2; *p = i4rows_copy_new ( p_m, p_n, l_mat ); } else if ( name == 'n' || name == 'N' ) { p_m = 2; p_n = 4; *p = i4rows_copy_new ( p_m, p_n, n_mat ); } else if ( name == 'p' || name == 'P' ) { p_m = 3; p_n = 2; *p = i4rows_copy_new ( p_m, p_n, p_mat ); } else if ( name == 't' || name == 'T' ) { p_m = 3; p_n = 3; *p = i4rows_copy_new ( p_m, p_n, t_mat ); } else if ( name == 'u' || name == 'U' ) { p_m = 2; p_n = 3; *p = i4rows_copy_new ( p_m, p_n, u_mat ); } else if ( name == 'v' || name == 'V' ) { p_m = 3; p_n = 3; *p = i4rows_copy_new ( p_m, p_n, v_mat ); } else if ( name == 'w' || name == 'W' ) { p_m = 3; p_n = 3; *p = i4rows_copy_new ( p_m, p_n, w_mat ); } else if ( name == 'x' || name == 'X' ) { p_m = 3; p_n = 3; *p = i4rows_copy_new ( p_m, p_n, x_mat ); } else if ( name == 'y' || name == 'Y' ) { p_m = 2; p_n = 4; *p = i4rows_copy_new ( p_m, p_n, y_mat ); } else if ( name == 'z' || name == 'Z' ) { p_m = 3; p_n = 3; *p = i4rows_copy_new ( p_m, p_n, z_mat ); } else { cout << "\n"; cout << "PENTOMINO_MATRIX - Fatal error!\n"; cout << " Illegal name = '" << name << "'\n"; cout << " Legal names: f, i, l, n, p, t, u, v, w, x, y, z.\n"; exit ( 1 ); } return; } //****************************************************************************80 void pentomino_plot ( int p_m, int p_n, int p[], string label ) //****************************************************************************80 // // Purpose: // // PENTOMINO_PLOT plots a particular pentomino in a 5x5 grid. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 22 April 2018 // // Author: // // John Burkardt // // Parameters: // // Input, int P_M, P_N, the number of rows and columns of the // representation. // // Input, int P[P_M*P_N], a matrix of 0's and 1's. // 1 <= P_M, P_N <= 5. There should be exactly 5 values of one. // // Input, string LABEL, a title for the plot. // { string color; int *color_index; string command_filename; ofstream command_unit; int i; int i_reverse; int j; int k; int m = 5; int n = 5; string plot_filename; command_filename = label + "_commands.txt"; plot_filename = label + ".png"; // // Initially, the grid is entirely white (color 0) // color_index = i4rows_zeros_new ( m, n ); // // Place the pentomino on the grid, so that it is "snug" in the upper left corner. // for ( i = 0; i < p_m; i++ ) { for ( j = 0; j < p_n; j++ ) { color_index[i*n+j] = p[i*p_n+j]; } } // // Create the command file. // command_unit.open ( command_filename.c_str ( ) ); command_unit << "# " << command_filename << "\n"; command_unit << "#\n"; command_unit << "# Usage:\n"; command_unit << "# gnuplot < " << command_filename << "\n"; command_unit << "#\n"; command_unit << "set term png\n"; command_unit << "set output '" << plot_filename << "'\n"; command_unit << "set title '" << label << "'\n"; // // Get a plot of TRUE SQUARES. // command_unit << "set xrange [ 0 : 5 ]\n"; command_unit << "set yrange [ 0 : 5 ]\n"; command_unit << "set size square\n"; command_unit << "unset border\n"; command_unit << "unset tics\n"; command_unit << "set nokey\n"; k = 0; for ( i = 0; i < m; i++ ) { for ( j = 0; j < n; j++ ) { k = k + 1; if ( color_index[i*n+j] == 0 ) { color = "white"; } else if ( color_index[i*n+j] == 1 ) { color = "black"; } i_reverse = m - 1 - i; command_unit << "set object " << k << " rect from " << j << ", " << i_reverse << " to " << j + 1 << ", " << i_reverse + 1 << " back\n"; command_unit << "set object " << k << " rect fc rgb '" << color << "' fillstyle solid 1.0\n"; } } // // If you don't have some bogus PLOT command here, all the previous work // results in no plot all. Way to go, gnuplot! // Here, we plot the function y = -1, which is out of range and won't show up. // command_unit << "plot -1 with lines\n"; command_unit.close ( ); cout << " PENTOMINO_PLOT created command file '" << command_filename << "'\n"; // // Free memory. // delete [] color_index; return; } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // Purpose: // // TIMESTAMP prints the current YMDHMS date as a time stamp. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 19 March 2018 // // Author: // // John Burkardt // // Parameters: // // None // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }