inline static int PushAllBrickToTheTail(std::vector<int> & tmpImageSingleRow, const std::vector<int> & brickList, unsigned int thisBrickIndexUpperBound) { std::vector<int> cpyBrickList = brickList; unsigned int thisBrickIndexReverseLowerBound = tmpImageSingleRow.size() - 1 - thisBrickIndexUpperBound; reverseVec(tmpImageSingleRow); reverseVec(cpyBrickList); int pushStatus = PushAllBrickToTheHead(tmpImageSingleRow, cpyBrickList, thisBrickIndexReverseLowerBound); reverseVec(tmpImageSingleRow); return pushStatus; }
int main(int argc, char *argv[]) { int i, tamanho; unsigned int *file = NULL; char *nome; FILE *fp_input; nome = readLine(); //recebe o nome do arquivo fp_input = fopen(nome, "rb");//abre o arquivo com o nome informado //usa-se "rb" pois o arquivo está salvo como binário(.bin) if(fp_input == NULL) noFile(nome, fp_input);//executa a função caso o arquivo esteja vazio tamanho = size(fp_input); //recebe o tamanho, em bytes, do arquivo file = alocateFile(tamanho, fp_input); //recebe o conteúdo do arquivo reverseVec(file, tamanho); //Função para desinverter o vetor chooseFunction(file, tamanho, nome); //Função para o usuário escolher a operação a ser executada //É liberado tudo que foi salvo na Heap. free(file); free(nome); return 0; }
static int GetOneBrickAllPossiblePosition(std::vector<std::vector<int> > & result, const std::vector<int> & puzzleSingleRow, const std::vector<int> & tmpImageSingleRow, const unsigned int brickId) { // instantly used values int rowLength = tmpImageSingleRow.size(); int thisBrickSize = puzzleSingleRow[brickId]; std::vector<int> leftList, rightList; for (unsigned int i = 0; i < brickId; i++) { leftList.push_back(puzzleSingleRow[i]); } for (unsigned int i = brickId + 1; i < puzzleSingleRow.size(); i++) { rightList.push_back(puzzleSingleRow[i]); } #if DEBUG == 3 std::cout << "ThisBrick: " << puzzleSingleRow[brickId] << "\n"; std::cout << "left list: "; for (unsigned int i = 0; i < leftList.size(); i++) {std::cout << leftList[i] << " ";} std::cout << "\n"; std::cout << "right list: "; for (unsigned int i = 0; i < rightList.size(); i++) {std::cout << rightList[i] << " ";} std::cout << "\n"; std::cout << "brickId: " << brickId << "\n"; #endif #if DEBUG == 3 std::cout << "tmpImageSingleRow: \n"; for (unsigned int j = 0; j < tmpImageSingleRow.size(); j++) { switch (tmpImageSingleRow[j]) { case NONOGRAM_IMAGE_CONST_SELECTED: std::cout << '#'; break; case NONOGRAM_IMAGE_CONST_UNSELECTED: std::cout << '.'; break; default: std::cout << '@'; break; } } std::cout << "\n"; #endif // get start upper bound and lower bound std::vector<int> leftListIncludeThisBrick; std::vector<int> rightListIncludeThisBrick; leftListIncludeThisBrick = leftList; leftListIncludeThisBrick.push_back(puzzleSingleRow[brickId]); rightListIncludeThisBrick = rightList; rightListIncludeThisBrick.insert(rightListIncludeThisBrick.begin(), puzzleSingleRow[brickId]); int brickStartLowerBound, brickStartUpperBound; std::vector<int> helpDetermineBoundImage; helpDetermineBoundImage = tmpImageSingleRow; PushAllBrickToTheHead(helpDetermineBoundImage, leftListIncludeThisBrick, 0); #if DEBUG == 3 std::cout << "helpDetermineBoundImage, head: "; for (unsigned int j = 0; j < helpDetermineBoundImage.size(); j++) { std::cout << std::setw(2) << helpDetermineBoundImage[j] << " "; } std::cout << "\n"; #endif brickStartLowerBound = getBrickStart(helpDetermineBoundImage, leftList.size()); helpDetermineBoundImage = tmpImageSingleRow; PushAllBrickToTheTail(helpDetermineBoundImage, rightListIncludeThisBrick, rowLength - 1); #if DEBUG == 3 std::cout << "helpDetermineBoundImage, tail: "; for (unsigned int j = 0; j < helpDetermineBoundImage.size(); j++) { std::cout << std::setw(2) << helpDetermineBoundImage[j] << " "; } std::cout << "\n"; #endif reverseVec(helpDetermineBoundImage); int revBrickStartUpperBound = getBrickStart(helpDetermineBoundImage, rightList.size()); // reverse(helpDetermineBoundImage); brickStartUpperBound = rowLength - revBrickStartUpperBound - thisBrickSize; #if DEBUG == 3 std::cout << "brickStartLowerBound = " << brickStartLowerBound << ", brickStartUpperBound = " << brickStartUpperBound << "\n"; #endif std::vector<int> validBrickStart; validBrickStart.clear(); validBrickStart.resize(rowLength, 1); // check validity (simple) for (int i = brickStartLowerBound; i <= brickStartUpperBound; i++) { for (int j = 0; j < thisBrickSize;j++) { if (tmpImageSingleRow[i + j] == NONOGRAM_IMAGE_CONST_UNSELECTED) { validBrickStart[i] = 0; } } } /* if (puzzleSingleRow.size() == 1) { std::vector<int> helpCheckValidity; for (int i = brickStartLowerBound; i <= brickStartUpperBound; i++) { helpCheckValidity = tmpImageSingleRow; for (int j = 0; j < thisBrickSize;j++) { helpCheckValidity[i + j] = NONOGRAM_IMAGE_CONST_SELECTED; } int selectedCount = 0; for (unsigned int j = 0; j < helpCheckValidity.size(); j++) { if (helpCheckValidity[j] == NONOGRAM_IMAGE_CONST_SELECTED) { selectedCount++; } } if (selectedCount != thisBrickSize) { validBrickStart[i] = 0; } } } */ std::vector<int> oneResult; for (int i = brickStartLowerBound; i <= brickStartUpperBound; i++) { if (validBrickStart[i]) { // find one possible, write answer // -1 means do not put oneResult.clear(); oneResult.resize(rowLength, -1); for (int j = 0; j < thisBrickSize;j++) { oneResult[i + j] = brickId; } #if DEBUG == -2 std::cout << "FindPossible: \n"; for (unsigned int j = 0; j < oneResult.size(); j++) { std::cout << std::setw(2) << oneResult[j] << " "; } std::cout << "\n"; system("pause"); #endif result.push_back(oneResult); } } return NONOGRAM_IMAGE_LOGIC_SOLVE_CONST_UNDETERMINED; /* int thisBrickSize = puzzleSingleRow[brickId]; int rowLength = tmpImageSingleRow.size(); int brickStartUpperBound = rowLength - thisBrickSize; std::vector<int> helpPutBrickIn; std::vector<int> helpJudgePuzzleSingleRow; std::vector<int> validBrickStart; validBrickStart.clear(); validBrickStart.resize(rowLength, 0); std::vector<int> afterHeadTail; for (int i = 0; i <= brickStartUpperBound; i++) { helpPutBrickIn = tmpImageSingleRow; // put this brick in for (int j = 0; j < thisBrickSize;j++) { helpPutBrickIn[i + j] = NONOGRAM_IMAGE_CONST_SELECTED; } afterHeadTail = helpPutBrickIn; // headTail PushAllBrickToTheHead(afterHeadTail, leftList, 0); PushAllBrickToTheTail(afterHeadTail, rightList, rowLength - 1); // fill for (int j = 0; j < rowLength; j++) { if (afterHeadTail[j] == NONOGRAM_IMAGE_CONST_UNDETERMINED) { afterHeadTail[j] = NONOGRAM_IMAGE_CONST_UNSELECTED; } } #if DEBUG == 3 std::cout << "Current: \n"; for (unsigned int j = 0; j < afterHeadTail.size(); j++) { switch (afterHeadTail[j]) { case NONOGRAM_IMAGE_CONST_SELECTED: std::cout << '#'; break; case NONOGRAM_IMAGE_CONST_UNSELECTED: std::cout << '.'; break; default: std::cout << '@'; break; } } std::cout << "\n"; #endif // check whether valid ImageSingleRowToPuzzleSingleRow(afterHeadTail, helpJudgePuzzleSingleRow); if (helpJudgePuzzleSingleRow == puzzleSingleRow) { int realBrickStart = 0; realBrickStart = getBrickStart(afterHeadTail, brickId); validBrickStart[realBrickStart] = 1; #if DEBUG == 3 std::cout << "is valid, realBrickStart = " << realBrickStart << "\n"; #endif } } std::vector<int> oneResult; for (int i = 0; i <= brickStartUpperBound; i++) { if (validBrickStart[i]) { // find one possible, write answer // -1 means do not put oneResult.clear(); oneResult.resize(rowLength, -1); for (int j = 0; j < thisBrickSize;j++) { oneResult[i + j] = brickId; } #if DEBUG == 3 std::cout << "FindPossible: \n"; for (unsigned int j = 0; j < oneResult.size(); j++) { std::cout << std::setw(2) << oneResult[j] << " "; } std::cout << "\n"; system("pause"); #endif result.push_back(oneResult); } } */ }