Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program{
- public static void Main(){
- int n = int.Parse(Console.ReadLine());
- if((n == 2) || (n == 3)){
- Console.WriteLine("Impossible");
- Console.ReadKey();
- return ;
- }
- char[ , ] a = new char [n , n];
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n; j++){
- a[i , j] = '.';
- }
- }
- bool ok = false;
- int y = 0;
- bool[] used = new bool[40* n + 2];
- int[] left = new int[40 * n];
- int[] right = new int[40 * n];
- for(int i = 0; i < n; i++){
- solve(i , y , n , ref ok , ref left , ref right , ref used, ref a);
- if(ok)
- break;
- }
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n; j++){
- Console.Write(a[i , j]);
- }
- Console.Write('\n');
- }
- Console.ReadKey();
- }
- public static void solve(int x , int y , int n, ref bool ok , ref int[] left , ref int[] right , ref bool[] used , ref char[,] a){
- if(ok == true){
- return ;
- }
- if(y == (n - 1)){
- a[x , y] = 'Q';
- ok = true;
- return ;
- }
- a[x , y] = 'Q';
- used[x] = true;
- left[3 * n - x - y]++;
- right[3 * n + x - y]++;
- for(int i = 0; i < n; i++){
- if(used[i] == false){
- if((left[3 * n - i - (y + 1)] == 0) && (right[3 * n + i - (y + 1)] == 0)){
- solve(i , (y + 1) , n , ref ok , ref left , ref right , ref used , ref a);
- if(ok == true){
- return ;
- }
- }
- }
- }
- used[x] = false;
- left[3 * n - x - y]--;
- right[3 * n + x - y]--;
- a[x , y] = '.';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement