-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw6.cpp
648 lines (563 loc) · 15.4 KB
/
hw6.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <iomanip>
#include <cmath>
#include <locale>
#include <list>
#include <unordered_map>
#include <map>
#include <cstdlib>
#include <cstdio>
#include <chrono>
typedef std::chrono::high_resolution_clock Clock;
using namespace std;
class Ship{
public:
int x = 0;
int y = 0;
int size_ = 0;
bool ori = true;
Ship(int s){
size_ = s;
ori = true;
x = 0;
y = 0;
}
void rotate(bool o){
ori = o;
}
int size(){
return size_;
}
bool operator==(Ship& s2){
if (x == s2.x and y == s2.y and size_ == s2.size() and ori == s2.ori){
return true;
}
return false;
}
};
bool read(Ship& s1, Ship& s2){
if(s1.y < s2.y || (s1.y == s2.y && s1.x < s2.x)){
return true;
}
return false;
}
class grid{
public:
grid(int x, int y, vector<int> rc, vector<int> cc, vector<int> sizes){
vector<bool> yaxis(y, false);
vector<vector<bool> > xaxis(x, yaxis);
g = xaxis;
r_const = rc;
c_const = cc;
sort(sizes.rbegin(), sizes.rend());
for(unsigned int i = 0; i<sizes.size(); i++){
Ship temp = Ship(sizes[i]);
ships_off_board.push_back(temp);
}
}
//This function investigates the grid g, which contains booleans that represent the current
//positions of all boards on the ship.
bool space_around_each_ship(){
bool ret = true;
if(g.size() <= 2 && g[0].size() <= 2 && ships_on_board.size() >=2){
return false;
}
for(list<Ship>::iterator i = ships_on_board.begin(); i!= ships_on_board.end(); i++){
int s = i->size();
int x = i->x;
int y = i->y;
if(i->ori){ //if the ships is oriented horizontally.
if(x == 0){ // if we're on the top row
if(y == 0){//if we're on the top left
if(s == 1){
ret = ret && !g[1][0] && !g[1][1] && !g[0][1];
}
else{
ret = ret && !g[1][0] && !g[1][1];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x+1][y+j];
}
if(y + s < g[0].size()){
ret = ret && !g[0][y+s] && !g[1][y+s];
}
}
}
else if(y == g[0].size()-1){ //on the top right
//remember, we assume the ship is oriented horizontally, so s must be 1.
ret = ret && !g[0][g[0].size()-2] && !g[1][g[0].size()-2] && !g[1][g[0].size()-1];
}
else{ //top middle
if(s == 1){
ret = ret && !g[x][y-1] && !g[x][y+1] && !g[x+1][y-1] && !g[x+1][y] && !g[x+1][y+1];
}
else{
ret = ret && !g[x][y-1] && !g[x+1][y-1] && !g[x+1][y];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x+1][y+j];
}
if(y+s < g[0].size()){
ret = ret && !g[0][y+s] && !g[1][y+s];
}
}
}
}
else if (x == g.size()-1){ // if we're on the bottom row
if(y == 0){ //bottom left
if(s == 1){
ret = ret && !g[x-1][0] && !g[x-1][1] && !g[x][1];
}
else{
ret = ret && !g[x-1][0] && !g[x-1][1];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x-1][y+j];
}
if(y + s < g[0].size()){
ret = ret && !g[x-1][y+s] && !g[x][y+s];
}
}
}
else if(y == g[0].size()-1){//bottom right
ret = ret && !g[x][y-1] && !g[x-1][y-1] && !g[x-1][y];
}
else{ //bottom middle
if(s == 1) {
ret = ret && !g[x][y-1] && !g[x][y+1] && !g[x-1][y-1] && !g[x-1][y] && !g[x-1][y+1];
}
else{
ret = ret && !g[x][y-1] && !g[x-1][y-1] && !g[x-1][y];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x-1][y+j];
}
if(y+s < g[0].size()){
ret = ret && !g[x-1][y+s] && !g[x][y+s];
}
}
}
}
else{ //we are on a middle row
if(y == 0){ //middle left
if(s == 1){
ret = ret && !g[x-1][0] && !g[x-1][1] && !g[x][1] && !g[x+1][1] && !g[x+1][0];
}
else{
ret = ret && !g[x-1][0] && !g[x+1][0];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x-1][y+j] && !g[x+1][y+j];
}
if(y + s < g[0].size()){ //if we dont end on the right edge:
ret = ret && !g[x-1][y+s] && !g[x][y+s] && !g[x+1][y+s];
}
else{
ret = ret && !g[x-1][y+s-1] && !g[x+1][y+s-1];
}
}
}
else if(y == g[0].size()-1){//middle right, must be s == 1
ret = ret && !g[x][y-1] && !g[x-1][y-1] && !g[x-1][y] && !g[x+1][y-1] && !g[x+1][y];
}
else{ //middle middle
if(s == 1) {
ret = ret && !g[x][y-1] && !g[x][y+1] && !g[x-1][y-1] && !g[x-1][y] && !g[x-1][y+1]
&& !g[x+1][y-1] && !g[x+1][y] && !g[x+1][y+1];
}
else{
ret = ret && !g[x][y-1] && !g[x-1][y-1] && !g[x-1][y] && !g[x+1][y] && !g[x+1][y-1];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x-1][y+j] && !g[x+1][y+j];
}
if(y+s < g[0].size()-1){
ret = ret && !g[x-1][y+s] && !g[x][y+s] && !g[x+1][y+s];
}
ret = ret && !g[x-1][y+s-1] && !g[x+1][y+s-1];
}
}
}
}
else{ //the ship is oriented vertically.
if(x == 0){ // if we're on the top row
if(y == 0){//if we're on the top left
if(s == 1){
ret = ret && !g[1][0] && !g[1][1] && !g[0][1];
}
else{
ret = ret && !g[0][1] && !g[1][1];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x+j][y+1];
}
if(x + s < g.size()){
ret = ret && !g[x+s][0] && !g[x+s][1] && !g[x+s-1][1];
}
}
}
else if(y == g[0].size()-1){ //on the top right
if(s == 1){
ret = ret && !g[x][y-1] && !g[x+1][y-1] && !g[x+1][y];
}
else{
ret = ret && !g[x][y-1] && !g[x+1][y-1];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x+j][y-1];
}
if(x + s < g.size()){
ret = ret && !g[x+s][g[0].size()-1] && !g[x+s][g[0].size()-2];
}
}
}
else{ //top middle
if(s == 1){
ret = ret && !g[x][y-1] && !g[x][y+1] && !g[x+1][y-1] && !g[x+1][y] && !g[x+1][y+1];
}
else{
ret = ret && !g[x][y-1] && !g[x][y+1];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x+j][y-1] && !g[x+j][y+1];
}
if(y+s < g[0].size()){
ret = ret && !g[x+s][y-1] && !g[x+s][y] && !g[x+s][y+1];
}
ret = ret && !g[1][y+s-1];
}
}
}
else if (x == g.size()-1){ // if we're on the bottom row
if(y == 0){ //bottom left
if(s == 1){
ret = ret && !g[x-1][0] && !g[x-1][1] && !g[x][1];
}
}
else if(y == g[0].size()-1){//bottom right
if(s == 1){
ret = ret && !g[x-1][g[0].size()-1] && !g[x-1][g[0].size()-2] && !g[x][g[0].size()-2];
} }
else{ //bottom middle
if(s == 1) {
ret = ret && !g[x][y-1] && !g[x][y+1] && !g[x-1][y-1] && !g[x-1][y] && !g[x-1][y+1];
}
}
}
else{ //we are on a middle row
if(y == 0){ //middle left
if(s == 1){
ret = ret && !g[x-1][0] && !g[x-1][1] && !g[x][1] && !g[x+1][1] && !g[x+1][0];
}
else{
ret = ret && !g[x-1][0] && !g[x-1][1];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x+j][y+1];
}
if(x + s < g.size()){ //if we dont end on the left edge:
ret = ret && !g[x+s][y] && !g[x+s][y+1];
}
}
}
else if(y == g[0].size()-1){ //middle right
if(s == 1){
ret = ret && !g[x][y-1] && !g[x-1][y-1] && !g[x-1][y] && !g[x+1][y-1] && !g[x+1][y];
}
else{
ret = ret && !g[x-1][y-1] && !g[x-1][y];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x+j][y-1];
}
if(x+s < g.size()){
ret = ret && !g[x+s][y] && !g[x+s][y-1];
}
}
}
else{ //middle middle
if(s == 1) {
ret = ret && !g[x][y-1] && !g[x][y+1] && !g[x-1][y-1] && !g[x-1][y] && !g[x-1][y+1]
&& !g[x+1][y-1] && !g[x+1][y] && !g[x+1][y+1];
}
else{
ret = ret && !g[x-1][y-1] && !g[x-1][y] && !g[x+1][y+1];
for(unsigned int j = 0; j<s; j++){
ret = ret && !g[x+j][y-1] && !g[x+j][y+1];
}
if(x+s < g.size()-1){
ret = ret && !g[x+s][y-1] && !g[x+s][y] && !g[x+s][y+1];
}
}
}
}
}
}
return ret;
}
void solve(){
if (ships_off_board.size() == 0 && verify_constraints()){
sols.push_back(ships_on_board);
return;
}
for(unsigned int i = 0; i<g.size(); i++){
for(unsigned int j = 0; j<g[0].size(); j++){
bool o = true;
if(insert(ships_off_board.front(), i, j, o)){
ships_off_board.erase(ships_off_board.begin());
if(space_around_each_ship()){
solve();
}
del(ships_on_board.back());
}
if(ships_off_board.front().size() != 1){
if(insert(ships_off_board.front(), i, j, !o) ){ //ensure no duplicate solutions
//with submarines
ships_off_board.erase(ships_off_board.begin());
if(space_around_each_ship()){
solve();
}
del(ships_on_board.back());
}
}
}
}
}
//x represents a row, y a column
//consider adding functionality such that insert returns false for optimality
//when the insert will not have space around the ship inserted.
bool insert(Ship& s, int x, int y, bool h){ //if h is true, then the ship should be inserted horrizontally
s.rotate(h);
//if the ship will go off an edge when being inserted, return false
if((x + s.size() > g.size() && !h) || (y + s.size() > g[0].size() && h) ){return false;}
s.x = x;
s.y = y;
if(!h){ //ship is vertical in orientation
//first check if there is space to place the ship vertically.
for (int i = x; i < x + s.size(); i++){
if(g[i][y]){ //if there's no space, do not insert, immediately return false
return false;
}
}
for(int i = x; i < x + s.size(); i++){
g[i][y] = true;
}
ships_on_board.push_back(s);
return true;
}
else if(h){
for (int i = y; i < y + s.size() && i < g[0].size(); i++){
//prevent writing over an existing ship
if(g[x][i]){ //if there's no space, do not insert, immediately return false
return false;
}
}
for(int i = y; i<y+s.size(); i++){
g[x][i] = true;
}
ships_on_board.push_back(s);
return true;
}
}
bool del(Ship& s){
bool ret;
for(list<Ship>::iterator i = ships_on_board.begin(); i!=ships_on_board.end(); i++){
if(*i == s){
if(s.ori){
for(unsigned int j = s.y; j< s.y+s.size(); j++) {
g[s.x][j] = false;
}
}
if(!s.ori){
for(unsigned int j = s.x; j< s.x+s.size(); j++){
g[j][s.y] = false;
}
}
ships_off_board.push_front(*i);
ships_on_board.erase(i);
return true;
}
}
return false;
}
//clears the grid, ships on board vector, and ships off board vector.
void clear_board(){
if(g.size() == 0){
return;
}
if(g[0].size() == 0){
return;
}
ships_off_board.clear();
ships_on_board.clear();
//clear board boolean representation
vector<bool> yaxis(g.size(), false);
vector<vector<bool> > xaxis(g[0].size(), yaxis);
g = xaxis;
}
void print_sols(){
clear_board();
for(unsigned int i = 0; i<sols.size(); i++){
for(list<Ship>::iterator j = sols[i].begin(); j!=sols[i].end(); j++){
insert(*j, j->x, j->y, j->ori);
}
print();
clear_board();
}
cout<<"Found "<<sols.size()<<" solutions."<<endl;
}
void print(){
ships_on_board.sort(read);
if(g.size() == 0){
cout<<"Empty grid"<<endl;
return;
}
if(ships_on_board.size() == 0){
cout<<" ";
for(unsigned int i = 0; i<g[0].size(); i++){
cout<<c_const[i];
}
cout<<endl;
cout<<" ";
for(unsigned int i = 0; i<g[0].size(); i++){
cout<<"-";
}
cout<<endl;
for(unsigned int i = 0; i<g.size(); i++){
cout<<"|";
for(unsigned int j = 0; j<g[0].size(); j++){
cout<<" ";
}
cout<<"|"<<r_const[i]<<endl;
}
cout<<" ";
for(unsigned int i = 0; i<g[0].size(); i++){
cout<<"-";
}
cout<<endl;
return;
}
cout<<" ";
for(unsigned int i = 0; i<g[0].size(); i++){
cout<<c_const[i];
}
cout<<endl;
cout<<" ";
for(unsigned int i = 0; i<g[0].size(); i++){
cout<<"-";
}
cout<<endl;
for(unsigned int i = 0; i<g.size(); i++){ //which row
cout<<"|";
for(unsigned int j = 0; j<g[0].size(); j++){ //which col
string out = " ";
for(list<Ship>::iterator k = ships_on_board.begin(); k!=ships_on_board.end();k++){//if any ship is on the current coord (i,j)
if(k->ori && k->x == i && k->y <= j &&
j < k->y + k->size()){
//if this ships is horizontal, and is on the same row as one of the ships,
//and the current column coordinate we're on is within the size of the starting point of the ship:
out = "X";
break;
}
else if(!k->ori && j == k->y &&
k->x <= i && i < k->x + k->size()){
out = "X";
break;
}
}
cout<<out;
if(j == g[0].size()-1){
cout<<"|"<<r_const[i];
cout<<endl;
}
}
}
cout<<" ";
for(unsigned int i = 0; i<g[0].size(); i++) {
cout<<"-";
}
cout<<endl;
}
int sum(vector<int> & a){
int s = 0;
for(unsigned int i = 0; i<a.size(); i++){
s+=a[i];
}
return s;
}
bool verify_constraints(){
if(g.size() == 0){
return false;
}
vector<int> rtemp = r_const;
vector<int> ctemp = c_const;
for(unsigned int i = 0; i<g.size(); i++){
for(unsigned int j = 0; j<g[0].size(); j++){
if(g[i][j]){
rtemp[i]-=1;
ctemp[j]-=1;
}
}
if(rtemp[i] != 0){
return false;
}
}
if(sum(rtemp) + sum(ctemp) == 0){
return true;
}
return false;
}
bool verify_cols(){
if(g.size() == 0){
return false;
}
for(unsigned int i = 0; i<g.size(); i++){
int row_c = 0;
for(unsigned int j = 0; j<g[0].size(); j++){
if(g[i][j]){
row_c++;
}
}
if(row_c != r_const[i]){
return false;
}
}
return true;
}
private:
vector<int> c_const;
vector<int> r_const; //contraints for all cols/rows
vector<vector<bool> > g;
list<Ship> ships_off_board;
list<Ship> ships_on_board;
vector<list<Ship> > sols;
};
int main(){
string cmd;
cout<<"Input file name: "<<endl;
cin>> cmd;
ifstream in_str(cmd); //input files will contain the number of
//rows, endl, cols, endl, rconstraints, endl, cconstraints, endl
//the number of ships, endl, all ship sizes.
in_str>>cmd; //number of rows read.
int rows = atoi(cmd.c_str());
in_str>>cmd; //number of columns read.
int cols = atoi(cmd.c_str());
vector<int> r_consts;
vector<int> c_consts;
for(unsigned int i = 0; i<rows; i++){
string temp;
in_str>>temp;
r_consts.push_back(atoi(temp.c_str()));
}
for(unsigned int i = 0; i<cols; i++){
string temp;
in_str>>temp;
c_consts.push_back(atoi(temp.c_str()));
}
in_str>>cmd;
vector<int> sizes;
for(unsigned int i = 0; i < atoi(cmd.c_str()); i++){
string temp;
in_str>>temp;
sizes.push_back(atoi(temp.c_str()));
}
grid gr(rows, cols, r_consts, c_consts, sizes);
//now solve the board according to constraints
gr.solve();
gr.print_sols();
}