Advertisement
Guest User

Chogo8Q

a guest
May 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.56 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. namespace CSharp_Shell
  7. {
  8.  
  9.     public static class Program
  10.     {
  11.        
  12.         static void printBoard(string [, ] board) {
  13.             for (int i = 0; i < 4; i++) {
  14.                 for (int j = 0; j < 4; j++) {
  15.                     Console.Write(board[i, j] + " ");
  16.                 }
  17.                 Console.Write("\n");
  18.             }
  19.         }
  20.        
  21.         static bool checkIfSafe(string [,] board, int row, int col)
  22.         {
  23.           //check if all cols in current row are safe
  24.           for(int i = col; i < 4; i++)
  25.           {
  26.              if(board[row, i] == "Q")
  27.                 {Console.WriteLine("This row is not safe");
  28.                 return false;}
  29.           }
  30.          
  31.           //check all rows in current col
  32.           for(int i = row; i < 4; i++)
  33.           {
  34.              if(board[i, col] == "Q"){
  35.                 Console.WriteLine("This col is not safe");
  36.                 return false;}
  37.           }
  38.          
  39.           //check diagonals to the left\
  40.          
  41.           for(int i = row, j = col; i >= 0 && j >= 0; i--, j--)
  42.           {
  43.               if(board[i, j] == "Q"){
  44.                  Console.WriteLine("Left Diag not safe");
  45.                  return false;}
  46.           }
  47.          
  48.           //check diagonals to the right
  49.           for(int i = row, j = col; i < 4 && j < 4; i++, j++)
  50.           {
  51.               if(board[i, j] == "Q")
  52.                  {Console.WriteLine("Right Diag not safe");
  53.                  return false;}
  54.           }
  55.          
  56.                     //check diagonals to the left downwards
  57.           for(int i = row, j = col; i < 4 && j >= 0; i++, j--)
  58.           {
  59.               if(board[i, j] == "Q")
  60.                  {Console.WriteLine("Left Diag downwards not safe");
  61.                  return false;}
  62.           }
  63.          
  64.                     //check diagonals to the right upwards
  65.           for(int i = row, j = col; i >= 0 && j < 4; i--, j++)
  66.           {
  67.               if(board[i, j] == "Q")
  68.                  {Console.WriteLine("Right Diag upwards not safe");
  69.                  return false;}
  70.           }
  71.          
  72.           //check directly upwards
  73.           for(int i = row; i >= 0; i-- )
  74.           {
  75.               if(board[i, col] == "Q")
  76.                 { Console.WriteLine("Upside not safe");
  77.                
  78.                 return false;}
  79.           }
  80.          
  81.                     //check directly left
  82.           for(int i = col; i >= 0; i-- )
  83.           {
  84.               if(board[row, i] == "Q")
  85.                 { Console.WriteLine("Left not safe");
  86.                
  87.                 return false;}
  88.           }
  89.           return true;
  90.          
  91.         }
  92.         static bool Solve8queen(string[,] board, int col)
  93.         {
  94.             //base case
  95.            
  96.             if(col >= 4)
  97.             {
  98.                 //return true;
  99.                 printBoard(board);
  100.                 return true;
  101.             }
  102.            
  103.             //loop over the rows in the current col
  104.              bool res = false;
  105.            
  106.             for(int i = 0; i < 4; i++)
  107.             {
  108.                 if(board[i, col] == "Q"){
  109.                  // i ++;
  110.                   col = 0;
  111.                  
  112.                   Console.WriteLine("row " + i + " col " + col + " is occupied");
  113.                 }
  114.            
  115.        
  116.                 if(checkIfSafe(board, i, col))
  117.                 {
  118.                     //place queen
  119.                     board[i, col] = "Q";
  120.                    
  121.                     //explore other solns
  122.                    
  123.                     res = Solve8queen(board, col + 1) || res;
  124.                    
  125.                     //backtrack
  126.                    
  127.                     board[i, col] = ".";
  128.                 }
  129.                 else
  130.                 {
  131.                     //col++;
  132.                 }
  133.             }
  134.            
  135.             return false;
  136.         }
  137.          
  138.        
  139.         public static void Main()
  140.         {
  141.            
  142.            string[,] board = {
  143.                         {".", "Q", ".", "."},
  144.                         {".", ".", ".", "."},
  145.                         {".", ".", ".", "."},
  146.                         {".", ".", ".", "."}
  147.                      
  148.                       };
  149.                      
  150.             //Console.WriteLine(checkIfSafe(board, 0, 1));
  151.            
  152.             Solve8queen(board, 0);
  153.             //printBoard(board);
  154.            
  155.             //Console.WriteLine(checkIfSafe(board, 0, 2));
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement