本文整理汇总了C++中Puzzle::printGrid方法的典型用法代码示例。如果您正苦于以下问题:C++ Puzzle::printGrid方法的具体用法?C++ Puzzle::printGrid怎么用?C++ Puzzle::printGrid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Puzzle
的用法示例。
在下文中一共展示了Puzzle::printGrid方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char *argv[]) {
// timer
struct timeval start, end;
long mtime, seconds, useconds;
gettimeofday(&start, NULL);
time_t startTime = time(0);
if (argc != 3) {
printf("wrong number of inputs\n");
exit(1);
}
int** grid = fileTo2dArray(argv[1], atoi(argv[2]));
Puzzle* p = new Puzzle(grid, atoi(argv[2]));
p->initialize();
/*
p->printGridDim();
p->printGrid();
p->printPreassigned();
p->printInitCandidates();
printf("start: %i, %i\n", p->getCurrentRow(), p->getCurrentCol());
while (p->nextSlot()) {
printf("nextslot: row is %i, col is %i\n", p->getCurrentRow(), p->getCurrentCol());
}
while (p->prevSlot()) {
printf("prevslot: row is %i, col is %i\n", p->getCurrentRow(), p->getCurrentCol());
}
printf("TEST: check candidates\n");
p->checkCandidates(p->getCurrentRow(), p->getCurrentCol());
printf("TEST: assign\n");
p->assign(p->getCurrentRow(), p->getCurrentCol());
printf("TEST: update neighbors\n");
p->updateNeighborConflicts(p->getCurrentRow(), p->getCurrentCol(), p->getCurrentAssigned(0, 0), true);
printf("TEST: check neighbors\n");
p->checkNeighborCandidates(p->getCurrentRow(), p->getCurrentCol());
*/
//int counter=10000;
while (!p->isSolved()) {
//while (!p->isSolved() && counter>0) {
//counter--;
//printf("step 1\n");
// step 1
if (checkCurrentCandidates(p)) {
//printf("step 3\n");
// step 3
bool succeeded = assignAndValidate(p);
if (succeeded) {
//printf("step 5\n");
// step 5
advanceSlot(p);
}
else {
//printf("step 4\n");
// step 4
backtrack(p);
}
}
else {
//printf("step 2\n");
// step 2
if (reverseSlot(p)) {
//printf("step 4\n");
// step 4
backtrack(p);
}
else {
//printf("step 2 failed\n");
break;
}
}
}
if (p->isSolved()) {
printf("solution found!\n");
p->printGrid();
}
else {
printf("no solution found\n");
}
//printf("deinitializing\n");
p->deinitialize();
//printf("deinitialized\n");
delete2dIntArray(grid, atoi(argv[2]));
//printf("deleted original array.\n");
//timer
double seconds_since_start = difftime( time(0), startTime);
printf("%f seconds to run program\n", seconds_since_start);
gettimeofday(&end, NULL);
seconds = end.tv_sec - start.tv_sec;
//.........这里部分代码省略.........
示例2: main
//.........这里部分代码省略.........
highestVisitedPosition = p->positionOnVisited;
}
// step 1
if (currentDepth >= parallelStartDepth && checkCurrentCandidates(p)) {
Puzzle* forSolving = p->makePreassignedCopy();
if (testLevel > 1) {
printf("making copy: old candidates are\n");
p->printInitCandidates();
printf("new candidates are\n");
forSolving->printInitCandidates();
}
toSolve.push_back(forSolving);
if (testLevel > 1) printf("toSolve size: %i\n", (int)toSolve.size());
if (reverseSlot(p)) {
currentDepth--;
backtrack(p);
}
else {
break;
}
if (toSolve.size() >= vectorSizeLimit) {
if (testLevel > 1) printf("toSolve reached %i elements, attempting solve now\n", vectorSizeLimit);
solved = solveInitializedPuzzles(&toSolve, highestVisitedPosition, &numTried);
if (solved != NULL) {
break;
}
else while (toSolve.size() > 0) {
toSolve.pop_back();
}
}
}
else if (checkCurrentCandidates(p)) {
// step 3
bool succeeded = assignAndValidate(p);
if (succeeded) {
// step 5
advanceSlot(p);
currentDepth++;
}
else {
// step 4
backtrack(p);
}
}
else {
// step 2
if (reverseSlot(p)) {
currentDepth--;
// step 4
backtrack(p);
}
else {
//printf("step 2 failed\n");
break;
}
}
}
if (!p->isSolved()) {
p->deinitialize();
delete p;
if (testLevel > 1) printf("number of branches: %i\n", (int)toSolve.size());
if (solved == NULL) {
solved = solveInitializedPuzzles(&toSolve, highestVisitedPosition, &numTried);
}
if (testLevel > 1) printf("parallel start depth was %i\n", parallelStartDepth);
}
else {
solved = p;
printf("puzzle solved while creating parallel branches, no parallelism done\n");
}
if (solved != NULL && solved->isSolved()) {
printf("solution found!\n");
solved->printGrid();
}
else {
printf("no solution found\n");
}
solved->deinitialize();
delete solved;
//timer
double seconds_since_start = difftime( time(0), startTime);
printf("%f seconds to run program\n", seconds_since_start);
gettimeofday(&end, NULL);
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
printf("Elapsed time: %ld milliseconds\n", mtime);
}
示例3: main
int main(int argc, char *argv[]) {
// timer
struct timeval start, end;
long mtime, seconds, useconds;
gettimeofday(&start, NULL);
time_t startTime = time(0);
if (argc != 3) {
printf("wrong number of inputs\n");
exit(1);
}
int** grid = fileTo2dArray(argv[1], atoi(argv[2]));
Puzzle p = Puzzle(grid, atoi(argv[2]));
p.initialize();
/*
p->printGridDim();
p->printGrid();
p->printPreassigned();
p->printInitCandidates();
printf("start: %i, %i\n", p->getCurrentRow(), p->getCurrentCol());
while (p->nextSlot()) {
printf("nextslot: row is %i, col is %i\n", p->getCurrentRow(), p->getCurrentCol());
}
while (p->prevSlot()) {
printf("prevslot: row is %i, col is %i\n", p->getCurrentRow(), p->getCurrentCol());
}
printf("TEST: check candidates\n");
p->checkCandidates(p->getCurrentRow(), p->getCurrentCol());
printf("TEST: assign\n");
p->assign(p->getCurrentRow(), p->getCurrentCol());
printf("TEST: update neighbors\n");
p->updateNeighborConflicts(p->getCurrentRow(), p->getCurrentCol(), p->getCurrentAssigned(0, 0), true);
printf("TEST: check neighbors\n");
p->checkNeighborCandidates(p->getCurrentRow(), p->getCurrentCol());
*/
//int counter=10000;
while (!p.isSolved()) {
//while (!p->isSolved() && counter>0) {
//counter--;
if (testLevel > 0) printf("step 1: on (%i, %i)\n", p.getCurrentRow(), p.getCurrentCol());
//printf("step 1: current row %i, current col %i, %i\n", p->getCurrentRow(), p->getCurrentCol(), p->getCurrentAssigned(p->getCurrentRow(), p->getCurrentCol()));
bool hasCandidate = p.checkCandidates(p.getCurrentRow(), p.getCurrentCol(), false);
//printf("step 1\n");
// step 1
if (hasCandidate) {
//printf("step 3\n");
// step 3
bool succeeded;
if (p.assign(p.getCurrentRow(), p.getCurrentCol())) {
if (testLevel > 0)
printf("step 3: on (%i, %i), assigned %i\n", p.getCurrentRow(), p.getCurrentCol(), p.getCurrentAssigned(p.getCurrentRow(), p.getCurrentCol()));
//printf("step 3: assignment succeeded\n");
//printf("calling updateNeighborConflicts with %i, %i, %i\n", p->getCurrentRow(), p->getCurrentCol(), p->getCurrentAssigned(p->getCurrentRow(), p->getCurrentCol()));
//printf("pre-assign candidate list: \n");
//p->printInitCandidates();
// deprecated
//p->updateNeighborConflicts(p->getCurrentRow(), p->getCurrentCol(), p->getCurrentAssigned(p->getCurrentRow(), p->getCurrentCol()), true);
//printf("post-assign candidate list: \n");
//p->printInitCandidates();
//printf("step 3: updateNeighborConflicts succeeded\n");
succeeded = p.checkNeighborAssignments(p.getCurrentRow(), p.getCurrentCol());
}
else {
printf("error: reached slot with no candidates. shouldn't happen. exiting.\n");
exit(1);
}
if (succeeded) {
//printf("step 5\n");
// step 5
if (testLevel > 0) printf("step 5: next slot\n");
p.nextSlot();
p.copyCandidates(p.getCurrentRow(), p.getCurrentCol());
}
else {
//printf("step 4\n");
// step 4
if (testLevel > 0)
printf("step 4: on (%i, %i), unassigning %i\n", p.getCurrentRow(), p.getCurrentCol(), p.getCurrentAssigned(p.getCurrentRow(), p.getCurrentCol()));
//printf("pre-update candidate list: \n");
//p.printInitCandidates();
//deprecated
//p->updateNeighborConflicts(p->getCurrentRow(), p->getCurrentCol(), p->getCurrentAssigned(p->getCurrentRow(), p->getCurrentCol()), false);
//printf("post-update candidate list: \n");
//p->printInitCandidates();
p.removeAndInvalidate(p.getCurrentRow(), p.getCurrentCol());
// loops back to front
}
}
else {
//printf("step 2\n");
// step 2
//.........这里部分代码省略.........