-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
142 lines (132 loc) · 2.66 KB
/
test.cpp
File metadata and controls
142 lines (132 loc) · 2.66 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include<stdio.h>
#include<stdlib.h>
#define swap( a, b ) int temp = a; a = b; b = temp;
int none[9] = {2,3,8,0,4,7,6,5,1};
int input[9];
int goal[9] = {1,2,3,8,0,4,7,6,5};
void print_display(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
printf("%d ",input[i*3+j]);
}
printf("\n");
}
}
int isWin(){
int check = 1;
for(int i=0;i<9;i++){
if(input[i] != goal[i]) check = 0;
}
return check;
}
int check(int *arr){
int count = 0;
for(int i=0;i<9;i++){
if(arr[i] != goal[i]) count++;
}
return count;
}
int* up(){
int zero_position;
for(int i=0;i<9;i++){
if(input[i] == 0) zero_position = i;
}
if(zero_position / 3 < 1) return none;
else{
int *up_arr;
up_arr= (int *)malloc(sizeof(int)*9);
for(int i=0;i<9;i++){
up_arr[i] = input[i];
}
swap(up_arr[zero_position],up_arr[zero_position-3]);
return up_arr;
}
}
int* down(){
int zero_position;
for(int i=0;i<9;i++){
if(input[i] == 0) zero_position = i;
}
if(zero_position / 3 > 1) return none;
else{
int *down_arr;
down_arr= (int *)malloc(sizeof(int)*9);
for(int i=0;i<9;i++){
down_arr[i] = input[i];
}
swap(down_arr[zero_position],down_arr[zero_position+3]);
return down_arr;
}
}
int* left(){
int zero_position;
for(int i=0;i<9;i++){
if(input[i] == 0) zero_position = i;
}
if(zero_position % 3 < 1) return none;
else{
int *left_arr;
left_arr= (int *)malloc(sizeof(int)*9);
for(int i=0;i<9;i++){
left_arr[i] = input[i];
}
swap(left_arr[zero_position],left_arr[zero_position-1]);
return left_arr;
}
}
int* right(){
int zero_position;
for(int i=0;i<9;i++){
if(input[i] == 0) zero_position = i;
}
if(zero_position % 3 > 1) return none;
else{
int *right_arr;
right_arr= (int *)malloc(sizeof(int)*9);
for(int i=0;i<9;i++){
right_arr[i] = input[i];
}
swap(right_arr[zero_position],right_arr[zero_position+1]);
return right_arr;
}
}
int main(void) {
for(int i=0;i<9;i++){
scanf("%d",&input[i]);
}
int last = -1;
while(1){
printf("\n");
print_display();
if(isWin()){
break;
}
int *u,*d,*l,*r;
u = up();
d = down();
l = left();
r = right();
int* next;
next = none;
if(last != 0 && check(next) > check(u)){
next = u;
last = 1;
}
if(last != 1 && check(next) > check(d)){
next = d;
last = 0;
}
if(last != 2 && check(next) > check(l)){
next = l;
last = 3;
}
if(last != 3 && check(next) > check(r)){
next = r;
last = 2;
}
for(int i=0;i<9;i++){
input[i] = next[i];
}
// char c = getchar();
}
}