You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
" matrix.append(row[:]) #copy of row, no indexing problems later on\n",
36
+
" return matrix"
37
+
]
38
+
},
39
+
{
40
+
"cell_type": "code",
41
+
"source": [
42
+
"#Knight's move restrictions, we restrict the squares attacked\n",
43
+
"def place_attacks(pos_x,pos_y,chessboard):\n",
44
+
" if pos_x+2<dim and pos_y +1<dim:\n",
45
+
" chessboard[pos_x+2][pos_y +1] = 'X'\n",
46
+
" if pos_x+2<dim and pos_y -1 >=0:\n",
47
+
" chessboard[pos_x+2][pos_y -1] = 'X'\n",
48
+
" if pos_x+1<dim and pos_y +2<dim:\n",
49
+
" chessboard[pos_x+1][pos_y +2] = 'X'\n",
50
+
" if pos_x+1<dim and pos_y -2>=0:\n",
51
+
" chessboard[pos_x+1][pos_y -2] = 'X'\n",
52
+
" if pos_x-2>=0 and pos_y+1<dim:\n",
53
+
" chessboard[pos_x-2][pos_y +1] = 'X'\n",
54
+
" if pos_x-2>=0 and pos_y-1>=0:\n",
55
+
" chessboard[pos_x-2][pos_y -1] = 'X'\n",
56
+
" if pos_x-1>=0 and pos_y-2>=0:\n",
57
+
" chessboard[pos_x-1][pos_y -2] = 'X'\n",
58
+
" if pos_x-1>=0 and pos_y+2<dim:\n",
59
+
" chessboard[pos_x-1][pos_y +2] = 'X'"
60
+
],
61
+
"metadata": {
62
+
"id": "EmvnRNUNEEDT"
63
+
},
64
+
"execution_count": 10,
65
+
"outputs": []
66
+
},
67
+
{
68
+
"cell_type": "code",
69
+
"source": [
70
+
"def check_attacks(chessboard):\n",
71
+
" pos_x = None\n",
72
+
" pos_y = None\n",
73
+
" less_restrictions = -9 # Highest number of squares restricted by the knight's moves is 8\n",
74
+
" A = copy.deepcopy(chessboard) # copy of chessboard\n",
75
+
" # Insert copies of original object, source: https://docs.python.org/dev/library/copy.html#module-copy\n",
76
+
" for i in range(0,len(A)):\n",
77
+
" for j in range(0,len(A)):\n",
78
+
" restricted_fields = 0\n",
79
+
"\n",
80
+
" if A[i][j] != 'X' and A[i][j] == 0: #square availabel, we will check if it gets us a good solution\n",
81
+
" if i+2<(len(A)) and j+1<(len(A)):\n",
82
+
" if A[i+2][j +1] != 'X' and A[i+2][j +1] <= 0:\n",
83
+
" restricted_fields = restricted_fields - 1\n",
84
+
"\n",
85
+
" if i+2<(len(A)) and j -1 >=0:\n",
86
+
" if A[i+2][j -1] != 'X' and A[i+2][j -1] <= 0:\n",
87
+
" restricted_fields = restricted_fields - 1\n",
88
+
"\n",
89
+
" if i+1<(len(A)) and j +2<(len(A)):\n",
90
+
" if A[i+1][j +2] != 'X' and A[i+1][j +2] <= 0:\n",
91
+
" restricted_fields = restricted_fields - 1\n",
92
+
"\n",
93
+
" if i+1<(len(A)) and j -2>=0:\n",
94
+
" if A[i+1][j -2] != 'X' and A[i+1][j -2] <= 0:\n",
95
+
" restricted_fields = restricted_fields - 1\n",
96
+
"\n",
97
+
" if i-2>=0 and j+1<(len(A)):\n",
98
+
" if A[i-2][j +1] != 'X' and A[i-2][j +1] <= 0:\n",
99
+
" restricted_fields = restricted_fields - 1\n",
100
+
"\n",
101
+
" if i-2>=0 and j-1>=0:\n",
102
+
" if A[i-2][j -1]!= 'X' and A[i-2][j -1] <= 0:\n",
103
+
" restricted_fields = restricted_fields - 1\n",
104
+
"\n",
105
+
" if i-1>=0 and j-2>=0:\n",
106
+
" if A[i-1][j -2]!= 'X' and A[i-1][j -2] <= 0:\n",
107
+
" restricted_fields = restricted_fields - 1\n",
108
+
"\n",
109
+
" if i-1>=0 and j+2<len(A):\n",
110
+
" if A[i-1][j +2]!= 'X' and A[i-1][j +2] <= 0:\n",
111
+
" restricted_fields = restricted_fields - 1\n",
112
+
"\n",
113
+
"\n",
114
+
" A[i][j] = restricted_fields\n",
115
+
"\n",
116
+
" if less_restrictions <= restricted_fields: #we keep our best solution (highest number, minimum = -8, highest = 0(best scenario))\n",
117
+
" less_restrictions = restricted_fields\n",
118
+
" pos_x = i\n",
119
+
" pos_y = j\n",
120
+
"\n",
121
+
"\n",
122
+
" return (pos_x,pos_y,A)"
123
+
],
124
+
"metadata": {
125
+
"id": "M815-gViEBDJ"
126
+
},
127
+
"execution_count": 11,
128
+
"outputs": []
129
+
},
130
+
{
131
+
"cell_type": "code",
132
+
"source": [
133
+
"#Placing the knights\n",
134
+
"def place_knights(chessboard):\n",
135
+
" global current_knight\n",
136
+
"\n",
137
+
" if current_knight == 1:\n",
138
+
" first_move = True\n",
139
+
" else:\n",
140
+
" first_move = False\n",
141
+
" dim = len(chessboard)\n",
142
+
"\n",
143
+
" if first_move: #On first move, we got no restrictions yet. By our heuristic, we will start in one of the 4 corners since it only blocks 2 squares\n",
144
+
"\n",
145
+
" #We will choose randomly the first square to be occupied\n",
146
+
"\n",
147
+
" corners = [0,dim-1]\n",
148
+
" pos_x = random.choice(corners)\n",
149
+
" pos_y = random.choice(corners)\n",
150
+
" chessboard[pos_x][pos_y] = current_knight\n",
151
+
"\n",
152
+
"\n",
153
+
" #We restrict positions on our chessboard based on the knight that was placed\n",
154
+
" place_attacks(pos_x,pos_y,chessboard)\n",
155
+
"\n",
156
+
" current_knight = current_knight + 1\n",
157
+
"\n",
158
+
"\n",
159
+
" #After first move\n",
160
+
" else:\n",
161
+
" for i in range(dim*dim - 1):\n",
162
+
"\n",
163
+
" pos = check_attacks(chessboard)\n",
164
+
" if pos[0] == None:\n",
165
+
" break\n",
166
+
" chessboard[pos[0]][pos[1]] = current_knight #Alocamos o i-ésimo cavaleiro a melhor posição possível (segundo nossa heurística).\n",
167
+
" place_attacks(pos[0],pos[1],chessboard)\n",
168
+
"\n",
169
+
" current_knight = current_knight + 1\n",
170
+
" #return chessboard\n",
171
+
"\n",
172
+
" return reset_chessboard()\n",
173
+
" return"
174
+
],
175
+
"metadata": {
176
+
"id": "9GR6nY_uD6YS"
177
+
},
178
+
"execution_count": 12,
179
+
"outputs": []
180
+
},
181
+
{
182
+
"cell_type": "code",
183
+
"source": [
184
+
"#We keep our chessboard and the number of knights on that board\n",
185
+
"#We reset the board to start another one, if necessary\n",
186
+
"def reset_chessboard():\n",
187
+
" global chessboard_solutions\n",
188
+
" global solutions\n",
189
+
" global current_knight\n",
190
+
" global chessboard\n",
191
+
"\n",
192
+
" chessboard_solutions.append(chessboard)\n",
193
+
" n_knights = current_knight - 1\n",
194
+
" solutions.append(n_knights)\n",
195
+
" current_knight = 1\n",
196
+
"\n",
197
+
" dim= len(chessboard)\n",
198
+
" chessboard = []\n",
199
+
" row = [0]*dim\n",
200
+
" for i in range(dim):\n",
201
+
" chessboard.append(row[:])\n",
202
+
"\n",
203
+
" return chessboard_solutions,solutions"
204
+
],
205
+
"metadata": {
206
+
"id": "jdA6fqVeD11V"
207
+
},
208
+
"execution_count": 13,
209
+
"outputs": []
210
+
},
211
+
{
212
+
"cell_type": "code",
213
+
"source": [
214
+
"def branch(P): #we will branch our board P in P0 (without putting a knight on the next position) and in P1(having a knight on the next position)\n",
0 commit comments