Advertisement
Pavle_nis

Roboti

Nov 5th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 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.  
  7. namespace Roboti
  8. {
  9.     class Program
  10.     {
  11.         void Igra()
  12.         {
  13.             int MAXR = 100, MAXS = 100, MAXPOTEZA = 100;
  14.             int R, S, N;
  15.             string[,] ploca = new string[MAXR, MAXS + 1], pomocna = new string[MAXR, MAXS + 1];
  16.             string[] igrac = new string[MAXPOTEZA + 1];
  17.  
  18.             R = Convert.ToInt32(Console.ReadLine());
  19.             S = Convert.ToInt32(Console.ReadLine());
  20.  
  21.             for (int r = 0; r < R; ++r)
  22.             {
  23.                 ploca[r, 0] = Console.ReadLine();
  24.             }
  25.             N = igrac.Length;
  26.  
  27.             int igrac_r = 0, igrac_s = 0;
  28.             for (int r = 0; r < R; ++r)
  29.             {
  30.                 for (int s = 0; s < S; ++s)
  31.                 {
  32.                     if (ploca[r, s] == "I")
  33.                     {
  34.                         igrac_r = r;
  35.                         igrac_s = s;
  36.                     }
  37.                 }
  38.             }
  39.  
  40.             for (int i = 0; i < N; ++i)
  41.             {
  42.                 for (int r = 0; r < R; ++r)
  43.                     for (int s = 0; s < S; ++s)
  44.                         pomocna[r, s] = ".";
  45.  
  46.                 igrac_r += 1 - (Convert.ToChar(igrac[i]) - '1') / 3;
  47.                 igrac_s += (Convert.ToChar(igrac[i]) - '1') % 3 - 1;
  48.  
  49.                 if (ploca[igrac_r, igrac_s] == "R")
  50.                 {
  51.                     Console.WriteLine("Kraj {0}\n", i + 1);
  52.                     return;
  53.                 }
  54.  
  55.                 pomocna[igrac_r, igrac_s] = "I";
  56.  
  57.                 for (int r = 0; r < R; ++r)
  58.                     for (int s = 0; s < S; ++s)
  59.                         if (ploca[r, s] == "R")
  60.                         {
  61.                             int dr = sgn(igrac_r - r);
  62.                             int ds = sgn(igrac_s - s);
  63.  
  64.                             if (pomocna[r + dr, s + ds] == "I")
  65.                             {
  66.                                 Console.WriteLine("Kraj {0}\n", i + 1);
  67.                                 return;
  68.                             }
  69.  
  70.                             if (pomocna[r + dr, s + ds] == ".") pomocna[r + dr, s + ds] = "R";
  71.                             else pomocna[r + dr, s + ds] = "X";
  72.                         }
  73.  
  74.                 for (int r = 0; r < R; ++r)
  75.                     for (int s = 0; s < S; ++s)
  76.                     {
  77.                         if (pomocna[r, s] == "X") pomocna[r, s] = ".";
  78.                         ploca[r, s] = pomocna[r, s];
  79.                     }
  80.             }
  81.  
  82.             for (int r = 0; r < R; ++r) Console.WriteLine("\n{0}", ploca[r, 0]);
  83.         }
  84.         static void Main(string[] args)
  85.         {
  86.             Program p = new Program();
  87.             p.Igra();
  88.         }
  89.         int sgn(int x)
  90.         {
  91.             if (x < 0) return -1;
  92.             if (x > 0) return 1;
  93.  
  94.             return 0;
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement