-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnonoheu.cpp
More file actions
executable file
·115 lines (110 loc) · 3.16 KB
/
nonoheu.cpp
File metadata and controls
executable file
·115 lines (110 loc) · 3.16 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>
#include <time.h>
#include <unistd.h>
#include "board.h"
#include "nonogramReader.h"
#include "nonogramWriter.h"
//default options
char *problemName = NULL;
int problemNum = 1;
int boardSize = 25;
bool isTourament = false;
/// @brief use getopt to get arg
///
/// reference: http://wen00072-blog.logdown.com/posts/171197-using-getopt-parse-command-line-parameter
void parseArgument(int argc, char** argv){
while(true){
int cmd_opt = getopt(argc, argv, "tn:f:s:");
if (cmd_opt == -1) {//End condition
break;
} else if (cmd_opt != '?') {//Print option when it is valid
fprintf(stderr, "get option:-%c\n", cmd_opt);
}
switch (cmd_opt) { /// Error handle: Mainly missing arg or illegal option
case 't':
fprintf(stderr, "enable tourament mode\n");
isTourament = true;
break;
case 'n':
if(optarg){
sscanf(optarg, "%d", &problemNum);
fprintf(stderr, "[OPTION] problem num %d\n", problemNum);
} else {
fprintf(stderr, "[OPTION] -n without argument\n");
exit(1);
}
break;
case 's':
if(optarg){
sscanf(optarg, "%d", &boardSize);
fprintf(stderr, "[OPTION] board size %d\n", boardSize);
} else {
fprintf(stderr, "[OPTION] -s without argument\n");
}
break;
case 'f':
if (optarg){
problemName = new char[1000];
int i = sprintf(problemName, "%s", optarg);
fprintf(stderr, "[OPTION] output file name: %s.out\n", problemName);
} else {
fprintf(stderr, "[OPTION] -f no argument\n");
}
break;
case '?':
fprintf(stderr, "Illegal option\n");
exit(1);
break;
default:
fprintf(stderr, "Not supported option %s\n", optarg);
exit(1);
break;
}
}
/* additional args */
if (argc > optind) {
int i = 0;
for (i = optind; i < argc; i++)
fprintf(stderr, "argv[%d] = %s\n", i, argv[i]);
}
//temporaily deleted
//if(problemName == NULL)
//int i = asprintf(&problemName, "result");
}
int main(int argc, char** argv){
setlocale(LC_ALL, "");//for wchar
parseArgument(argc, argv);
Board board;
NonogramInputReader ir(stdin, boardSize);//read question from stdin
NonogramWriterInterface* writer;
if(isTourament){
writer = new NonogramWriter_Tourament;
} else {
writer = new NonogramWriter;
}
board.setWriter(writer);
clock_t beginTime = clock();
for(int i = 0; i < problemNum; i++){
clock_t start_t = clock();
fprintf(stderr, "Solving problem %d\n", i);
ir.readInputAndGetBoard(&board, problemName, isTourament);
//solve
board.doHeuristic();
board.doDFS();
if(!board.checkAnswer())
fprintf(stderr, "recheck answer failed\n");
else
fprintf(stderr, "%d recheck correct\n", i);
board.printBoard("after solved");
board.saveResult();
clock_t end_t = clock();
printf("time spent: %lf\n", (double(end_t) - double(start_t)) / CLOCKS_PER_SEC);
}
clock_t endTime = clock();
printf("total time spent: %lf\n", (double(endTime) - double(beginTime)) / CLOCKS_PER_SEC);
return 0;
}