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

using namespace std;

int main()
{
    int plansza[10][10];
    srand(time(NULL));
    for(int x=0; x<10;x++)
    {
        for(int y=0; y<10; y++)
            plansza[x][y]=rand()%10; //wypelnianie tablicy losowymi wartosciami z przedzialu (0,9)
    }
    for(int x=0; x<10;x++)
    {
        for(int y=0; y<10; y++)
            cout<<plansza[x][y]<<" ";
        cout<<endl;
    }
    int pozycja, suma=0,tmx=0,tmp;

    for(int y=0; y<10; y++)
    {
        pozycja=0;

        if(y==0)
        {
            for(int x=0; x<10 ; x++)
            {

                if(plansza[x][y]>pozycja)
                {
                    pozycja=plansza[x][y];
                    tmx=x;
                }

            }
        }
        else
        {
            if(tmx>0 && tmx<9)

                {
                    for(int x=tmx-1; x<=tmx+1; x++)
                    {
                        if(plansza[x][y]>pozycja)
                        {
                            pozycja=plansza[x][y];
                            //tmx=x;
                            tmp=x;
                        }
                        else if(plansza[x][y]==pozycja) continue;
                    }
                    tmx=tmp;

                }
            else if(tmx==0)
                {
                    for(int x=tmx; x<=tmx+1; x++)
                    {
                        if(plansza[x][y]>pozycja)
                        {
                            pozycja=plansza[x][y];
                            //tmx=x;
                            tmp=x;
                        }
                    }
                    tmx=tmp;

                }
            else if(tmx==9)
                {
                    for(int x=tmx; x>=tmx-1; x--)
                    {
                        if(plansza[x][y]>pozycja)
                        {
                            pozycja=plansza[x][y];
                            //tmx=x;
                            tmp=x;
                        }
                    }
                    tmx=tmp;

                }

        }
        //cout<<"tmx= "<<tmx<<endl;
      //  cout<<"pozycja="<<pozycja<<endl;
        suma+=pozycja;
    }
    cout<<"suma="<<suma;

    return 0;
}
