Queens problem

3

2 votes
Basic Math, Algorithms, Mathamatics, Math
Problem

You are trying to solve the famous N queens problem. You broke one of the queens from your chess pieces collections. Now, you have created the following puzzle:

You are given an N×N chessboard. Is it possible to place N1 queens on it in such a way that no pair of queens is attacking each other?

Two queens are attacking each other if at least one of the following conditions is true:

  • They share the same row
  • They share the same column
  • They share the same diagonal

You are given a single integer N. Print any possible configuration in the format below. It is guaranteed that there is always an answer.

Input format

The first and only line consists of a single integer N.

Output format

Print N lines consisting of N space-separated characters where a '' represents a queen, and a '.' represents empty space. You can print extra spaces and newlines.

If there are multiple valid answers, then you can print any of them.

Constraints 

2N1000

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

Notice this is not the only answer for N = 4

Editor Image

?