From 41edb890b8e2cfddd9bbc7aeb714afd82015f804 Mon Sep 17 00:00:00 2001 From: "harryhao.zhang" Date: Mon, 18 Mar 2019 19:51:32 +0800 Subject: [PATCH] add printf huarongdao solution result Signed-off-by: harryhao.zhang --- puzzle/huarong.cc | 376 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 327 insertions(+), 49 deletions(-) diff --git a/puzzle/huarong.cc b/puzzle/huarong.cc index eecdce7..dad4f05 100644 --- a/puzzle/huarong.cc +++ b/puzzle/huarong.cc @@ -11,13 +11,34 @@ #include #include +//int board[5][4] = { +// //0 1 2 3 +// { 1, 2, 2, 3, }, // 0 +// { 1, 2, 2, 3, }, // 1 +// { 4, 5, 5, 6, }, // 2 +// { 4, 7, 8, 6, }, // 3 +// { 9, 0, 0, 10 } }; // 4 + +int hrd[5][4] = {{0}}; + +//int board[5][4] = { +// // 0 1 2 3 +// {11, 1, 1, 3}, // 0 +// {12, 1, 1, 3}, // 1 +// { 4, 2, 2,13}, // 2 +// { 4, 6, 6,14}, // 3 +// { 0, 5, 5, 0} }; // 4 + +//华容道第三关 int board[5][4] = { -// 0 1 2 3 - { 1, 2, 2, 3, }, // 0 - { 1, 2, 2, 3, }, // 1 - { 4, 5, 5, 6, }, // 2 - { 4, 7, 8, 6, }, // 3 - { 9, 0, 0, 10 } }; // 4 + // 0 1 2 3 + { 1, 2, 2, 3}, // 0 + { 4, 2, 2, 3}, // 1 + { 5, 6, 6, 7}, // 2 + { 5, 8, 8, 9}, // 3 + { 0,10,10, 0} }; // 4 + + struct Mask; @@ -184,6 +205,8 @@ struct State "func must be callable with a 'const State&' parameter."); const Mask mask = toMask(); + int temp_parents = this->counts; + for (int i = 0; i < kBlocks; ++i) { Block b = blocks_[i]; @@ -193,6 +216,7 @@ struct State && mask.empty(b.top-1, b.right())) { State next = *this; + next.parents = temp_parents; next.step++; next.blocks_[i].top--; func(next); @@ -203,6 +227,7 @@ struct State && mask.empty(b.bottom()+1, b.right())) { State next = *this; + next.parents = temp_parents; next.step++; next.blocks_[i].top++; func(next); @@ -213,6 +238,7 @@ struct State && mask.empty(b.bottom(), b.left-1)) { State next = *this; + next.parents = temp_parents; next.step++; next.blocks_[i].left--; func(next); @@ -223,6 +249,7 @@ struct State && mask.empty(b.bottom(), b.right()+1)) { State next = *this; + next.parents = temp_parents; next.step++; next.blocks_[i].left++; func(next); @@ -233,57 +260,308 @@ struct State // std::vector moves() const; Block blocks_[kBlocks]; + +public: int step = 0; + + int parents = 0; + int counts = 0; }; -int main() +int test_main(int list_count_all_parent[512000], int pcount[], int pstep[]) { - printf("sizeof(Mask) = %zd, sizeof(State) = %zd\n", sizeof(Mask), sizeof(State)); - std::unordered_set seen; - std::deque queue; - - State initial; - initial.blocks_[0] = Block(Shape::kVertical, 0, 0); - initial.blocks_[1] = Block(Shape::kSquare, 1, 0); - initial.blocks_[2] = Block(Shape::kVertical, 3, 0); - initial.blocks_[3] = Block(Shape::kVertical, 0, 2); - initial.blocks_[4] = Block(Shape::kHorizon, 1, 2); - initial.blocks_[5] = Block(Shape::kVertical, 3, 2); - initial.blocks_[6] = Block(Shape::kSingle, 1, 3); - initial.blocks_[7] = Block(Shape::kSingle, 2, 3); - initial.blocks_[8] = Block(Shape::kSingle, 0, 4); - initial.blocks_[9] = Block(Shape::kSingle, 3, 4); - - queue.push_back(initial); - seen.insert(initial.toMask()); - - while (!queue.empty()) - { - const State curr = queue.front(); - queue.pop_front(); + printf("sizeof(Mask) = %zd, sizeof(State) = %zd\n", sizeof(Mask), sizeof(State)); + std::unordered_set seen; + std::deque queue; + + State initial; + + // int board[5][4] = { + // //0 1 2 3 + // { 1, 2, 2, 3, }, // 0 + // { 1, 2, 2, 3, }, // 1 + // { 4, 5, 5, 6, }, // 2 + // { 4, 7, 8, 6, }, // 3 + // { 9, 0, 0, 10 } }; // 4 + // initial.blocks_[0] = Block(Shape::kVertical, 0, 0); + // initial.blocks_[1] = Block(Shape::kSquare, 1, 0); + // initial.blocks_[2] = Block(Shape::kVertical, 3, 0); + // initial.blocks_[3] = Block(Shape::kVertical, 0, 2); + // initial.blocks_[4] = Block(Shape::kHorizon, 1, 2); + // initial.blocks_[5] = Block(Shape::kVertical, 3, 2); + // initial.blocks_[6] = Block(Shape::kSingle, 1, 3); + // initial.blocks_[7] = Block(Shape::kSingle, 2, 3); + // initial.blocks_[8] = Block(Shape::kSingle, 0, 4); + // initial.blocks_[9] = Block(Shape::kSingle, 3, 4); + + // int board[5][4] = { + // //0 1 2 3 + // { 1, 2, 2, 3}, // 0 + // { 4, 2, 2, 3}, // 1 + // { 5, 6, 6, 7}, // 2 + // { 5, 8, 8, 9}, // 3 + // { 0,10,10, 0} }; // 4 + initial.blocks_[0] = Block(Shape::kSingle, 0, 0); + initial.blocks_[1] = Block(Shape::kSquare, 1, 0); + initial.blocks_[2] = Block(Shape::kVertical, 3, 0); + initial.blocks_[3] = Block(Shape::kSingle, 0, 1); + initial.blocks_[4] = Block(Shape::kVertical, 0, 2); + initial.blocks_[5] = Block(Shape::kHorizon, 1, 2); + initial.blocks_[6] = Block(Shape::kSingle, 3, 2); + initial.blocks_[7] = Block(Shape::kHorizon, 1, 3); + initial.blocks_[8] = Block(Shape::kSingle, 3, 3); + initial.blocks_[9] = Block(Shape::kHorizon, 1, 4); + + queue.push_back(initial); + seen.insert(initial.toMask()); + + int i = 0; + int count = 1; + int temp_count = 0; + + while (!queue.empty()) + { + State curr = queue.front(); + queue.pop_front(); + + curr.counts = count; + + list_count_all_parent[curr.counts] = curr.parents; + + if (curr.isSolved()) + { + + temp_count = curr.counts; + for(i=0; i 500) + { + printf("too many steps.\n"); + break; + } + + count = count + 1; + curr.move([&seen, &queue](const State& next) { + auto result = seen.insert(next.toMask()); + if (result.second) + queue.push_back(next); + }); + + // for (const State& next : curr.moves()) + // { + // auto result = seen.insert(next.toMask()); + // if (result.second) + // queue.push_back(next); + // } + + } +} + + - if (curr.isSolved()) +int test_main_show(int list_count_sort[500]) +{ + printf("sizeof(Mask) = %zd, sizeof(State) = %zd\n", sizeof(Mask), sizeof(State)); + std::unordered_set seen; + std::deque queue; + + State initial; + + // int board[5][4] = { + // //0 1 2 3 + // { 1, 2, 2, 3, }, // 0 + // { 1, 2, 2, 3, }, // 1 + // { 4, 5, 5, 6, }, // 2 + // { 4, 7, 8, 6, }, // 3 + // { 9, 0, 0, 10 } }; // 4 + // initial.blocks_[0] = Block(Shape::kVertical, 0, 0); + // initial.blocks_[1] = Block(Shape::kSquare, 1, 0); + // initial.blocks_[2] = Block(Shape::kVertical, 3, 0); + // initial.blocks_[3] = Block(Shape::kVertical, 0, 2); + // initial.blocks_[4] = Block(Shape::kHorizon, 1, 2); + // initial.blocks_[5] = Block(Shape::kVertical, 3, 2); + // initial.blocks_[6] = Block(Shape::kSingle, 1, 3); + // initial.blocks_[7] = Block(Shape::kSingle, 2, 3); + // initial.blocks_[8] = Block(Shape::kSingle, 0, 4); + // initial.blocks_[9] = Block(Shape::kSingle, 3, 4); + + // int board[5][4] = { + // //0 1 2 3 + // { 1, 2, 2, 3}, // 0 + // { 4, 2, 2, 3}, // 1 + // { 5, 6, 6, 7}, // 2 + // { 5, 8, 8, 9}, // 3 + // { 0,10,10, 0} }; // 4 + initial.blocks_[0] = Block(Shape::kSingle, 0, 0); + initial.blocks_[1] = Block(Shape::kSquare, 1, 0); + initial.blocks_[2] = Block(Shape::kVertical, 3, 0); + initial.blocks_[3] = Block(Shape::kSingle, 0, 1); + initial.blocks_[4] = Block(Shape::kVertical, 0, 2); + initial.blocks_[5] = Block(Shape::kHorizon, 1, 2); + initial.blocks_[6] = Block(Shape::kSingle, 3, 2); + initial.blocks_[7] = Block(Shape::kHorizon, 1, 3); + initial.blocks_[8] = Block(Shape::kSingle, 3, 3); + initial.blocks_[9] = Block(Shape::kHorizon, 1, 4); + + queue.push_back(initial); + seen.insert(initial.toMask()); + + int i = 0; + int j = 0; + int count = 1; + + while (!queue.empty()) { - printf("found solution with %d steps\n", curr.step); - break; + State curr = queue.front(); + queue.pop_front(); + + curr.counts = count; + + for(i=0; i<5; i++) + { + for(j=0; j<4; j++) + { + hrd[i][j] = 0; + } + } + for(i=0; i<10; i++) + { + //printf("%d %2d %2d \r\n",curr.blocks_[i].shape, curr.blocks_[i].left, curr.blocks_[i].top); + hrd[curr.blocks_[i].top][curr.blocks_[i].left] = (int)curr.blocks_[i].shape; + // kInvalid, + // kSingle, + // kHorizon, + // kVertical, + // kSquare, + if(curr.blocks_[i].shape == Shape::kSingle) + { + hrd[curr.blocks_[i].top][curr.blocks_[i].left] = (int)Shape::kSingle; + } + if(curr.blocks_[i].shape == Shape::kHorizon) + { + hrd[curr.blocks_[i].top][curr.blocks_[i].left+1] = (int)Shape::kHorizon; + } + if(curr.blocks_[i].shape == Shape::kVertical) + { + hrd[curr.blocks_[i].top+1][curr.blocks_[i].left] = (int)Shape::kVertical; + } + if(curr.blocks_[i].shape == Shape::kSquare) + { + hrd[curr.blocks_[i].top][curr.blocks_[i].left+1] = (int)Shape::kSquare; + hrd[curr.blocks_[i].top+1][curr.blocks_[i].left] = (int)Shape::kSquare; + hrd[curr.blocks_[i].top+1][curr.blocks_[i].left+1] = (int)Shape::kSquare; + } + + } + // printf("curr.step = %d count = %d\r\n", curr.step, count); + if(list_count_sort[curr.step] == curr.counts) + { + printf("curr.step = %d curr.counts = %d\r\n", curr.step, curr.counts); + for(i=0; i<5; i++) + { + for(j=0; j<4; j++) + { + printf("%2d ",hrd[i][j]); + } + printf("\r\n"); + } + printf("\r\n"); + } + + if (curr.isSolved()) + { + printf("found solution with %d steps count = %d\r\n", curr.step, count); + + break; + } + else if (curr.step > 500) + { + printf("too many steps.\n"); + break; + } + + count = count + 1; + curr.move([&seen, &queue](const State& next) { + auto result = seen.insert(next.toMask()); + if (result.second) + queue.push_back(next); + }); + + // for (const State& next : curr.moves()) + // { + // auto result = seen.insert(next.toMask()); + // if (result.second) + // queue.push_back(next); + // } + } - else if (curr.step > 200) +} + +int test_list_sort(int list_count_all_parent[512000], int mcount, int mstep, int list_count_sort[500]) +{ + int temp_sort = 0; + int i = 0; + int j = 0; + + printf("mcount = %d\r\n", mcount); + temp_sort = mcount; + list_count_sort[mstep] = temp_sort; + for(j=mstep - 1; j>=0; j--) { - printf("too many steps.\n"); - break; + //printf("temp_sort = %d\r\n", temp_sort); + list_count_sort[j] = list_count_all_parent[temp_sort]; + temp_sort = list_count_all_parent[temp_sort]; } - curr.move([&seen, &queue](const State& next) { - auto result = seen.insert(next.toMask()); - if (result.second) - queue.push_back(next); - }); - - // for (const State& next : curr.moves()) - // { - // auto result = seen.insert(next.toMask()); - // if (result.second) - // queue.push_back(next); - // } - } + printf("list_count_sort:\r\n"); + for(i = 0; i