USACO 2.4 The Tamworth Two

USACO 2.4 The Tamworth Two


模拟题。按题目要求编码即可,每过一分钟,更新一下农夫和奶牛的状态。如果该状态以前出现过,说明有循环,不可能到达,输出0.

#include  < iostream >
#include 
< fstream >

using   namespace  std;

ifstream fin(
" ttwo.in " );
ofstream fout(
" ttwo.out " );

#ifdef _DEBUG
#define  out cout
#define  in cin
#else
#define  out fout
#define  in fin
#endif

char  grid[ 10 ][ 10 ];

bool  visited[ 10 ][ 10 ][ 4 ][ 10 ][ 10 ][ 4 ];

int  north  =   0 ;
int  east  =   1 ;
int  south  =   2 ;
int  west  =   3 ;

// 方向为0,1,2,3时向前走一步row和column的增加值
int  delr[ 4 =  {
    
- 1 , 0 , 1 , 0
};
int  delc[ 4 =  {
    
0 , 1 , 0 , - 1
};

//  farmer的当前row,column,direction.
int  fc,fr,fd;
int  cc,cr,cd;

bool  available( int  i, int  j)
{
    
if (i >= 0 && i <= 9 && j >= 0 && j <= 9 && grid[i][j] != ' * ' )
        
return   true ;
    
else
        
return   false ;
}

void  one_step()
{
    
if ( available( fr + delr[fd],fc + delc[fd] ) ){
        fr 
+=  delr[fd];
        fc 
+=  delc[fd];
    }
else {
        fd
+= 1 ;
        fd
%= 4 ;
    }

    
if ( available( cr + delr[cd],cc + delc[cd] ) ){
        cr 
+=  delr[cd];
        cc 
+=  delc[cd];
    }
else {
        cd
+= 1 ;
        cd
%= 4 ;
    }
}

void  solve()
{
    
for ( int  i = 0 ;i < 10 ; ++ i)
        
for ( int  j = 0 ;j < 10 ; ++ j){
            
in >> grid[i][j];
            
if (grid[i][j] == ' F ' )
                fr
= i,fc = j,fd = 0 ;
            
if (grid[i][j] == ' C ' )
                cr
= i,cc = j,cd = 0 ;
        }

    memset(visited,
0 , sizeof (visited));

    visited[fc][fr][fd][cc][cr][cd] 
=   true ;

    
int  res  =   0 ;

    
while ( true ){

        one_step();
        res
++ ;

        
if ( fc == cc && fr == cr ){
            
out << res << endl;
            exit(
0 );
        }

        
if (visited[fc][fr][fd][cc][cr][cd]){
            
out << 0 << endl;
            exit(
0 );
        }

        visited[fc][fr][fd][cc][cr][cd]
= true ;
    }
}

int  main( int  argc, char   * argv[])
{
    solve(); 
    
return   0 ;
}


你可能感兴趣的:(USACO 2.4 The Tamworth Two)