ALDS_1_13_A 8 クイーン
目次
# 問題
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_13_A
# 解説
# 計算量
# 解答
#define MAX_K
#define W 8
Int K;
bool B[W][W];
#define FREE -1
#define NOT_FREE 1
vector<Int> rows(W, FREE), cols(W, FREE), dpos(W*2-1, FREE), dneg(W*2-1, FREE);
void input() {
Int r, c;
cin >> K;
loop(r,0,W) {
loop(c,0,W) {
B[r][c] = false;
}
}
loop(k,0,K) {
cin >> r >> c;
B[r][c] = true;
}
}
void dump() {
// Filter the answer
loop(c,0,W) {
loop(r,0,W) {
if (B[r][c]) {
if (rows[c] != r) return;
}
}
}
loop(c,0,W) {
loop(r,0,W) {
if (rows[r] == c) cout << 'Q';
else cout << '.';
}
cout << endl;
}
}
bool is_free(Int col, Int row) {
return cols[row] == FREE && dpos[col + row] == FREE && dneg[col - row + W - 1] == FREE;
}
void rec(Int col) {
if (col == W) {
dump(); return;
}
loop(row,0,W) {
if (!is_free(col, row)) continue;
rows[col] = row;
cols[row] = dpos[col + row] = dneg[col - row + W - 1] = NOT_FREE;
rec(col + 1);
rows[col] = cols[row] = dpos[col + row] = dneg[col - row + W - 1] = FREE;
}
}
void solve() {
rec(0);
}
int main(void) {
input();
solve();
}