我想改变" 1"进入" 0"在模式中。 在给定的代码中,我尝试了所有可能的逻辑。哪个不能正常工作。我评论所有的逻辑。请逐一检查,并确定错误。
问候。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
class Program
{
static void Main(string[] args)
{
int[,] a = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 1, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }};
int r, c;
for (r = 0; r < 10; r++)
{
for (c = 0; c < 10; c++)
{
Console.Write("{0} ", a[r, c]);
}
Console.Write("\n");
}//For Printing...
r = 0;
c = 0;
for (r = 0; r < 10; r++)// it is coding for changing row position
{
for (c = 0; c < 10; c++)// it is coding for changing column position
{
if (a[r, c] != 0)// here i check position in array is not equal to zero.
{
int q, w, e, t, g, s, d, f;
q = a[r - 1, c];//Up
w = a[r + 1, c];//down
e = a[r, c - 1];//left
t = a[r, c + 1];//right
g = a[r + 1, c + 1];//down,right/* These all condintions are design for check circular surround of arrays position.*/
s = a[r + 1, c - 1];//down,left
d = a[r - 1, c + 1];//Up,right
f = a[r - 1, c - 1];//up,left
/*These are all possible logics which should be possibly made for checking around the position of array. If you want to check it, active one by one by removing comment*/
//if (q != 0 || w != 0 || e != 0 || t != 0 || g != 0 || s != 0 || d != 0 || f != 0)
//if (q != 0 && w != 0 && e != 0 && t != 0 && g != 0 && s != 0 && d != 0 && f != 0)
//if (q != 0 || w != 0 || e != 0 || t != 0 )
//if (q != 0 && w != 0 && e != 0 && t != 0 )
//if (q == 0 || w == 0 || e == 0 || t == 0 || g == 0 || s == 0 || d == 0 || f == 0)
//if (q == 0 && w == 0 && e == 0 && t == 0 && g == 0 && s == 0 && d == 0 && f == 0)
//if (q == 0 || w == 0 || e == 0 || t == 0 )
//if (q == 0 && w == 0 && e == 0 && t == 0 )
//if (q == 1 || w == 1 || e == 1 || t == 1 || g == 1 || s == 1 || d == 1 || f == 1)
//if (q == 1 && w == 1 && e == 1 && t == 1 && g == 1 && s == 1 && d == 1 && f == 1)
//if (q == 1 || w == 1 || e == 1 || t == 1 )
//if (q == 1 && w == 1 && e == 1 && t == 1 )
//if (q != 1 || w != 1 || e != 1 || t != 1 || g != 1 || s != 1 || d != 1 || f != 1)
//if (q != 1 && w != 1 && e != 1 && t != 1 && g != 1 && s != 1 && d != 1 && f != 1)
//if (q != 1 || w != 1 || e != 1 || t != 1 )
//if (q != 1 && w != 1 && e != 1 && t != 1 )
{
a[r, c] = 0;
}
}
}
}
/*Further below coding is for Output*/
Console.Write("\n\n");
for (r = 0; r < 10; r++)
{
for (c = 0; c < 10; c++)
{
Console.Write("{0} ", a[r, c]);
}
Console.Write("\n");
}
}
}
}
所需的结果..
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 1, 0
0, 0, 0, 0, 0, 0, 0, 1, 1, 0
0, 0, 0, 0, 0, 0, 1, 0, 1, 0
0, 0, 0, 0, 0, 1, 0, 0, 1, 0
0, 0, 0, 0, 1, 0, 0, 0, 1, 0
0, 0, 0, 1, 0, 0, 0, 0, 1, 0
0, 0, 1, 0, 0, 0, 0, 0, 1, 0
0, 1, 1, 1, 1, 1, 1, 1, 1, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
答案 0 :(得分:2)
为了从1中清空矩形,我们应该确定包含该矩形的行的开始和结束。之后,我们从矩形的第3行进行迭代,因为前两行不需要更改,我们找到当前行的列的开始和结束。在找到这两个之后,我们在开始之后到结束之前将1变换为0。我们继续迭代到最后一行之前。
int startI = 0, endI = 0;
for (int i = 0; i < a.GetLength(0); i++)//find start and end for rows
{
for (int j = 0; j < a.GetLength(1); j++)
{
if (a[i, j] == 1)
{
endI = i;//whenever you see 1, update it
if (startI == 0)//Update just the first time you see 1
startI = i;
}
}
}
//Make changes to all rows except the first one and the last one
for (int i = startI + 1; i < endI ; i++)
{
int startJ = 0, endJ = 0;
for (int j = 0; j < a.GetLength(1); j++)//find start and end for columns
{
if (a[i, j] == 1)
{
endJ = j;
if (startJ == 0)
startJ = j;
}
}
//set all 1 to 0, except the first and the last for this row
for (int j = startJ + 1; j < endJ ; j++)
a[i, j] = 0;
}
我从您的代码中了解到,您试图在每个元素周围找到一个正方形,然后根据方形元素值来决定。像这样的东西(x是当前元素)
f q d
e x t
s w g
这个变量有很多组合,所以我评论你检查的最后一个条件
if (q != 1 || w != 1 || e != 1 || t != 1 )
a[r, c] = 0;
这种情况失败,例如我们有(错误地设置x = 0)
0 0 1
0 x=1 1
1 1 1
注意:此代码仅适用于凸形。