קוד:
static void Main(string[] args)
{
string line;
int r, c, k, m, j;
double max, maxk, tmp;
string[] arr;
double[,] board;
StreamReader txt = new StreamReader("d:/input.txt");
txt.ReadLine();
while ((line = txt.ReadLine()) != null)
{
max = 0;
maxk = 0;
arr = line.Split(' ');
r = int.Parse(arr[0]);
c = int.Parse(arr[1]);
k = int.Parse(arr[2]);
m = int.Parse(arr[3]);
board = new double[r, 2 * c - 1];
for (int i = 0; i < r; i++)
for (j = 0; j < 2 * c - 1; j++)
board[i, j] = (((i % 2) ^ (j % 2))*2) ^ 2;
for (j = 5; j < arr.Length; j += 2)
board[int.Parse(arr[j - 1]), int.Parse(arr[j]) * 2 + int.Parse(arr[j - 1]) % 2] = 0;
for (j = 1; j < 2*c - 1; j+=2)
{
board[0, j] = 1;
tmp = func(r, c, k, board);
if (max < tmp)
{
max = tmp;
maxk = j/2;
}
board[0, j] = 0;
}
Console.WriteLine(maxk + " " + addZeros(Math.Round(max, 6)));
}
Console.Read();
}
public static string addZeros(double n)
{
string str = n.ToString();
if (!str.Contains('.'))
str += ".";
int len = 8 - str.Length;
for (int i = 0; i < len; i++)
str += "0";
return str;
}
public static double func(int r, int c, int k, double[,] board)
{
int j;
for (int i = 1; i < r; i++)
{
for (j = 1; j < c * 2 - 2; j++)
{
if (board[i, j] > 1) continue;
board[i, j] = 0;
if (board[i - 1, j] <= 1)
board[i, j] += board[i - 1, j];
if (board[i, j + 1] > 1)
{
if (j == c * 2 - 4)
board[i, j] += board[i - 1, j + 1];
else
board[i, j] += board[i - 1, j + 1] / 2.0;
}
if (board[i, j - 1] > 1)
{
if (j == 2)
board[i, j] += board[i - 1, j - 1];
else
board[i, j] += board[i - 1, j - 1] / 2.0;
}
}
}
return board[r-1, k*2+1];
}