Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Assignment_2___Question_4{
- class Program{
- static void Main(string[] args){
- int[,] line = { { 6, 3, 4, 9, 1, 5, 7, 2, 8 }, { 2, 7, 8, 6, 4, 3, 5, 1, 9 }, { 5, 9, 1, 2, 7, 8, 6, 4, 3 }, { 4, 5, 7, 3, 6, 9, 2, 8, 1 }, { 9, 8, 6, 4, 2, 1, 3, 5, 7 }, { 3, 1, 2, 8, 5, 7, 4, 9, 6 }, { 1, 2, 5, 7, 8, 6, 9, 3, 4 }, { 8, 6, 3, 5, 9, 4, 1, 7, 2 }, { 7, 4, 9, 1, 3, 2, 8, 6, 5 } };
- Console.WriteLine("---------------------------------------");
- for (int i = 0; i < 9; i++){
- for (int j = 0; j < 9; j++){
- Console.Write(" | "+line[i,j]);
- }
- Console.WriteLine(" |");
- Console.WriteLine("---------------------------------------");
- }
- if (CheckAllLines(line) && CheckAllColls(line) && CheckAllBlocks(line)){
- Console.WriteLine("This solution is correct!");
- }else{
- Console.WriteLine("This solution is wrong!");
- }
- Console.ReadKey();
- }
- public static bool LineCorrectness(int[] lineArray, int count = 0){
- return count > 8 || (!lineArray.Skip(count+1).Contains(lineArray[count]) && LineCorrectness(lineArray, count+1));
- }
- public static bool CheckAllLines(int[,] arrayOfLines, int count = 0){
- if(count < 9){
- int[] row = new int[9];
- for (int i = 0; i < 9; i++){
- row[i] = arrayOfLines[count,i];
- }
- return LineCorrectness(row) && CheckAllLines(arrayOfLines, count+1);
- }
- return true;
- }
- public static bool CheckAllColls(int[,] arrayOfLines, int count = 0){
- if(count < 9){
- int[] row = new int[9];
- for (int i = 0; i < 9; i++){
- row[i] = arrayOfLines[i, count];
- }
- return LineCorrectness(row) && CheckAllColls(arrayOfLines, count + 1);
- }
- return true;
- }
- public static bool CheckAllBlocks(int[,] arrayOfLines, int count = 0){
- if(count < 9){
- int rowPos = 0;
- int[] row = new int[9];
- for (int i = (count%3)*3; i < ((count % 3) * 3)+3; i++){
- double val = count / 3;
- for (int k = ((int)Math.Floor(val)*3); k < ((int)Math.Floor(val) * 3)+3; k++) {
- row[rowPos] = arrayOfLines[i, k];
- rowPos++;
- }
- }
- return LineCorrectness(row) && CheckAllBlocks(arrayOfLines, count + 1);
- }
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement