[ACM]Q118: Mutant Flatworld Expolrers
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Q118
{
class Program
{
static private char[] par = { ' ' }; static private int[] coordinates = new int[2]; static private char[] command;
static private string[] robot; static private bool tof = true; static private string[] fallp;
static void Main(string[] args)
{
for (; ; )
{
labelc: Console.Write("(x,y)>>>");
try
{
coordinates = Array.ConvertAll<string, int>(Console.ReadLine().Split(par, StringSplitOptions.RemoveEmptyEntries), int.Parse);
}
catch { goto labelc; }
if (coordinates.Length != 2) goto labelc;
fallp = new string[3];
for (; ; )
{
labelr: Console.Write("Robot>>>");
try
{
robot = Console.ReadLine().Split(par, StringSplitOptions.RemoveEmptyEntries);
int temp;
if ((!int.TryParse(robot[0], out temp)) || (!int.TryParse(robot[1], out temp)) || robot.Length != 3 || (robot[2].ToLower() != "e" && robot[2].ToLower() != "w" && robot[2].ToLower() != "s" && robot[2].ToLower() != "n")) //throw new Exception();
//Console.WriteLine("{0}\t{1}\t{2}",robot[0],robot[1],robot[2]);
throw new Exception();
}
catch { goto labelr; }
Console.Write("command>>>");
string str = Console.ReadLine();
command = new char[str.Length];
tof = true;
for (int i = 0; i < str.Length; i++)
{
if (str.Substring(i, 1).ToUpper() == "R")
{
if (robot[2].ToUpper() == "E") robot[2] = "S";
else if (robot[2].ToUpper() == "S") robot[2] = "W";
else if (robot[2].ToUpper() == "W") robot[2] = "N";
else if (robot[2].ToUpper() == "N") robot[2] = "E";
}
else if (str.Substring(i, 1).ToUpper() == "L")
{
if (robot[2].ToUpper() == "E") robot[2] = "N";
else if (robot[2].ToUpper() == "S") robot[2] = "E";
else if (robot[2].ToUpper() == "W") robot[2] = "S";
else if (robot[2].ToUpper() == "N") robot[2] = "W";
}
else if (str.Substring(i, 1).ToUpper() == "F" && !(fallp[0] == robot[0] && fallp[1] == robot[1] && fallp[2] == robot[2]))
{
if (robot[2].ToUpper() == "E")
{
if (int.Parse(robot[0]) == coordinates[0])
{
Console.WriteLine("({0},{1}) {2} Lost", robot[0], robot[1], robot[2]);
fallp[0] = robot[0]; fallp[1] = robot[1]; fallp[2] = robot[2];
tof = false;
break;
}
robot[0] = (int.Parse(robot[0]) + 1).ToString();
}
else if (robot[2].ToUpper() == "S")
{
if (int.Parse(robot[1]) == 0)
{
Console.WriteLine("({0},{1}) {2} Lost", robot[0], robot[1], robot[2]);
fallp[0] = robot[0]; fallp[1] = robot[1]; fallp[2] = robot[2];
tof = false;
break;
}
robot[1] = (int.Parse(robot[1]) - 1).ToString();
}
else if (robot[2].ToUpper() == "W")
{
if (int.Parse(robot[0]) == 0)
{
Console.WriteLine("({0},{1}) {2} Lost", robot[0], robot[1], robot[2]);
fallp[0] = robot[0]; fallp[1] = robot[1]; fallp[2] = robot[2];
tof = false;
break;
}
robot[0] = (int.Parse(robot[0]) - 1).ToString();
}
else if (robot[2].ToUpper() == "N")
{
if (int.Parse(robot[1]) == coordinates[1])
{
Console.WriteLine("({0},{1}) {2} Lost", robot[0], robot[1], robot[2]);
fallp[0] = robot[0]; fallp[1] = robot[1]; fallp[2] = robot[2];
tof = false;
break;
}
robot[1] = (int.Parse(robot[1]) + 1).ToString();
}
}
//command[i] = char.Parse(str.Substring(i, 1).ToUpper());
}
if (tof) Console.WriteLine("({0},{1}) {2} ", robot[0], robot[1], robot[2]);
}
}
}
}
}