适用于吸尘器中的环路

时间:2016-03-06 18:25:44

标签: c++ for-loop

我正在编写一个关于真空吸尘器的代码,用于清洁二维阵列中的灰尘。我希望这个清洁工走到最后一行,然后回来完成下一行,依此类推。但是在这段代码中,它会在第一行结束并再次回到第一行的开头!我该怎么办?非常感谢你。

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(0));
    int i = 0;
    int j;
    int x = 0;
    int y = 0;
    int m = 0;
    int t = 0;
    int array[5][5];
    int step = 0 , trash, total = 0, num = 0;

    //Prepare the random array

    for (x=0; x<5; x++)
    {
        for(y=0; y<5; y++)
        {

            array[x][y] = rand() % 2;

            if (array[x][y] == 1)
            {
               total++;
            }
        }
    }

    //Print the total number of trashes based on the random number

    cout<<endl<<"The total number of trashes are: "<<total<<endl<<endl;

    //print the random array

    for (x=0; x<5; x++)
    {
        for(y=0; y<5; y++)
        {
            cout<<array[x][y]<<"\t";
        }
        cout<<endl;
    }
    cout<<endl<<endl;

    //------Manual Garbage Collector
    cout<<"------ Manual Garbage Collector ------"<<endl<<endl;

    while(num != total)
    {
        if(m % 2 != 0)
        {
            if(t==1)
                i=1;
            else if (t==3)
                i=3;

            for(i ; i<5 ; i++)
            {
                for(j=5 ; j>0 ; j--)
                {
                    trash = array[i][j];

                    if(trash == 1)
                    {
                        cout<<"I found a trash in   -->   "<<"row: "<<i<<"\t column: "<<j<<endl;
                        num++;
                        step++;
                    }
                    else
                    {
                        step++;
                    }
                }
            m++;
            t++;
            break;
            }
        }
        else if (m % 2 == 0)
        {
            if(t==2)
                i=2;
            else if (t==4)
                i=4;

            for(i ; i<5 ; i++)
            {
                for(j=0 ; j<5 ; j++)
                {
                    trash=array[i][j];

                    if(trash == 1)
                    {
                        cout<<"I found a trash in   -->   "<<"row: "<<i<<"\t column: "<<j<<endl;
                        num++;
                        step++;
                    }
                    else
                    {
                        step++;
                    }

                }
            m++;
            t++;
            break;

            }
        }
    }

    cout<<endl<<"----------------"<<endl<<"The number of steps that I have tried: "<<step<<endl;

    return 0;
}

1 个答案:

答案 0 :(得分:0)

你的代码在到达行尾时实际上会进入下一行,但是在你的第一个j循环(第65行)中有一个问题,因为它超出了界限。它应该是:

for( j = 4; j >= 0; j-- ) {

但更大的问题是整个逻辑很奇怪且不必要地复杂化。同样的任务可以用这样简单的东西完成:

const int SIZE = 5;
int direction[] = { 1, -1 };
int lower_limit[] = { 0, SIZE - 1 };
int count = 0;
int k = 0;

// for every row
for ( int i = 0; i < SIZE; i++ ) {
    // sweep the row in the right direction
    int j = lower_limit[k];
    for( int l = 0; l < SIZE; ++l ) {
        // clean the trash
        if ( array[i][j] ) {

            cout << "I found a trash in   -->   row: " << i
                 << "\t column: " << j << '\n';
            ++count;

            // I don't think it's worth checking now if all the dust were
            // collected, you'll save a few iterations but this condition
            // has to be checked for half of the cells at average
        }
        j += direction[k];
    }
    k = ( k == 0 ) ? 1 : 0;
}

或者更类似于您的代码:

const int SIZE = 5;
int count = 0;
bool going_right = true;

// for every row
for ( int i = 0; i < SIZE; i++ ) {
    // sweep the row in the right direction
    if ( going_right ) {
        for( int j = 0; j < SIZE; ++j ) {
            // clean the trash
            if ( array[i][j] ) {
                cout << "I found a trash in   -->   row: " << i
                     << "\t column: " << j << '\n';
                ++count;
            }
        }
        // reverse direction
        going_right = false;
    }
    else {
        for( int j = SIZE - 1; j >= 0; --j ) {
            // clean the trash
            if ( array[i][j] ) {
                cout << "I found a trash in   -->   row: " << i
                    << "\t column: " << j << '\n';
                ++count;
            }
        }
        going_right = true;
    }
}

在你的程序中给出了这个输出:

The total number of trashes are: 16

1   0   1   0   0   
1   1   0   1   0   
1   1   0   1   1   
1   1   1   0   0   
0   1   1   1   1   


------ Manual Garbage Collector ------

I found a trash in   -->   row: 0    column: 0
I found a trash in   -->   row: 0    column: 2
I found a trash in   -->   row: 1    column: 3
I found a trash in   -->   row: 1    column: 1
I found a trash in   -->   row: 1    column: 0
I found a trash in   -->   row: 2    column: 0
I found a trash in   -->   row: 2    column: 1
I found a trash in   -->   row: 2    column: 3
I found a trash in   -->   row: 2    column: 4
I found a trash in   -->   row: 3    column: 2
I found a trash in   -->   row: 3    column: 1
I found a trash in   -->   row: 3    column: 0
I found a trash in   -->   row: 4    column: 1
I found a trash in   -->   row: 4    column: 2
I found a trash in   -->   row: 4    column: 3
I found a trash in   -->   row: 4    column: 4

----------------
The number of steps that I have tried: 25