-
Notifications
You must be signed in to change notification settings - Fork 0
/
shang.c
194 lines (175 loc) · 4.63 KB
/
shang.c
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// #include <stdio.h>
#define ROWS 40
#define COLS 20
#ifndef true
#define true 1
#define false 0
#endif
char g_mainSetting[ROWS][COLS+1] = // the row setting so far
{
" *",
" **",
" * * * ",
" * *** ",
"* * * * ",
" ** * *",
" * ** * * ",
" ***** ",
" ** * * * ",
" * * *** ",
" * * * * * ",
" ** * * * ",
" * * * * * ",
" * * ** *",
" *** * * ",
" * * * * * * ",
" ** * * * * ",
" ***** * ",
" * * * ** * ",
" * * * * * * ",
" * *** ** ",
" ** * * * * ",
" *** * * * ",
" *** * * * ",
" * ** * * * ",
" ** * * * * * ",
" * * * *** * ",
"***** * *",
" ** * * *** ",
"* * * *** * ",
" *** ** * *",
" ** * ** * * ",
"*** * * *** ",
" ** ** *** * ",
" * * ** *** * ",
"* *** *** ** ",
" *** * ** ***",
" **** ** *** ",
"** *** * ** ** ",
"* * * ***** ** *",
};
typedef struct
{
char rg[ROWS][COLS+1]; // the row setting so far
int rowIndexes[20];
int afAvailableRows[40];
} Game;
void initGame(Game* oldg, Game* newg){
int i,j;
for(i = 0; i < ROWS; i++)
for(j = 0; j < COLS+1; j++)
newg->rg[i][j] = oldg->rg[i][j];
for(i = 0; i < 20; i++)
newg->rowIndexes[i] = oldg->rowIndexes[i];
for(i = 0; i < 40; i++)
newg->afAvailableRows[i] = oldg->afAvailableRows[i];
}
int my_strncmp(char* one, char* two, int num){
int i;
int result = 0;
for(i = 0; i < num; i++){
if(one[i] < two[i]){
result = -1;
break;
}
else if(one[i] > two[i]){
result = 1;
break;
}
}
return result;
}
void my_strcpy(char* news, char* olds){
int i = 0;
do{
news[i] = olds[i];
i++;
}while(olds[i] != '\0');
}
int CheckConstraint(Game *g, int NextRow)
{
// make sure that there is a viable set of columns from the remaining rows
// in the main set
// sweep across all 20 columns and convert them into rows
char rowsToCols[20][20];
int i,j;
for(i = 0; i< 20; i++)
{
for(j =0; j <20; j++)
{
rowsToCols[i][j] = g->rg[j][i];
}
}
int temp_afAvailableRows[40];
//memcpy(temp_afAvailableRows, g->afAvailableRows, sizeof(int) *40);
for(i = 0; i < 40; i++)
temp_afAvailableRows[i] = g->afAvailableRows[i];
// go through all rows, and make sure that there is an available row that
// matches the first NextRow characters
for(i = 0; i<20; i++)
{
for(j = 0; j<40; j++)
{
if(!temp_afAvailableRows[j])
continue;
if(my_strncmp(g_mainSetting[j], rowsToCols[i], NextRow + 1) == 0) // match
{
temp_afAvailableRows[j] = false;
break;
}
}
if(j>=40) // weren't able to fulfill constraints
return false;
}
return true;
}
int SolveGame(Game *g, int NextRow)
{
int i,j;
if (NextRow == 20)
{
/*
cout<<"Solved"<<endl;
// solved the game!! Print it out and exit
for (i=0; i<20; i++)
cout<<g_mainSetting[g->rowIndexes[i]]<<endl;
cout<<"Rows"<<endl;
for(i=0; i<20; i++)
cout<<g->rowIndexes[i]<<endl;
*/
return true;
}
// try to locate the first available row
for (i = 0; i < 40; i++)
{
if(g->afAvailableRows[i])
{
Game newGame;
initGame(g, &newGame);
newGame.rowIndexes[NextRow] = i;
newGame.afAvailableRows[i] = false;
my_strcpy(newGame.rg[NextRow], g_mainSetting[i]);
// Do constraint check
if (!CheckConstraint(&newGame, NextRow))
continue;
// constraint check succeeded
if( SolveGame(&newGame, NextRow + 1))
{
return true;
}
}
}
return false;
}
int main()
{
Game game;
int i;
for (i = 0; i < 40; i++)
{
game.afAvailableRows[i] = true;
}
int f = SolveGame(&game, 0);
// printf("%d\n", f);
return f;
}