sudoku region check function

Page 1 of 1

2 Replies - 3490 Views - Last Post: 18 November 2011 - 08:49 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=256116&amp;s=a96257db93412a59056d4c3287614675&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 start0

Reputation: 0
• Posts: 9
• Joined: 15-November 11

sudoku region check function

Posted 17 November 2011 - 02:43 PM

hello everyone,
i have to write a c++ program that checks whether or not a block or matrix of values is a solution to a Sudoku. the program need not solve a Sudoku puzzle, it only checks whether a row, column and region does not have duplicate values.
here is my code
```#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
using namespace std;
const int N = 9;	// array size constant

void readMatrix (ifstream &in_f, int a[][N]);
void printMatrix (int a[][N]);
void open_input_file (ifstream &in_f);
bool validMatrix(int a[][N]);

int main()
{
int arr[N][N];		//declaring the 2D array

ifstream in_f;		//input file
open_input_file(in_f);	//open the input file if it exists

printMatrix (arr);	// print the matrix
cout<<endl;		// into the terminal

if (validMatrix(arr))	// validity or invalidity conformation
cout<< "\n Is a sudoku solution. \n\n";
else
cout << "\n Is Not a sudoku solution. \n\n";

system("PAUSE");
in_f.close();
return 0;
}

// function reads matrix from a file
void readMatrix (ifstream& in_f, int a[][N])
{
int row,col;
do{
for (row=0;row<N;row++)
for (col=0;col<N;col++)
in_f>>a[row][col];

}while (!in_f.eof());

}
//function prints the matrix out
void printMatrix (int a[][N])
{
cout<<"The Matrix \n";
for (int row=0;row<N;++row){
cout<<setw(3);
cout<<endl;
for (int col=0;col<N;++col){
cout<<a[row][col]<<" ";
}
cout<<setw(3);
}
}
//funtion the checks the existance of
//the file that holds the matrix
void open_input_file (ifstream &in_f)
{
char input_file_name[80];
do{
in_f.clear();
cout<<"Enter input file name:";
cin>>input_file_name;
in_f.open(input_file_name);
}while(in_f.fail());
}
bool validMatrix(int a[][N])
{
int i, row , col;
int freq[N];

//check for duplicate values in a row
for (i=0;i<N;i++)
freq[i]=0;
for (row=0;row<N;row++)
for (col=0;col<N;col++)
freq[a[row][col]-1]+=1;

for (i=0;i<N;i++){
if (freq[i]!=N)
return false;
}

// check for duplicate values in a column
for (i=0;i<N;++i)
freq[i]=0;
for (col=0;col<N;col++)
for (row=0;row<N;row++)
freq[a[row][col]-1]+=1;

for (i=0;i<N;i++){
if (freq[i]!=N)
return false;
}

// check for duplicate values in a region
for (i=0;i<N;i++)
freq[i]=0;
for (row=0;row<N;row+=3)
for (col=0; col<N;col+=3){

freq[a[row][col]-1]+=1;
freq[a[row][col+1]-1]+=1;
freq[a[row][col+2]-1]+=1;
freq[a[row+1][col]-1]+=1;
freq[a[row+1][col+1]-1]+=1;
freq[a[row+1][col+2]-1]+=1;
freq[a[row+2][col]-1]+=1;
freq[a[row+2][col+1]-1]+=1;
freq[a[row+2][col+2]-1]+=1;

for (i=0;i<N;++i){
if(freq[i]>1)		// if duplicates found
return false;
freq[i]=0;
}
}
return true;
}

```

my program works like a charm but i think my region checking function contains redundant code or there is a better way of doing the same thing without soo much code for example inside my function "validMatrix" there is this block of code
```          for (row=0;row<N;row+=3)
for (col=0; col<N;col+=3){

freq[a[row][col]-1]+=1;
freq[a[row][col+1]-1]+=1;
freq[a[row][col+2]-1]+=1;
freq[a[row+1][col]-1]+=1;
freq[a[row+1][col+1]-1]+=1;
freq[a[row+1][col+2]-1]+=1;
freq[a[row+2][col]-1]+=1;
freq[a[row+2][col+1]-1]+=1;
freq[a[row+2][col+2]-1]+=1;

for (i=0;i<N;++i){
if(freq[i]>1)		// if duplicates found
return false;
freq[i]=0;
}
}

```

i am trying to rewrite it with less repetition or redundancy of the freq[.] array, i tried everything i could, even using a loop for the freq array like this, but it did not work. no errors but it gives the wrong result.
is there anything in there that could be written differently?
```for (row=0;row<N;row+=3)
for (col=0; col<N;col+=3){

for (row=0;row<N;row+=3){
for (col=0; col<N;col+=3)
freq[a[row][col]-1]+=1;

for (i=0;i<N;++i){
if(freq[i]>1)		// if duplicates found
return false;
freq[i]=0;
}
}

```

thank you
AM

Is This A Good Question/Topic? 0

Replies To: sudoku region check function

#2 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

Re: sudoku region check function

Posted 18 November 2011 - 06:53 PM

You use the row, col variables twice, something like:

```for (row=0; row<N; row+=3)
for (col=0; col<N; col+=3){

for (rowoff=0; ...; ...){
for (coloff=0; ...; ...)
freq[a[row+rowoff][col+coloff]-1]+=1;

```

#3 start0

Reputation: 0
• Posts: 9
• Joined: 15-November 11

Re: sudoku region check function

Posted 18 November 2011 - 08:49 PM

#define, on 18 November 2011 - 06:53 PM, said:

You use the row, col variables twice, something like:

```for (row=0; row<N; row+=3)
for (col=0; col<N; col+=3){

for (rowoff=0; ...; ...){
for (coloff=0; ...; ...)
freq[a[row+rowoff][col+coloff]-1]+=1;

```

wow, thank you soo much #define, its working beautifully now. u made it look soo simple, coloff and rowoff would just act like indexes for the region and in a 3x3 region, both indexes need to iterate 3 times, counting for the rows and columns, wow.

thank you a million.

```for (row=0;row<N;row+=3)
for (col=0; col<N;col+=3){

for (rowoff=0; rowoff < 3; rowoff++)
for (coloff=0; coloff < 3; coloff++){
freq[a[row+rowoff][col+coloff]-1]+=1;
}

```