-
Notifications
You must be signed in to change notification settings - Fork 2
/
solvability.h
56 lines (37 loc) · 882 Bytes
/
solvability.h
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
int inv_count(int **puzzle , int N){
int arr[N*N]; int p=0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
arr[p++]=puzzle[i][j];
}
int count=0;
for(int i=0;i<p-1;i++){
for(int j=i+1;j<p;j++){
if(arr[i]>0 && arr[j]>0 && arr[i]>arr[j])
count++;
}
}
return count;
}
int get_pos(int **puzzle, int N){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(puzzle[i][j]==0)
return N-i;
}
}
}
bool isSolvable(int **puzzle, int N){
if(N%2){
if(inv_count(puzzle,N)%2==0)
return true;
return false;
}
else{
int count=inv_count(puzzle,N);
int pos=get_pos(puzzle,N);
if(count%2 != pos%2)
return true;
return false;
}
}