死循环问题?????

huanyingni 发布于 2013/11/19 17:15
阅读 231
收藏 0

这里fanwei开始为空,tx,ty为整数,list为已经赋值的遗嘱数据,jy为常数!为什么我的循环会成为死循环呀! private void xunhuan(List<int> fanwei, int tx, int ty, List<int> list)

  {

            if (!(fanwei.Contains(tx * (jy + 1) + ty)))
            {
                    for (int i = (tx - 1); i <= (tx + 1); i++)
                    {
                        if ((i>=0)&&(i <= ix))
                        {
                            for (int j = (ty - 1); j <= (ty + 1); j++)
                            {
                                if ((j>=0)&&(j <= jy))
                                {
                                    if ((i == tx) && (j == ty))
                                    {
                                        if (!(fanwei.Contains(i * (jy + 1) + j)))
                                            fanwei.Add(i * (jy + 1) + j);
                                        if (list.Contains(i * (jy + 1) + j))
                                            list.Remove(i * (jy + 1) + j);
                                    }
                                    else
                                    {
                                        if (list.Contains(i * (jy + 1) + j))
                                        {
                                            if (!(fanwei.Contains(i * (jy + 1) + j)))
                                                xunhuan(fanwei, i, j, list);
                                            else
                                                list.Remove(i * (jy + 1) + j);
                                        }
                                        else
                                        {
                                            if (!(fanwei.Contains(i * (jy + 1) + j)))
                                                fanwei.Add(i * (jy + 1) + j);
                                        }
                                    }
                                }
                            }
                        }
                    }        
            }
            else
            {
                if (list.Contains(tx * (jy + 1) + ty))
                {
                    list.Remove(tx * (jy + 1) + ty);
                }
            }
        }
加载中
0
h
huanyingni

这个问题补充一下:就是一个格网,我要求出红线内部的占用的网格是多少,fanwei为要得到的值,list是红线内部完全占用网格的值(也就是网格内部无点的网格值集合),按照九分网的方法求!!!

tx为x方向的网格值,ty为y方向的网格值,tx,ty起始值为list[0],就是说从list的一个值开始。怎么会死循环呢?

0
h
huanyingni

红色横线部分为list包含的值!!

0
h
huanyingni
private void xunhuan(List<int> fanwei, int tx, int ty, List<int> list)

  {

           for (int i = (tx - 1); i <= (tx + 1); i++)

                    {
                        if ((i>=0)&&(i <= ix))
                        {
                            for (int j = (ty - 1); j <= (ty + 1); j++)
                            {
                                if ((j>=0)&&(j <= jy))
                                {
                                    if ((i == tx) && (j == ty))
                                    {
                                        if (!(fanwei.Contains(i * (jy + 1) + j)))
                                            fanwei.Add(i * (jy + 1) + j);
                                        if (list.Contains(i * (jy + 1) + j))
                                            list.Remove(i * (jy + 1) + j);
                                    }
                                    else
                                    {
                                       
                                            if (!(fanwei.Contains(i * (jy + 1) + j)))

                                             {

                                               fanwei.Add(i * (jy + 1) + j);                                    if (list.Contains(i * (jy + 1) + j))

                                        {

                                                           list.Remove(i * (jy + 1) + j);

                                                             xunhuan(fanwei, i, j, list);

                                            }

                                        }
                                       
                                    }
                                }
                            }
                        }
                    }        
          
           
        }
0
h
huanyingni
private void xunhuan(List<int> fanwei, int tx, int ty, List<int> list)

  {

           for (int i = (tx - 1); i <= (tx + 1); i++)

                    {
                        if ((i>=0)&&(i <= ix))
                        {
                            for (int j = (ty - 1); j <= (ty + 1); j++)
                            {
                                if ((j>=0)&&(j <= jy))
                                {
                                    if ((i == tx) && (j == ty))
                                    {
                                        if (!(fanwei.Contains(i * (jy + 1) + j)))
                                            fanwei.Add(i * (jy + 1) + j);
                                        if (list.Contains(i * (jy + 1) + j))
                                            list.Remove(i * (jy + 1) + j);
                                    }
                                    else
                                    {                                        
                                       if (!(fanwei.Contains(i * (jy + 1) + j)))

                              {

                                 fanwei.Add(i * (jy + 1) + j);                                  

                                 if (list.Contains(i * (jy + 1) + j))

                                        {

                                                           list.Remove(i * (jy + 1) + j);

                                                             xunhuan(fanwei, i, j, list);

                                            }

                                        }
                                       
                                    }
                                }
                            }
                        }
                    }        
          
           
        }
0
h
huanyingni
改成这样以后,仍然出现循环错误,怎么办?
0
h
huanyingni
private void xunhuan(List<int> fanwei, int tx, int ty, List<int> list)

  {

        List<int> test=new    List<int>();

        for (int i = (tx - 1); i <= (tx + 1); i++)

                    { 
                        if ((i>=0)&&(i <= ix)) 
                        { 
                            for (int j = (ty - 1); j <= (ty + 1); j++) 
                            { 
                                if ((j>=0)&&(j <= jy)) 
                                { 
                                    if (!(fanwei.Contains(i * (jy + 1) + j))) 

                            {

                                fanwei.Add(i * (jy + 1) + j);                                                                   test.Add(i * (jy + 1) + j);                            

                                        }                                          
                                    } 
                                } 
                            } 
                        } 

                  for(int i=0;i<test.Count;i++)

                    {  

                        int tx0=(int)test[i]/(jy+1);

                        int ty0=test[i]-tx0*(jy+1);
                        

                         if (list.Contains(tx0 * (jy + 1) + ty0))

                         {

                               list.Remove(tx0 * (jy + 1) + ty0);

                               xunhuan(fanwei, tx0,ty0, list);

                           }

            }

           
            
        }
0
h
huanyingni
为什么上边那么做还是不对?还死循环?
返回顶部
顶部