그냥 저냥 지나가다가

#include <stdio.h>
#define countof( a ) ( sizeof a / sizeof a[ 0 ] )

constexpr int c = 7;
constexpr int G = 2;

int maze[][ c ]
{
	{ 0,0,0,0,0,0,0 },
	{ 0,1,1,1,1,1,0 },
	{ 0,0,0,1,0,0,0 },
	{ 0,0,1,1,1,1,0 },
	{ 0,0,1,0,0,0,0 },
	{ 0,0,1,1,1,G,0 },
	{ 0,0,0,0,0,0,0 },
};

constexpr int r = countof( maze );

constexpr int d[]
{
	      -c,
	-1,         +1,
	      +c,

	-1-c,     -c+1,

    -1+c,     +c+1,	
};

int costs[ r * c ];

void move( int* pos, int cost = 0 )
{
	*pos = 0;
	for( int i = 0; i < countof( d ); ++i )
		if( pos[ d[ i ] ] )
			if( pos[ d[ i ] ] == G )
				++costs[ cost + i / 4 + 1 ];
			else
                move( pos + d[ i ], cost + i / 4 + 1 );
	*pos = 1;
}

int main()
{
    move( &maze[ 1 ][ 1 ] );
    for( auto cost: costs )
    	if( cost )
    	{
	        printf( "%d\n", cost );
	        break;
    	}

	return  0;
}
4 Likes

예쁜 dfs네요

1 Like