1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -fsyntax-only %s
2*f4a2713aSLionel Sambuc
3*f4a2713aSLionel Sambuc typedef unsigned long uint64_t;
4*f4a2713aSLionel Sambuc
5*f4a2713aSLionel Sambuc struct Board {
6*f4a2713aSLionel Sambuc uint64_t State;
7*f4a2713aSLionel Sambuc bool Failed;
8*f4a2713aSLionel Sambuc
BoardBoard9*f4a2713aSLionel Sambuc constexpr Board() : State(0), Failed(false) {}
BoardBoard10*f4a2713aSLionel Sambuc constexpr Board(const Board &O) : State(O.State), Failed(O.Failed) {}
BoardBoard11*f4a2713aSLionel Sambuc constexpr Board(uint64_t State, bool Failed = false) :
12*f4a2713aSLionel Sambuc State(State), Failed(Failed) {}
addQueenBoard13*f4a2713aSLionel Sambuc constexpr Board addQueen(int Row, int Col) {
14*f4a2713aSLionel Sambuc return Board(State | ((uint64_t)Row << (Col * 4)));
15*f4a2713aSLionel Sambuc }
getQueenRowBoard16*f4a2713aSLionel Sambuc constexpr int getQueenRow(int Col) {
17*f4a2713aSLionel Sambuc return (State >> (Col * 4)) & 0xf;
18*f4a2713aSLionel Sambuc }
okBoard19*f4a2713aSLionel Sambuc constexpr bool ok(int Row, int Col) {
20*f4a2713aSLionel Sambuc return okRecurse(Row, Col, 0);
21*f4a2713aSLionel Sambuc }
okRecurseBoard22*f4a2713aSLionel Sambuc constexpr bool okRecurse(int Row, int Col, int CheckCol) {
23*f4a2713aSLionel Sambuc return Col == CheckCol ? true :
24*f4a2713aSLionel Sambuc getQueenRow(CheckCol) == Row ? false :
25*f4a2713aSLionel Sambuc getQueenRow(CheckCol) == Row + (Col - CheckCol) ? false :
26*f4a2713aSLionel Sambuc getQueenRow(CheckCol) == Row + (CheckCol - Col) ? false :
27*f4a2713aSLionel Sambuc okRecurse(Row, Col, CheckCol + 1);
28*f4a2713aSLionel Sambuc }
atBoard29*f4a2713aSLionel Sambuc constexpr bool at(int Row, int Col) {
30*f4a2713aSLionel Sambuc return getQueenRow(Col) == Row;
31*f4a2713aSLionel Sambuc }
32*f4a2713aSLionel Sambuc constexpr bool check(const char *, int=0, int=0);
33*f4a2713aSLionel Sambuc };
34*f4a2713aSLionel Sambuc
35*f4a2713aSLionel Sambuc constexpr Board buildBoardRecurse(int N, int Col, const Board &B);
36*f4a2713aSLionel Sambuc constexpr Board buildBoardScan(int N, int Col, int Row, const Board &B);
tryBoard(const Board & Try,int N,int Col,int Row,const Board & B)37*f4a2713aSLionel Sambuc constexpr Board tryBoard(const Board &Try,
38*f4a2713aSLionel Sambuc int N, int Col, int Row, const Board &B) {
39*f4a2713aSLionel Sambuc return Try.Failed ? buildBoardScan(N, Col, Row, B) : Try;
40*f4a2713aSLionel Sambuc }
buildBoardScan(int N,int Col,int Row,const Board & B)41*f4a2713aSLionel Sambuc constexpr Board buildBoardScan(int N, int Col, int Row, const Board &B) {
42*f4a2713aSLionel Sambuc return Row == N ? Board(0, true) :
43*f4a2713aSLionel Sambuc B.ok(Row, Col) ?
44*f4a2713aSLionel Sambuc tryBoard(buildBoardRecurse(N, Col + 1, B.addQueen(Row, Col)),
45*f4a2713aSLionel Sambuc N, Col, Row+1, B) :
46*f4a2713aSLionel Sambuc buildBoardScan(N, Col, Row + 1, B);
47*f4a2713aSLionel Sambuc }
buildBoardRecurse(int N,int Col,const Board & B)48*f4a2713aSLionel Sambuc constexpr Board buildBoardRecurse(int N, int Col, const Board &B) {
49*f4a2713aSLionel Sambuc return Col == N ? B : buildBoardScan(N, Col, 0, B);
50*f4a2713aSLionel Sambuc }
buildBoard(int N)51*f4a2713aSLionel Sambuc constexpr Board buildBoard(int N) {
52*f4a2713aSLionel Sambuc return buildBoardRecurse(N, 0, Board());
53*f4a2713aSLionel Sambuc }
54*f4a2713aSLionel Sambuc
55*f4a2713aSLionel Sambuc constexpr Board q8 = buildBoard(8);
56*f4a2713aSLionel Sambuc
check(const char * p,int Row,int Col)57*f4a2713aSLionel Sambuc constexpr bool Board::check(const char *p, int Row, int Col) {
58*f4a2713aSLionel Sambuc return
59*f4a2713aSLionel Sambuc *p == '\n' ? check(p+1, Row+1, 0) :
60*f4a2713aSLionel Sambuc *p == 'o' ? at(Row, Col) && check(p+1, Row, Col+1) :
61*f4a2713aSLionel Sambuc *p == '-' ? !at(Row, Col) && check(p+1, Row, Col+1) :
62*f4a2713aSLionel Sambuc *p == 0 ? true :
63*f4a2713aSLionel Sambuc false;
64*f4a2713aSLionel Sambuc }
65*f4a2713aSLionel Sambuc static_assert(q8.check(
66*f4a2713aSLionel Sambuc "o-------\n"
67*f4a2713aSLionel Sambuc "------o-\n"
68*f4a2713aSLionel Sambuc "----o---\n"
69*f4a2713aSLionel Sambuc "-------o\n"
70*f4a2713aSLionel Sambuc "-o------\n"
71*f4a2713aSLionel Sambuc "---o----\n"
72*f4a2713aSLionel Sambuc "-----o--\n"
73*f4a2713aSLionel Sambuc "--o-----\n"), "");
74