How to check square block condition in sudoku for its validity?

This is a discussion on How to check square block condition in sudoku for its validity? within the C Programming forums, part of the General Programming Boards category; Thank you for helping!!!!1...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    8

    Post How to check square block condition in sudoku for its validity?

    Thank you for helping!!!!1

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    So where is YOUR effort?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are more elegant ways to do this, but take a look at this basic idea:

    Code:
    int TestBox(int nudigit, int r, int c)	{    //just tests one box 
    	int c1, r1, i, n, lowrow, lowcol;
    	/*get lowrow & lowcol*/
    	switch (r)	{
    		case 1: case 2: case 3: lowrow = 1; break;
    		case 4: case 5: case 6: lowrow = 4; break;
    		case 7: case 8: case 9: lowrow = 7; break;
    	}   
    	if(c < 4) 
    		lowcol = 1; 
    	else if(c > 3 && c < 7)	
    		lowcol = 4; 
    	else	 
    		lowcol = 7;  
    												
    	for(r1 = lowrow; r1 < lowrow + 3; r1++)	{
    		for(c1 = lowcol; c1 < lowcol + 3; c1++)	{
    			if(nudigit == A[r1][c1])
    				return 0;
    		}
    	}
    	return 1;
    This tests one digit which you are trying out, to see if it can work in the box of 9. In this version, I used rows and columns 1 through 9, instead of 0 through 8, so you may need to adjust.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    8
    here is my effort from line 36 to 57
    Code:
     1 #include<stdio.h>
      2 #include<math.h>
      3 int main()
      4 {
      5         int t,count=1,i,j,k=0,n,z,l=0,p,f=0,key=0;
      6         int a[100][100];
      7         scanf("%d",&t);
      8         for(count=1;count<=t;count++)
      9         {
     10                 scanf("%d",&n);
     11                 for(i=0;i<n;i++)
     12                         for(j=0;j<n;j++)
     13                                 scanf("%d",&a[i][j]);
     14                 for(i=0;i<n;i++)
     15                 {
     16                         z=0;
     17                         for(j=0;j<n;j++)
     18                                 z=z+a[i][j];
     19                         if(z==(n*(n+1))/2)
     20                                 k=1;
     21                         else
     22                                 k=0;
     23                 }
    
     24                 for(j=0;j<n;j++)
     25                 {
     26                         z=0;
     27                         for(i=0;i<n;i++)
     28                                 z=z+a[i][j];
     29                         if(z==(n*(n+1))/2)
     30                                 l=1;
     31                         else
     32                                 l=0;
     33                 }
     34                 z=sqrt(n);
     35                 u=0;v=0;
     36                 for(i=u;i<u+z&&i<n;i++)
     37                 {
     38                         for(j=v;j<v+z&&j<n;j++)
     39                         {
     40                                 printf("%d",a[i][j]);
     41                                 z=z+a[i][j];
     42                                 f++;
     43                         }
     44                         if(
     45                         if(i==u+z-1&&j==v+z-1)
     46                                 v=v+z;
     47                         printf("\n");
     48                         if(f==n)
     49                         {
     50                                 f=0;
     51                         if(z==(n*(n+1))/2)
     52                                 p=1;
     53                         else
     54                                 p=0;
     55                         z=0;
     56                         }
     57                 }
     58                 if(k==1&&l==1&&p==1)
     59                         printf("YES\n");
     60                 else if(k!=1||l!=1||p!=1)
     61                         printf("NO\n");
     62         }
     63         return 0;
     64 }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    How about removing the extra set of line numbers?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    8
    my code is not complete....please help me
    I m a beginner.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why is your Sudoku array so HUGE? Are you trying to work with Super sized Sudoku puzzles?

    You'll find it easier to develop your program using the standard 9x9 grid, and then move to the larger puzzle size. Definitely, you should be setting this up with separate functions!

    And add some notes to your code! Your variable names are not helping.
    Last edited by Adak; 09-25-2012 at 03:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IF Condition Check
    By nickman in forum C Programming
    Replies: 3
    Last Post: 09-06-2012, 08:03 AM
  2. Check for validity of IP address
    By krishnampkkm in forum Linux Programming
    Replies: 1
    Last Post: 03-25-2010, 06:38 AM
  3. check for y condition
    By alperen1994 in forum C Programming
    Replies: 3
    Last Post: 03-28-2009, 03:04 PM
  4. Help! with character validity check
    By Tdankert in forum C Programming
    Replies: 19
    Last Post: 07-30-2007, 09:41 PM
  5. Pointer validity check
    By Carlos in forum Windows Programming
    Replies: 6
    Last Post: 12-11-2003, 03:40 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21