4*4 Sudoku

Hi, I was asked to write a program,given board with some integers fixed,
outputs a valid solution or reports No solution if the board has no solution.I use "." represent the grid is not filled. we can only fill,1~4.

My program can do checking on the existing board. However, it does not give me right output for valid solutions.

here is my code:


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
#include <iostream>

using namespace std;

char Pdot[4]={1,2,3,4};

//pre: given 4 characthers, to check whethere there are any duplicate digits
//post:if there are duplicate digits in a[4], if returns false, otherwise returns true.
bool checkFour(char a[4]){
                    for (int i=0; i<4; i++){
                             for(int j=1;j<4;j++) {
			           if (a[i] != '.' && a[i] == a[j])
				     return false;
                        }
                    }
	              return true;
}

bool check(char sudo[][4]) {
                char a[4];
                for (int i=0;i<4;++i){
                      for (int m=0; m<4; ++m){
                            a[m]= sudo[i][m];
                            }
                      if (!checkFour(a))
                      return false;
                }
                
                for (int n=0; n<4; ++n){
                         for (int j=0;j<4;++j){
                            a[n]= sudo[n][j];
                            }
                      if (!checkFour(a))
                      return false;
                }
                
                
                   a[0]=sudo[0][0];
                   a[1]=sudo[0][1];
                   a[2]=sudo[1][0];
                   a[3]=sudo[1][0];
                
                   if (!checkFour(a))
                   return false;
                    
                   else{
                         a[0]=sudo[0][2];
                         a[1]=sudo[0][3];
                         a[2]=sudo[1][2];
                         a[3]=sudo[1][3];
                      
                         if (!checkFour(a))
                         return false;
                      
                         else {
                               a[0]=sudo[2][0];
                               a[1]=sudo[2][1];
                               a[2]=sudo[3][0];
                               a[3]=sudo[3][1];
                                  
                               if (!checkFour(a))
                               return false;
                    
                               else {
                                     a[0]=sudo[2][2];
                                     a[1]=sudo[2][3];
                                     a[2]=sudo[3][2];
                                     a[3]=sudo[3][3];
                                           
                                     if (!checkFour(a))
                                     return false;
                         
                                     else 
                                     return true;
                               }
                         }
                }
                return true;
}

bool fill(char sudo[][4]){           
      for (int i=0;i<4;i++){
               for (int j=0;j<4;j++){
                        if (!sudo[i][j]== '.') // if the grid is not ".", then check to next grid                       
                        continue;
                        else {
                            for(int m=0;m<4;m++){
                                  sudo[i][j]=Pdot[m];
                                  if (!check(sudo))
                                      continue;
                                  else
                                  return true;
                            }
                        }
               }  
      }
}
          
int main(){
      char sudo[4][4];
      
      for (int i=0;i<4;i++){
               for (int j=0;j<4;j++){
                        cin >> sudo[i][j];
               }
      }
      if (fill(sudo)){
            for (int i=0;i<4;i++){
                for (int j=0;j<4;j++){
                   cout << sudo[i][j];
                }
                cout<< "\n" ;
            }
      }
               
      else 
      cout << "No solution!" << endl;
      
      system ("pause");
      return 0;
}
On line 11, you should initialize j to i so you're never checking an element against itself. I don't know if it's the only thing wrong, but it kind of jumped out at me.
Topic archived. No new replies allowed.