Advertisement
HTsekin

Untitled

Jan 9th, 2021
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Core_Tasks_3
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.  
  13.             int size = 4; //Int32.Parse(Console.ReadLine());
  14.  
  15.             //optimal search  2, 1, -1, -2, -2, -1, 1, 2
  16.             //                1, 2, 2, 1, -1, -2, -2, -1
  17.  
  18.             // --------- clesest
  19.             //-2, -1, 1, 2, -2, -1, 1, 2
  20.             //-1, -2, -2, -1, 1, 2, 2, 1
  21.             int[] rowPostions = new int[] { 2, 1, -1, -2, -2, -1, 1, 2 }; // -2, -1, 1, 2, 2, 1, -1, -2   | -2, -1, 1, 2,  2,  1, -1, -2 | 2, 1, -1, -2, -2, -1, 1, 2 \ 1, -2, 1,  -2, -1, 2, 1, -1
  22.             int[] colPostions = new int[] { 1, 2, 2, 1, -1, -2, -2, -1 }; // -1, -2, -2, -1, 1, 2, 2, 1  | 1,  2, 2, 1, -1, -2, -2, -1 | 1, 2, 2, 1, -1, -2, -2, -1 \ 2,  1, -2, -1, -2,  -1, 2, 2
  23.                
  24.             int counterPosition = 1;
  25.  
  26.             int[,] matrix = new int[size,size];
  27.  
  28.             // array initiatig -1 to all boxes
  29.             for(int row = 0; row < size; row++)
  30.             {
  31.                 for (int col = 0; col < size; col++)
  32.                 {
  33.                     matrix[row, col] = -1;
  34.                 }
  35.             }
  36.  
  37.             bool alive = true;
  38.  
  39.             int rowCurrPosition = 0;
  40.             int colCurrPosition = 0;
  41.  
  42.             matrix[rowCurrPosition, colCurrPosition] = counterPosition;
  43.            
  44.             bool isPossibleMove = false;
  45.             int tm = 0;
  46.  
  47.             while (alive)
  48.             {
  49.                 isPossibleMove = false;
  50.                 bool cnt = false;
  51.                 for(int i = 0; i < rowPostions.Length; i++)
  52.                 {
  53.  
  54.  
  55.                     //Console.WriteLine((rowCurrPosition + (rowPostions[i])) + " : " + (colCurrPosition + (colPostions[i])) + " : " + counterPosition);
  56.                     int tmpRowPos = rowCurrPosition + (rowPostions[i]);
  57.                     int tmpColPos = colCurrPosition + (colPostions[i]);
  58.                     if (tmpRowPos >= 0 && tmpRowPos < size &&
  59.                             tmpColPos >= 0 && tmpColPos < size &&
  60.                             matrix[tmpRowPos, tmpColPos] == -1)
  61.                     {
  62.                         //
  63.                         rowCurrPosition = tmpRowPos;
  64.                         colCurrPosition = tmpColPos;
  65.                         counterPosition++;
  66.  
  67.                         matrix[rowCurrPosition, colCurrPosition] = counterPosition;
  68.                         break;
  69.                     }
  70.                     else
  71.                     {
  72.                         cnt = true;
  73.                     }
  74.  
  75.                    
  76.  
  77.                     tm++;
  78.                    
  79.                 }
  80.  
  81.                 if (tm >= 100)
  82.                     alive = false;
  83.             }
  84.  
  85.  
  86.             for (int row = 0; row < size; row++)
  87.             {
  88.                 for (int col = 0; col < size; col++)
  89.                 {
  90.                     Console.Write(matrix[row, col] + " ");
  91.                 }
  92.                 Console.WriteLine();
  93.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement