tu144
BAN USERstatic void Main(string[] args)
{
int[,] array = { { 0, 0, 1, 1, 1 },
{ 0, 1, 0, 0, 0 },
{ 1, 0, 0, 1, 1 },
{ 0, 0, 0, 0, 1 },
};
if (checkIfPathExists(array, 3, 0, 1, 2))
Console.WriteLine("True");
else
Console.WriteLine("False");
}
static Boolean checkIfPathExists(int[,] array, int startRow, int startCol, int endRow, int endCol)
{
Dictionary<int, Dictionary<int, int>> currentStackOfCells = new Dictionary<int, Dictionary<int, int>>();
if (!canGoToNextCell(array, startRow, startCol, currentStackOfCells)) return false;
if (!canGoToNextCell(array, endRow, endCol, currentStackOfCells)) return false;
return FindPath(array, startRow, startCol, endRow, endCol, currentStackOfCells);
}
static Boolean FindPath(int[,] array, int currentRow, int currentCol, int endRow, int endCol, Dictionary<int, Dictionary<int, int>> currentStackOfCells)
{
if (!currentStackOfCells.ContainsKey(currentRow))
currentStackOfCells[currentRow] = new Dictionary<int, int>();
currentStackOfCells[currentRow][currentCol] = 1; //push current cell into visited cell's list
Boolean retCd = false;
if ((currentRow == endRow) && (currentCol == endCol)) retCd = true;
if ((!retCd) && canGoToNextCell(array, currentRow - 1, currentCol, currentStackOfCells)) //try to go up
retCd = FindPath(array, currentRow - 1, currentCol, endRow, endCol, currentStackOfCells);
if ((!retCd) && canGoToNextCell(array, currentRow + 1, currentCol, currentStackOfCells)) //try to go down
retCd = FindPath(array, currentRow + 1, currentCol, endRow, endCol, currentStackOfCells);
if ((!retCd) && canGoToNextCell(array, currentRow, currentCol - 1, currentStackOfCells)) //try to go left
retCd = FindPath(array, currentRow, currentCol - 1, endRow, endCol, currentStackOfCells);
if ((!retCd) && canGoToNextCell(array, currentRow, currentCol + 1, currentStackOfCells)) //try to go right
retCd = FindPath(array, currentRow, currentCol + 1, endRow, endCol, currentStackOfCells);
currentStackOfCells[currentRow].Remove(currentCol); //remove cell from the visited cell's list
return retCd;
}
static Boolean canGoToNextCell(int[,] array, int nextRow, int nextCol, Dictionary<int, Dictionary<int, int>> currentStackOfCells)
{
//check if out of boundaries
if (nextRow < 0) return false;
if (nextRow >= array.GetLength(0)) return false;
if (nextCol < 0) return false;
if (nextCol >= array.GetLength(1)) return false;
//end out of boundaries check
//check if current cell is 0
if (array[nextRow, nextCol] == 1) return false;
//check if we already visited this cell and going in cycles
if (currentStackOfCells.ContainsKey(nextRow))
if (currentStackOfCells[nextRow].ContainsKey(nextCol)) return false;
return true;
}
static void Main(string[] args)
{
Console.WriteLine("Result = " + calcSQRT(2)); //Test Call;
Console.WriteLine("End");
}
static Double calcSQRT(int square, int overAllPrecision = 32)
{
int[] pairArray = getPairsArray(square);
String resultBuffer = "";
int currentIndex = 0;
BigInteger A = new BigInteger(pairArray[pairArray.Length - currentIndex - 1]);//BigInteger is from the System.Numerics
BigInteger y = new BigInteger(0);
while (true)
{
int i = 0;
while ((A - (10 * y + 2 * (i + 1) - 1)) >= 0)
{
i++;
A = A - (10 * y + 2 * i - 1);
}
if (currentIndex == pairArray.Length)
resultBuffer = resultBuffer + ".";
resultBuffer = resultBuffer + i;
currentIndex++;
if (currentIndex > overAllPrecision) break;
if (A == 0)
if (currentIndex >= pairArray.Length)
break;
A = 100 * A;
if (currentIndex < pairArray.Length)
A = A + pairArray[pairArray.Length - currentIndex - 1];
if (currentIndex == 1)
y = 2 * i;
else
y = 10 * y + 2 * i;
}
return Double.Parse(resultBuffer);
}
private static int[] getPairsArray(int square)
{
int[] pair = new int[1];
int count = 0;
while(square > 0)
{
int digit = square % 10;
square = square / 10;
if(square > 0)
{
digit = (square % 10) * 10 + digit;
square = square / 10;
}
if (count > 0)
Array.Resize(ref pair, count + 1);
pair[count] = digit;
count++;
}
return pair;
}
static void Main(string[] args)
{
Console.WriteLine("Result = " + calcSQRT(2)); //Test Call;
Console.WriteLine("End");
}
static Double calcSQRT(int square, int overAllPrecision = 32)
{
int[] pairArray = getPairsArray(square);
String resultBuffer = "";
int currentIndex = 0;
BigInteger A = new BigInteger(pairArray[pairArray.Length - currentIndex - 1]);//BigInteger is from the System.Numerics
BigInteger y = new BigInteger(0);
while (true)
{
int i = 0;
while ((A - (10 * y + 2 * (i + 1) - 1)) >= 0)
{
i++;
A = A - (10 * y + 2 * i - 1);
}
if (currentIndex == pairArray.Length)
resultBuffer = resultBuffer + ".";
resultBuffer = resultBuffer + i;
currentIndex++;
if (currentIndex > overAllPrecision) break;
if (A == 0)
if (currentIndex >= pairArray.Length)
break;
A = 100 * A;
if (currentIndex < pairArray.Length)
A = A + pairArray[pairArray.Length - currentIndex - 1];
if (currentIndex == 1)
y = 2 * i;
else
y = 10 * y + 2 * i;
}
return Double.Parse(resultBuffer);
}
private static int[] getPairsArray(int square)
{
int[] pair = new int[1];
int count = 0;
while(square > 0)
{
int digit = square % 10;
square = square / 10;
if(square > 0)
{
digit = (square % 10) * 10 + digit;
square = square / 10;
}
if (count > 0)
Array.Resize(ref pair, count + 1);
pair[count] = digit;
count++;
}
return pair;
}
- tu144 April 14, 2016