Advertisement
Artem_Chepurov

Ex 4

Jun 15th, 2022
920
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace SandBox
  5. {
  6.     public class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Queue<Tuple<int, int>> q = new Queue<Tuple<int, int>>();
  11.             string d = Console.ReadLine();
  12.             int n = Convert.ToInt32(d.Split(' ')[0]);
  13.             int m = Convert.ToInt32(d.Split(' ')[1]);
  14.             List<List<char>> lab = new List<List<char>>(n);
  15.  
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 lab.Add(new List<char>(m));
  19.                 for (int j = 0; j < m; j++)
  20.                 {
  21.                     lab[i].Add('_');
  22.                 }
  23.             }
  24.  
  25.             for (int i = 0; i < n; i++)
  26.             {
  27.                 string col = Console.ReadLine();
  28.                 for (int j = 0; j < m; j++)
  29.                 {
  30.                     lab[i][j] = col[j];
  31.                 }
  32.             }
  33.  
  34.             for (int i = 0; i < n; i++)
  35.             {
  36.                 for (int j = 0; j < m; j++)
  37.                 {
  38.                     if (lab[i][j] == 'S')
  39.                     {
  40.                         q.Enqueue(new Tuple<int, int>(i,j));
  41.                     }
  42.                 }
  43.             }
  44.  
  45.             while (q.Count > 0)
  46.             {
  47.                 Tuple<int, int> tmp = q.Dequeue();
  48.  
  49.                 if (tmp.Item1 - 1 >= 0 && lab[tmp.Item1 - 1][tmp.Item2] == '.')
  50.                 {
  51.                     q.Enqueue(new Tuple<int, int>(tmp.Item1 - 1, tmp.Item2));
  52.                     lab[tmp.Item1 - 1][tmp.Item2] = 'U';
  53.                 }
  54.                 if (tmp.Item1 + 1 < n && lab[tmp.Item1 + 1][tmp.Item2] == '.')
  55.                 {
  56.                     q.Enqueue(new Tuple<int, int>(tmp.Item1 + 1, tmp.Item2));
  57.                     lab[tmp.Item1 + 1][tmp.Item2] = 'D';
  58.                 }
  59.                 if (tmp.Item2 - 1 >= 0 && lab[tmp.Item1][tmp.Item2 - 1] == '.')
  60.                 {
  61.                     q.Enqueue(new Tuple<int, int>(tmp.Item1, tmp.Item2 - 1));
  62.                     lab[tmp.Item1][tmp.Item2 - 1] = 'L';
  63.                 }
  64.                 if (tmp.Item2 + 1 >= 0 && lab[tmp.Item1][tmp.Item2 + 1] == '.')
  65.                 {
  66.                     q.Enqueue(new Tuple<int, int>(tmp.Item1, tmp.Item2 + 1));
  67.                     lab[tmp.Item1][tmp.Item2 + 1] = 'R';
  68.                 }
  69.             }
  70.  
  71.             foreach (var s in lab)
  72.             {
  73.                 foreach (var s1 in s)
  74.                 {
  75.                     Console.Write(s1);
  76.                 }
  77.                 Console.WriteLine();
  78.             }
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement