-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathback.cpp
More file actions
83 lines (78 loc) · 1.59 KB
/
back.cpp
File metadata and controls
83 lines (78 loc) · 1.59 KB
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
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <iomanip>
//Shelly Huang
//Recursion and backtracking waas used!~
int excl = -1;
void Print(int** arr, int n){
cout << endl;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << setw(3) << arr[i][j];
}
cout << endl;
}
}
int** CreateGrid(int n){
int** arr = new int*[n];
for(int i = 0; i < n; i++){
arr[i] = new int[n];
}
for(int k = 0; k < n; k++){
for(int m = 0; m < n; m++){
arr[k][m] = 0;
}
}
return arr;
}
bool CanWe(int** arr, int x, int y, int n){
for(int i = 0; i < x; i++){
if(arr[x][y] == arr[i][y]) return false;
}
for(int j = 0; j < y; j++){
if(arr[x][y] == arr[x][j]) return false;
}
return true;
}
bool Iterate(int** arr, int &x, int &y, int n){
for(x = 0; x < n; x++){
for(y = 0; y < n; y++){
if(arr[x][y] == 0) return true;
}
}
return false;
}
bool Solve(int** arr, int n){
int x, y;
srand(time(NULL));
if(Iterate(arr, x, y, n) == false) return true;
arr[x][y] = (rand()%n)+1;
for(int k = 1; k <= n; k++){
if(k == excl) continue;
//check if original number is ok
if(CanWe(arr, x, y, n)){
if(Solve(arr, n)) return true;
}
arr[x][y] = k;
//check if number iterated through array is ok
if(CanWe(arr, x, y, n)){
if(Solve(arr, n)) return true;
}
}
//if all possible choices are exhausted
excl = arr[x][y];
//label element as untouched, to start again
arr[x][y] = 0;
return false;
}
int main(){
int n;
cout << "Enter dimension";
cin >> n;
int** arr = CreateGrid(n);
Solve(arr, n);
Print(arr, n);
}