Cod sursă (job #115777)

Utilizator avatar dummy contdezactivat dummy IP ascuns
Problemă Immortal (clasele 9-10) Compilator cpp | 3,68 kb
Rundă Tema 14 clasele 9-10 2014/15 Status evaluat
Dată 10 feb. 2015 10:13:56 Scor 24
#include <fstream>
#define Dragoste 20
#define Smerenie 15
#define Nadejde 4

using namespace std ;

typedef struct {
        short l , c ;
}       immortal ;

typedef struct {
        short l , c , dir ;
}       solution ;

immortal imm[ Smerenie ] ;
solution Sol[ Smerenie ] ;
short Board[ Dragoste + 4 ][ Dragoste + 4 ] ;
short Go[ Nadejde ][ 2 ] = { { -1 , 0 } ,  {  0 , 1  } , { 1 , 0 } , { 0 , -1 } } ;
short nl , nc , nondp , nonp ;
bool gasit ;

ofstream fout( "immortal.out" ) ;

void Bordare( )
{
    short i ;

    for( i = 2 ; i < nl + 2 ; i ++ )
       {
         Board[ i ][ 0 ] = Board[ i ][ 1 ] = Board[ i ][ nc + 2 ] = Board[ i ][ nc + 3 ] = 7 ;
       }
    for( i = 2 ; i < nc + 2 ; i ++ )
       {
         Board[ 0 ][ i ] = Board[ 1 ][ i ] = Board[ nl + 2 ][ i ] = Board[ nl + 3 ][ i ] = 7 ;
       }
}

void PrintSolution( )
{
    short i ;
    for( i = nondp ; i > 1 ; i -- )
       {
         fout<< Sol[ i ].l - 1 <<" " ;
         fout<< Sol[ i ].c - 1<<" " ;
         fout<< Sol[ i ].l + ( Go[ Sol[ i ].dir ][ 0 ] << 1 ) - 1 <<" " ;
         fout<< Sol[ i ].c + ( Go[ Sol[ i ].dir ][ 1 ] << 1 ) - 1 <<" " ;
         fout<< "\n" ;
       }
    fout.close( ) ;
}

void SearchNext ( )
{
    short i , j ;
    short l1 ;
    short c1 ;
    short l2 ;
    short c2 ;

    if( !gasit )
      {
       if( nonp == 1 )
         {
           PrintSolution( ) ;
           gasit = 1 ;
         }
       else
          {
          for( i = 0 ; i < nondp ; i ++ )
             {
                if( Board[ imm[ i ].l ][ imm[ i ].c ] == 1 )
                  {
                   for( j = 0 ; j < Nadejde ; j ++ )
                      {
                      if( !Board[ imm[ i ].l + ( Go[ j ][ 0 ] << 1 ) ][ imm[ i ].c + ( Go[ j ][ 1 ] << 1 ) ] && Board[ imm[ i ].l + Go[ j ][ 0 ] ][ imm[ i ].c + Go[ j ][ 1 ] ] == 1 )
                        {
                        l1 = imm[ i ].l + Go[ j ][ 0 ] ;
                        c1 = imm[ i ].c + Go[ j ][ 1 ] ;
                        l2 = imm[ i ].l + ( Go[ j ][ 0 ] << 1 ) ;
                        c2 = imm[ i ].c + ( Go[ j ][ 1 ] << 1 ) ;

                        ///Schimbam valoarea de adevar a pozitiilor
                        Board[ imm[ i ].l ][ imm[ i ].c ] = 0 ;
                        Board[ l1 ][ c1 ] = 0 ;
                        Board[ l2 ][ c2 ] = 1 ;

                        Sol[ nonp ] = { imm[ i ].l , imm[ i ].c , j } ;

                        imm[ i ].l = l2 ;
                        imm[ i ].c = c2 ;
                        nonp -- ;

                        ///Apelam recursiv functia SearchNext
                        SearchNext( ) ;

                        ///Refacem tabla de joc
                        nonp ++ ;

                        imm[ i ].l = l2 - ( Go[ j ][ 0 ] << 1 ) ;
                        imm[ i ].c = c2 - ( Go[ j ][ 1 ] << 1 ) ;

                        Board[ imm[ i ].l ][ imm[ i ].c ] = 1 ;
                        Board[ l1 ][ c1 ] = 1 ;
                        Board[ l2 ][ c2 ] = 0 ;
                        }
                    }
                }
            }
        }
    }
}

int main( )
{
    short i , linie , coloana ;

    ifstream fin ( "immortal.in" ) ;

    fin>>nl>>nc>>nondp ;

    for( i = 0 ; i < nondp ; i ++ )
       {
         fin>>linie>>coloana ;
         imm[ i ].l = linie + 1 ;
         imm[ i ].c = coloana + 1 ;
         Board[ imm[ i ].l ][ imm[ i ].c ] = 1 ;
       }
    Bordare( ) ;

    ///fout<<"Iisuse miluieste - ma \n";

    fin.close( ) ;

    nonp = nondp ;

    SearchNext( ) ;

    return 0 ;
    ///Gresit-am inaintea Ta si nu sunt vrednic sa ma numesc fiul Tau
}