Hello, there,
I spent above seven hours trying to figure out this problem, but our test system gave me only 10/100 points. Can somebody please help me to find my mistake?
With the given input I got a successful output, although my solution is incorrect.
Requirement:
Ivo's galaxy is represented as a two-dimensional array.
You will receive two integers, separated by a space, which represent the two dimensional array - the first being the rows and the second being the columns.
Every cell in the matrix is a star that has a value. Ivo starts at the given row and col. He can move only on the diagonal from the lowest left to the upper right, and adds to his score all the stars (values) from the cells he passes through.
Unfortunately, there is always an Evil power that tries to prevent his success.
Evil power starts at the given row and col and instantly destroys all stars on the opposite diagonal - from lowest right to the upper left. Ivo adds the values only of the stars that are not destroyed by the evil power.
Then, you must fill the two dimensional array with increasing integers starting from 0, and continuing on every row, like this:
• first row: 0, 1, 2… m
• second row: n+1, n+2, n+3… n + n.
The input ends when you receive the command "Let the Force be with you". When that happens, you must print the value of all stars that Ivo has collected successfully.
Input
• On the first line, you will receive the number N, M -> the dimensions of the matrix. You must then fill the matrix according to these dimensions.
• On the next several lines you will begin receiving 2 integers separated by a single space, which represent Ivo's row and col. On the next line you will receive the Evil Power's coordinates.
• There will always be at least 2 lines of input to represent at least 1 path of Ivo and the Evil force.
• When you receive the command, "Let the Force be with you" the input ends.
Output
• The output is simple. Print the sum of the values from all stars that Ivo has collected.
What I have tried:
using System;
using System.Linq;
namespace JediGalaxy
{
public class Program
{
static void Main(string[] args)
{
int[] arrayInfo = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
int matrixRows = arrayInfo[0];
int matrixCols = arrayInfo[1];
int[,] matrix = CreateMatrix(matrixRows, matrixCols);
long sumOfStars = 0;
string line = Console.ReadLine();
while (line != "Let the Force be with you")
{
long[] ivoCoordinates = line.Split().Select(long.Parse).ToArray();
long ivoRow = ivoCoordinates[0];
long ivoCol = ivoCoordinates[1];
long ivoStartRow = 0;
long ivoStartCol = 0;
ValidateIvoStart(matrixRows, ivoRow, ivoCol, out ivoStartRow, out ivoStartCol);
long[] evilsCoordinates = Console.ReadLine().Split().Select(long.Parse).ToArray();
long evilRow = evilsCoordinates[0];
long evilCol = evilsCoordinates[1];
long evilStartRow = 0;
long evilStartCol = 0;
ValidateEvilStart(matrixRows, matrixCols, evilRow, evilCol, out evilStartRow, out evilStartCol);
if(evilStartRow <= matrixRows - 1 && evilStartCol >= 0 && evilStartCol <= matrixCols - 1)
{
matrix[evilStartRow, evilStartCol] = 0;
}
while (evilStartRow > 0 && evilStartCol > 0)
{
EvilDestroysStars(matrix, ref evilStartRow, ref evilStartCol);
}
if (ivoStartRow <= matrixRows - 1 && ivoStartCol >= 0 && ivoStartCol <= matrixCols - 1)
{
sumOfStars += matrix[ivoStartRow, ivoStartCol];
}
while (ivoStartRow > 0 && ivoStartCol < matrixCols - 1)
{
IvoCollectsStars(matrix, ref sumOfStars, ref ivoStartRow, ref ivoStartCol);
}
line = Console.ReadLine();
}
Console.WriteLine(sumOfStars);
}
private static void IvoCollectsStars(int[,] matrix, ref long sum, ref long ivoStartRow, ref long ivoStartCol)
{
ivoStartRow--;
ivoStartCol++;
sum += matrix[ivoStartRow, ivoStartCol];
}
private static void EvilDestroysStars(int[,] matrix, ref long evilStartRow, ref long evilStartCol)
{
evilStartRow--;
evilStartCol--;
matrix[evilStartRow, evilStartCol] = 0;
}
private static void ValidateEvilStart(int matrixRows, int matrixCols, long evilRow, long evilCol, out long evilStartRow, out long evilStartCol)
{
if (evilRow > matrixRows - 1)
{
evilStartRow = matrixRows;
}
else
{
evilStartRow = evilRow;
}
if (evilCol > matrixCols)
{
evilStartCol = matrixCols;
}
else
{
evilStartCol = evilCol;
}
}
private static void ValidateIvoStart(int matrixRows, long ivoRow, long ivoCol, out long ivoStartRow, out long ivoStartCol)
{
if (ivoRow > matrixRows - 1)
{
ivoStartRow = matrixRows;
}
else
{
ivoStartRow = ivoRow;
}
if (ivoCol < 0)
{
ivoStartCol = -1;
}
else
{
ivoStartCol = ivoCol;
}
}
private static int[,] CreateMatrix(int matrixRows, int matrixCols)
{
int[,] matrix = new int[matrixRows, matrixCols];
int currentNum = 0;
for (int row = 0; row < matrixRows; row++)
{
for (int col = 0; col < matrixCols; col++)
{
matrix[row, col] = currentNum;
currentNum++;
}
}
return matrix;
}
}
}