using
System;
class
MainClass
{
public
static
void
SpiralMatrixAddRemove(
int
[,] mat,
int
R,
int
A)
{
if
(R == A)
{
return
;
}
int
rows = mat.GetLength(0);
int
cols = mat.GetLength(1);
int
top = 0, bottom = rows - 1, left = 0, right = cols - 1;
if
(R < A)
{
int
prevX = -1, prevY = -1;
while
(top <= bottom && left <= right)
{
for
(
int
col = left; col <= right; col++)
{
if
(mat[top, col] > A)
{
mat[prevX, prevY] = A;
return
;
}
if
(mat[top, col] > R && mat[top, col] <= A)
{
mat[prevX, prevY] = mat[top, col];
}
prevX = top;
prevY = col;
}
top += 1;
for
(
int
row = top; row <= bottom; row++)
{
if
(mat[row, right] > A)
{
mat[prevX, prevY] = A;
return
;
}
if
(mat[row, right] > R && mat[row, right] <= A)
{
mat[prevX, prevY] = mat[row, right];
}
prevX = row;
prevY = right;
}
right -= 1;
if
(top <= bottom && left <= right)
{
for
(
int
col = right; col >= left; col--)
{
if
(mat[bottom, col] > A)
{
mat[prevX, prevY] = A;
return
;
}
if
(mat[bottom, col] > R && mat[bottom, col] <= A)
{
mat[prevX, prevY] = mat[bottom, col];
}
prevX = bottom;
prevY = col;
}
bottom -= 1;
for
(
int
row = bottom; row >= top; row--)
{
if
(mat[row, left] > A)
{
mat[prevX, prevY] = A;
return
;
}
if
(mat[row, left] > R && mat[row, left] <= A)
{
mat[prevX, prevY] = mat[row, left];
}
prevX = row;
prevY = left;
}
left += 1;
}
}
mat[prevX, prevY] = A;
}
else
{
int
temp = A;
while
(top <= bottom && left <= right)
{
for
(
int
col = left; col <= right; col++)
{
if
(mat[top, col] > A && mat[top, col] < R)
{
int
tempVal = temp;
temp = mat[top, col];
mat[top, col] = tempVal;
if
(temp == R)
return
;
}
else
if
(mat[top, col] == R)
{
int
tempVal = temp;
temp = mat[top, col];
mat[top, col] = tempVal;
return
;
}
}
top += 1;
for
(
int
row = top; row <= bottom; row++)
{
if
(mat[row, right] > A && mat[row, right] < R)
{
int
tempVal = temp;
temp = mat[row, right];
mat[row, right] = tempVal;
if
(temp == R)
return
;
}
else
if
(mat[row, right] == R)
{
int
tempVal = temp;
temp = mat[row, right];
mat[row, right] = tempVal;
return
;
}
}
right -= 1;
if
(top <= bottom && left <= right)
{
for
(
int
col = right; col >= left; col--)
{
if
(mat[bottom, col] > A && mat[bottom, col] < R)
{
int
tempVal = temp;
temp = mat[bottom, col];
mat[bottom, col] = tempVal;
if
(temp == R)
return
;
}
else
if
(mat[bottom, col] == R)
{
int
tempVal = temp;
temp = mat[bottom, col];
mat[bottom, col] = tempVal;
return
;
}
}
bottom -= 1;
for
(
int
row = bottom; row >= top; row--)
{
if
(mat[row, left] > A && mat[row, left] < R)
{
int
tempVal = temp;
temp = mat[row, left];
mat[row, left] = tempVal;
if
(temp == R)
return
;
}
else
if
(mat[row, left] == R)
{
int
tempVal = temp;
temp = mat[row, left];
mat[row, left] = tempVal;
return
;
}
}
left += 1;
}
}
}
}
public
static
void
Main(
string
[] args)
{
int
[,] matrix = { { 1, 2, 3 }, { 7, 9, 4 }, { 7, 6, 5 } };
int
R = 7, A = 8;
SpiralMatrixAddRemove(matrix, R, A);
for
(
int
i = 0; i < matrix.GetLength(0); i++)
{
for
(
int
j = 0; j < matrix.GetLength(1); j++)
{
Console.Write(matrix[i, j] +
" "
);
}
Console.WriteLine();
}
}
}