From 646343f71ae5948fbe62dac7621be014ab59be59 Mon Sep 17 00:00:00 2001 From: Seng-Jik <853974536@qq.com> Date: Fri, 12 Jun 2020 12:54:14 +0800 Subject: [PATCH] =?UTF-8?q?C++=E6=A8=A1=E6=9D=BF=E5=85=83=E7=BC=96?= =?UTF-8?q?=E7=A8=8B=E7=BC=96=E8=AF=91=E6=9C=9FN=E7=9A=87=E5=90=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...N\347\232\207\345\220\216\350\247\243.cpp" | 239 ++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 "\346\250\241\346\235\277\345\205\203\347\274\226\347\250\213\347\274\226\350\257\221\346\234\237N\347\232\207\345\220\216\350\247\243.cpp" diff --git "a/\346\250\241\346\235\277\345\205\203\347\274\226\347\250\213\347\274\226\350\257\221\346\234\237N\347\232\207\345\220\216\350\247\243.cpp" "b/\346\250\241\346\235\277\345\205\203\347\274\226\347\250\213\347\274\226\350\257\221\346\234\237N\347\232\207\345\220\216\350\247\243.cpp" new file mode 100644 index 0000000..6cacf01 --- /dev/null +++ "b/\346\250\241\346\235\277\345\205\203\347\274\226\347\250\213\347\274\226\350\257\221\346\234\237N\347\232\207\345\220\216\350\247\243.cpp" @@ -0,0 +1,239 @@ +#include +#include + +using namespace std; + +template +struct Factorial +{ + constexpr static int Value = N * Factorial::Value; +}; + +template <> +struct Factorial<0> +{ + constexpr static int Value = 1; +}; + +template < + size_t Index, + size_t N> +struct RemoveElementAt +{ +private: + template < + size_t... Fis, + size_t... Bis> + + constexpr array RemoveElementImpl( + array arr, + index_sequence, + index_sequence) + { + return array + { + get(arr)..., + get(arr)... + }; + } + +public: + constexpr array operator()(array arr) + { + return RemoveElementImpl( + arr, + make_index_sequence{}, + make_index_sequence{}); + } +}; + +template < + int N, + typename T, + size_t... is> +constexpr array PushFrontArray(T a, array arr, index_sequence) +{ + return array + { + a, + get(arr)... + }; +} + +template < + int N, + int M, + size_t... Fis, + size_t... Bis, + typename T> +constexpr array UnionArray( + array a1, + array a2, + index_sequence, + index_sequence) +{ + return array + { + get(a1)..., + get(a2)... + }; +} + +template < + int Index, + int N> +constexpr array InvCantor(array indics) +{ + + constexpr auto curIndex = Index / Factorial::Value; + const auto curNumber = get(indics); + + if constexpr(N - 1 > 0) + { + const auto newIndics = RemoveElementAt{}(indics); + const auto child = InvCantor::Value, N - 1>(newIndics); + + return PushFrontArray(curNumber, child, make_index_sequence{}); + } + else + { + return array{curNumber }; + } +} + +template +constexpr array BuildMainIndicis(integer_sequence) +{ + return array + { + is... + }; +} + + +constexpr int Abs(int N) +{ + return N < 0 ? -N : N; +} + +template +constexpr bool Check(array queens) +{ + for (int i = 0; i < N; ++i) + { + for (int j = 0; j < N; ++j) + { + if (i != j) + { + const int p = queens[i] - queens[j]; + const int q = i - j; + if (Abs(p) == Abs(q)) + { + return false; + } + } + } + } + return true; +} + +constexpr int QueenCount = 7; + +template +constexpr auto BuildLine(int index) +{ + const array child = BuildLine(index); + + if(index == C - 1) + return PushFrontArray('*', child, make_index_sequence{}); + else + return PushFrontArray('0', child, make_index_sequence{}); +} + +template <> +constexpr auto BuildLine<0>(int) +{ + return array{}; +} + +template +constexpr auto BuildGraph(array arr) +{ + if constexpr(L >= 1) + { + const auto line = BuildLine(get(arr)); + const auto endLined = PushFrontArray('\n', line, make_index_sequence{}); + const auto child = BuildGraph(arr); + return UnionArray( + endLined, + child, + make_index_sequence{}, + make_index_sequence{}); + } + else + { + return array{}; + } +} + +template +void PrintGraph(array arr) +{ + for (int i = 0; i < N; ++i) + cout << arr[i]; + cin.get(); +} + +template +constexpr void MainFor() +{ + if constexpr (Top >= Bottom) + { + constexpr array t = + BuildMainIndicis(make_integer_sequence{}); + constexpr auto current = InvCantor(t); + + constexpr bool check = Check(current); + + if constexpr(check) + { + constexpr auto graph = BuildGraph(current); + PrintGraph(graph); + } + + MainFor(); + } +} + +template +constexpr void MainForSplited() +{ + constexpr int space = A - B; + constexpr int layerA = space / 2; + constexpr int layerB = space - layerA; + + constexpr int spaceAB = B; + constexpr int spaceAA = B + layerB; + constexpr int spaceBB = spaceAA + 1; + constexpr int spaceBA = spaceBB + layerA - 1; + + if constexpr(Layer == 0) + { + MainFor(); + MainFor(); + } + else + { + MainForSplited(); + MainForSplited(); + } +} + +int main() +{ + constexpr auto all = Factorial::Value - 1; + + MainForSplited(); + + return 0; +} -- Gitee