Guest User

Untitled

a guest
Nov 17th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace safeboard_task
  9. {
  10. class Program
  11. {
  12.  
  13. static void Main(string[] args)
  14. {
  15. string pos_file = @"D:\fil.txt";
  16. int size_matrix = SizeMatrixIn_File(pos_file);
  17. int[,] Matrix = new int[size_matrix,size_matrix];
  18. FillMassOf_File(pos_file, size_matrix,ref Matrix);
  19. int result = MinDistance(Matrix, size_matrix);
  20. Console.WriteLine(result);
  21. Console.ReadKey();
  22. }
  23.  
  24. static int MinDistance(int[,] matrix, int Size)
  25. {
  26.  
  27. int[,] min_dist = new int[Size, Size];
  28. for (int i = 0; i < Size; i++)
  29. {
  30. for (int j = 0; j < Size; j++)
  31. {
  32. if (i == 0 && j == 0)
  33. min_dist[i, j] = matrix[i, j];
  34. else
  35. if (i == 0)
  36. min_dist[i, j] = matrix[i, j] + min_dist[0, j - 1];
  37. else if (j == 0)
  38. min_dist[i, j] = matrix[i, j] + min_dist[i - 1, 0];
  39. else
  40. min_dist[i, j] = Math.Min(min_dist[i, j - 1], min_dist[i - 1, j]) + matrix[i, j];
  41. }
  42. }
  43. return min_dist[Size - 1, Size - 1];
  44. }
  45.  
  46. static void FillMassOf_File(string patch, int sizeMass, ref int[,] mass)
  47. {
  48. using (StreamReader sr = new StreamReader(patch))
  49. {
  50. string line = "";
  51. int y = -1;
  52. while ((line = sr.ReadLine()) != null)
  53. {
  54. if (y >= 0)
  55. {
  56. for (int x = 0; x < sizeMass; x++)
  57. {
  58. mass[x, y] = int.Parse(line.Split(' ')[x]);
  59.  
  60. }
  61. }
  62. y++;
  63.  
  64. }
  65. }
  66. }
  67.  
  68. static int SizeMatrixIn_File(string patch)
  69. {
  70. int value = 0;
  71. using (StreamReader sr = new StreamReader(patch))
  72. {
  73. value = int.Parse(sr.ReadLine());
  74. }
  75. return value;
  76. }
  77. }
  78. }
Add Comment
Please, Sign In to add comment