1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include <iostream>
#include <cstdlib>
using std::cin;
using std::cout;
const int N = 3;
void get_matrix(int matrix[][N*N]);
bool row_ok(int matrix[][N*N],int row);
bool col_ok(int matrix[][N*N],int col);
bool square_ok(int matrix[][N*N], int box_row,int box_col);
int main()
{
int matrix[N*N][N*N],count=0;
bool check_col=true,check_row=true,check2=true,check4=true,check_sub_square=true;
get_matrix(matrix);
/* this is so that you can visualize the matrix
cout<<"\n\n\n";
for (int i=0; i<N*N; i++)
{
for (int j=0; j<N*N; j++)
{
cout << matrix[i][j] << ' ';
}
cout<<'\n';
}
*/
check2=square_ok(matrix,7,4);
if (check2)
{
cout<<"true\n";
}
else
{
cout<<"false\n";
}
for (int i=0;i<N*N && check_row;i++)
{
check_row = row_ok(matrix,i);
}
for (int j=0;j<N*N && check_col && check_row ;j++)
{
check_col = col_ok(matrix,j);
}
for (int i = 0 ;i<N && check4; i++)
{
for(int j=0;j<N && check4; j++)
{
check4 = square_ok(matrix,i*N+1,j*N+1);
if (check4)
{
count++;
}
else
{
check4=false;
break;
}
}
}
if (count==N*N)
{
check_sub_square=true;
}
else
{
check_sub_square=false;
}
if (!check_row || !check_col || !check_sub_square)
{
cout<<"0\n";
}
else
{
cout<<"1\n";
}
}
void get_matrix(int matrix[N*N][N*N])
{
int i,j,temp=0;
for (i=0; i<N*N; i++)
{
for (j=0; j<N*N; j++)
{
//I added the cout so you can keep track of numbers at input
cout << "Row " << i+1 << " column " << j+1 << ": ";
cin >> temp;
if(temp>=0 && temp<=N*N)
{
matrix[i][j]=temp;
}
else
{
j--;
}
}
}
}
bool row_ok(int matrix[][N*N],int row)
{
//here you have to change: row must be in the first set of brackets.
//that is because point[i][j] means it has i lines and j columns.
for(int i=0; i<N*N; i++)
{
for(int j=(N*N)-1; j>=i; j--)
//j>=i will reduce the number of executions 36 times when the grid is valid.
//just like you did in square_ok
{
if (matrix[row][i]!=0)
{
if (matrix[row][i]==matrix[row][j] && i!=j)
{
return false;
}
}
}
}
return true; //else the function returns true
}
bool col_ok(int matrix[][N*N],int col)
{
//here you have to change: col must be in the second set of brackets.
//that is because point[i][j] means it has i lines and j columns.
for(int i=0; i<N*N; i++)
{
for(int j=(N*N)-1; j>=i; j--)
//j>=i will reduce the number of executions 36 times when the grid is valid.
//just like you did in square_ok
{
if (matrix[i][col]!=0)
{
if (matrix[i][col]==matrix[j][col] && i!=j)
{
return false;
}
}
}
}
return true;
}
//this has some comparing bugs, try this
bool square_ok(int matrix[][N*N],int box_row,int box_col)
{
int remainder1,remainder2,corner_row,corner_col;
remainder1 = box_row %N;
remainder2 = box_col %N;
int corner_row_holder=box_row - remainder1
,corner_col_holder =box_col - remainder2 ;
for(corner_row=corner_row_holder ;corner_row<corner_row_holder+N; corner_row++)
{
for(corner_col=corner_col_holder ;corner_col<corner_col_holder+N; corner_col++)
{
for (int i=corner_row_holder+N-1; i>=corner_row_holder; i--)
{
for (int j=corner_col_holder+N-1; j>=corner_col_holder; j--)
{
if (matrix[i][j]!=0 && i!=corner_row || j!=corner_col)
{
if (matrix[i][j]==matrix[corner_row][corner_col])
{
return false;
}
}
}
}
}
}
return true;
}
| |