Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- public class Program
- {
- public static void Main()
- {
- int numofrows=int.Parse(Console.ReadLine());
- List<string>matrixlist=new List<string>();
- char start='s';
- char end='e';
- int[]position=new int[2];
- int count=0;
- int []exitposition=new int[2];
- for(int i=0;i<numofrows;i++)
- {
- string input=Console.ReadLine().ToLower();
- matrixlist.Add(input);
- if(input.Contains(start.ToString()))
- {
- position[0]=i;
- int rownum=input.LastIndexOf('s');
- position[0]=i;
- position[1]=rownum;
- }
- if(input.Contains(end.ToString()))
- {
- position[0]=i;
- int rownum=input.LastIndexOf('e');
- exitposition[0]=i;
- exitposition[1]=rownum;
- }
- }
- char[] commands=Console.ReadLine().ToCharArray();
- bool exit=false;
- for(int k=0;k<commands.Length;k++)
- {
- // проверка дали движението е възможно и ако не да се изпълни следащото условие
- //бих го направил с метод може и с switch може и пак с bool
- // нещо такова ти трябав не 200 реда Д:
- int row;
- string currentrow;
- switch(commands[k].ToString().ToLower())
- {
- case "u":
- if(position[0]-1<=0)
- {
- position[0]-=1;
- }
- else
- {
- position[0]=0;
- }
- break;
- case "d":
- if(position[0]+1<=matrixlist.Count)
- {
- position[0]+=1;
- }
- else
- {
- position[0]=matrixlist.Count;
- }
- break;
- case "r":
- row=position[0];
- currentrow=matrixlist[row];
- if(position[1]+1>currentrow.Length)
- {
- position[1]=0;
- }
- else
- {
- position[1]+=1;
- }
- break;
- case "l":
- row=position[0];
- currentrow=matrixlist[row];
- if(position[1]-1<0)
- {
- position[1]=currentrow.Length;
- }
- else
- {
- position[1]-=1;
- }
- break;
- }
- count++;
- exit=position[0]==exitposition[0] && position[1]==exitposition[1];
- }
- if(exit)
- {
- Console.WriteLine("Experiment successful. {0} turns required.",count);
- }
- else
- {
- Console.WriteLine("Robot stuck at {0} {1}. Experiment failed",position[0],position[1]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment