# include <bits/stdc++.h>
# define x first
# define y second
using namespace std;
typedef pair< int , int > pii;
const int dx[ 4 ] = { 1 , 0 , - 1 , 0 } ;
const int dy[ 4 ] = { 0 , - 1 , 0 , 1 } ;
const int N = 1010 ;
char g[ N] [ N] ;
int d[ N] [ N] ;
bool st[ N] [ N] ;
int n, m;
void bfs ( )
{
queue< pii> q;
for ( int i = 0 ; i < n; i++ )
{
for ( int j = 0 ; j < m; j++ )
{
if ( g[ i] [ j] == '1' )
{
q. push ( { i, j} ) ;
st[ i] [ j] = true ;
}
}
}
while ( q. size ( ) )
{
pii t = q. front ( ) ;
q. pop ( ) ;
for ( int k = 0 ; k < 4 ; k++ )
{
int nx = t. x + dx[ k] , ny = t. y + dy[ k] ;
if ( nx >= 0 && nx < n && ny >= 0 && ny < m)
{
if ( ! st[ nx] [ ny] )
{
d[ nx] [ ny] = d[ t. x] [ t. y] + 1 ;
q. push ( { nx, ny} ) ;
st[ nx] [ ny] = true ;
}
}
}
}
}
int main ( )
{
cin >> n >> m;
for ( int i = 0 ; i < n; i++ ) cin >> g[ i] ;
bfs ( ) ;
for ( int i = 0 ; i < n; i++ )
{
for ( int j = 0 ; j < m; j++ )
{
cout << d[ i] [ j] << ' ' ;
}
cout << endl;
}
return 0 ;
}
# include <bits/stdc++.h>
using namespace std;
char target[ 10 ] ;
string basic = "12345678" ;
unordered_map< string, int > d;
unordered_map< string, bool > st;
unordered_map< string, string> path;
string move_A ( string s)
{
string ts = "" ;
for ( int i = 7 ; i >= 0 ; i -- ) ts += s[ i] ;
return ts;
}
string move_B ( string s)
{
string ts = "" ;
ts += s[ 3 ] ;
for ( int i = 0 ; i < 3 ; i++ ) ts += s[ i] ;
for ( int i = 5 ; i < 8 ; i++ ) ts += s[ i] ;
ts += s[ 4 ] ;
return ts;
}
string move_C ( string s)
{
string ts = "" ;
ts = ts + s[ 0 ] + s[ 6 ] + s[ 1 ] + s[ 3 ] + s[ 4 ] + s[ 2 ] + s[ 5 ] + s[ 7 ] ;
return ts;
}
int bfs ( )
{
queue< string> q;
q. push ( basic) ;
st[ basic] = true ;
d[ basic] = 0 ;
path[ basic] = "" ;
while ( q. size ( ) )
{
string t = q. front ( ) ;
q. pop ( ) ;
if ( t == target) return d[ t] ;
string as = move_A ( t) ;
if ( ! st[ as] )
{
st[ as] = true ;
d[ as] = d[ t] + 1 ;
path[ as] = path[ t] + 'A' ;
q. push ( as) ;
}
string bs = move_B ( t) ;
if ( ! st[ bs] )
{
st[ bs] = true ;
d[ bs] = d[ t] + 1 ;
path[ bs] = path[ t] + 'B' ;
q. push ( bs) ;
}
string cs = move_C ( t) ;
if ( ! st[ cs] )
{
st[ cs] = true ;
d[ cs] = d[ t] + 1 ;
path[ cs] = path[ t] + 'C' ;
q. push ( cs) ;
}
}
return - 1 ;
}
int main ( )
{
for ( int i = 0 ; i < 8 ; i++ ) cin >> target[ i] ;
if ( target == basic)
{
cout << 0 << endl;
return 0 ;
}
cout << bfs ( ) << endl;
cout << path[ target] << endl;
return 0 ;
}
# include <bits/stdc++.h>
# define x first
# define y second
using namespace std;
typedef pair< int , int > pii;
const int dx[ 4 ] = { - 1 , 1 , 1 , - 1 } ;
const int dy[ 4 ] = { - 1 , - 1 , 1 , 1 } ;
const int ix[ 4 ] = { - 1 , 0 , 0 , - 1 } ;
const int iy[ 4 ] = { - 1 , - 1 , 0 , 0 } ;
const char match[ 4 ] = { '\\' , '/' , '\\' , '/' } ;
const int N = 510 ;
int T, n, m;
char g[ N] [ N] ;
int d[ N] [ N] ;
void bfs ( )
{
memset ( d, 0x3f , sizeof d) ;
deque< pii> q;
q. push_back ( { 0 , 0 } ) ;
d[ 0 ] [ 0 ] = 0 ;
while ( q. size ( ) )
{
pii t = q. front ( ) ;
q. pop_front ( ) ;
for ( int k = 0 ; k < 4 ; k++ )
{
int nx = t. x + dx[ k] , ny = t. y + dy[ k] ;
if ( nx >= 0 && nx <= n && ny >= 0 && ny <= m)
{
if ( g[ t. x + ix[ k] ] [ t. y + iy[ k] ] == match[ k] )
{
if ( d[ nx] [ ny] > d[ t. x] [ t. y] )
{
d[ nx] [ ny] = d[ t. x] [ t. y] ;
q. push_front ( { nx, ny} ) ;
}
}
else if ( d[ nx] [ ny] > d[ t. x] [ t. y] + 1 )
{
d[ nx] [ ny] = d[ t. x] [ t. y] + 1 ;
q. push_back ( { nx, ny} ) ;
}
}
}
}
}
int main ( )
{
cin >> T;
while ( T -- )
{
cin >> n >> m;
for ( int i = 0 ; i < n; i++ ) cin >> g[ i] ;
bfs ( ) ;
if ( d[ n] [ m] == 0x3f3f3f3f ) puts ( "NO SOLUTION" ) ;
else cout << d[ n] [ m] << endl;
}
return 0 ;
}