Aliendreamer

portal programing fundamentals

Apr 23rd, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.                    
  4. public class Program
  5. {
  6.     public static void Main()
  7.     {
  8.        
  9.         int numofrows=int.Parse(Console.ReadLine());
  10.        
  11.         List<string>matrixlist=new List<string>();
  12.         char start='s';
  13.         char end='e';
  14.         int[]position=new  int[2];
  15.         int count=0;
  16.         int []exitposition=new int[2];
  17.        
  18.         for(int i=0;i<numofrows;i++)
  19.         {
  20.             string input=Console.ReadLine().ToLower();
  21.             matrixlist.Add(input);
  22.            
  23.             if(input.Contains(start.ToString()))
  24.             {
  25.                 position[0]=i;
  26.                 int rownum=input.LastIndexOf('s');
  27.                 position[0]=i;
  28.                 position[1]=rownum;
  29.                
  30.             }
  31.             if(input.Contains(end.ToString()))
  32.             {
  33.                 position[0]=i;
  34.                 int rownum=input.LastIndexOf('e');
  35.                 exitposition[0]=i;
  36.                 exitposition[1]=rownum;
  37.                
  38.             }
  39.         }
  40.         char[] commands=Console.ReadLine().ToCharArray();
  41.         bool exit=false;
  42.         for(int k=0;k<commands.Length;k++)
  43.         {
  44.            
  45.             // проверка дали движението е възможно и ако не да се изпълни следащото условие
  46.             //бих го направил с метод може и с switch може и пак с bool
  47.             // нещо такова ти трябав не 200 реда Д:
  48.                 int row;
  49.                 string currentrow;
  50.             switch(commands[k].ToString().ToLower())
  51.             {
  52.                
  53.                
  54.                 case "u":
  55.                 if(position[0]-1<=0)
  56.                 {
  57.                     position[0]-=1;
  58.                 }
  59.                 else
  60.                 {
  61.                     position[0]=0;
  62.                 }
  63.                 break;
  64.                
  65.                 case "d":
  66.                 if(position[0]+1<=matrixlist.Count)
  67.                 {
  68.                     position[0]+=1;
  69.                 }
  70.                 else
  71.                 {
  72.                     position[0]=matrixlist.Count;
  73.                 }
  74.                 break;
  75.                
  76.                  case "r":
  77.                   row=position[0];
  78.                   currentrow=matrixlist[row];
  79.                    
  80.                 if(position[1]+1>currentrow.Length)
  81.                 {
  82.                     position[1]=0;
  83.                 }
  84.                 else
  85.                 {
  86.                     position[1]+=1;
  87.                 }
  88.                 break;
  89.                
  90.                  case "l":
  91.                  row=position[0];
  92.                  currentrow=matrixlist[row];
  93.                
  94.                 if(position[1]-1<0)
  95.                 {
  96.                     position[1]=currentrow.Length;
  97.                 }
  98.                 else
  99.                 {
  100.                     position[1]-=1;
  101.                 }
  102.                 break;     
  103.             }
  104.         count++;   
  105.         exit=position[0]==exitposition[0] && position[1]==exitposition[1];
  106.         }
  107.            
  108.         if(exit)
  109.         {
  110.             Console.WriteLine("Experiment successful. {0} turns required.",count);
  111.         }
  112.         else
  113.         {
  114.             Console.WriteLine("Robot stuck at {0} {1}. Experiment failed",position[0],position[1]);
  115.         }
  116.            
  117.        
  118.        
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment