5.2.1 Snail Trails 蜗牛的旅行
/*
PROG: snail
LANG: C++
*/
#include < iostream >
using namespace std;
int N,M;
int ans;
int G[ 121 ][ 121 ] = { 0 };
int d[ 4 ][ 2 ] = { 0 , - 1 , - 1 , 0 , 0 , 1 , 1 , 0 };
void init()
{
scanf( " %d%d\n " , & N, & M);
int a; char b;
for ( int i = 1 ;i <= M; ++ i){
scanf( " %c%d\n " , & b, & a);
G[a][b - ' A ' + 1 ] = 1 ;
}
for ( int i = 1 ;i <= N; ++ i)
G[i][ 0 ] = G[ 0 ][i] = G[i][N + 1 ] = G[N + 1 ][i] = 1 ;
}
void dfs( int x, int y, int cur, int k){
if (G[x][y] == 2 ){
if (cur > ans) ans = cur;
return ;
}
G[x][y] = 2 ;
if (cur > ans) ans = cur;
if (G[x + d[k][ 0 ]][y + d[k][ 1 ]] != 1 )
dfs(x + d[k][ 0 ],y + d[k][ 1 ],cur + 1 ,k);
else {
for ( int i = 0 ;i < 4 ; ++ i) if (k != i){
int xi = x + d[i][ 0 ],yi = y + d[i][ 1 ];
if (G[xi][yi] != 1 )
dfs(xi,yi,cur + 1 ,i);
}
}
G[x][y] = 0 ;
}
int main()
{
freopen( " snail.in " , " r " ,stdin);
freopen( " snail.out " , " w " ,stdout);
init();
dfs( 1 , 1 , 0 , 2 );
dfs( 1 , 1 , 0 , 3 );
printf( " %d\n " ,ans);
return 0 ;
}
/*
Executing
Test 1: TEST OK [0.000 secs, 2860 KB]
Test 2: TEST OK [0.000 secs, 2860 KB]
Test 3: TEST OK [0.000 secs, 2860 KB]
Test 4: TEST OK [0.000 secs, 2860 KB]
Test 5: TEST OK [0.000 secs, 2860 KB]
Test 6: TEST OK [0.000 secs, 2860 KB]
Test 7: TEST OK [0.000 secs, 2860 KB]
Test 8: TEST OK [0.011 secs, 2860 KB]
Test 9: TEST OK [0.000 secs, 2860 KB]
Test 10: TEST OK [0.000 secs, 2860 KB]
Test 11: TEST OK [0.000 secs, 2860 KB]
Test 12: TEST OK [0.043 secs, 2860 KB]
*/
PROG: snail
LANG: C++
*/
#include < iostream >
using namespace std;
int N,M;
int ans;
int G[ 121 ][ 121 ] = { 0 };
int d[ 4 ][ 2 ] = { 0 , - 1 , - 1 , 0 , 0 , 1 , 1 , 0 };
void init()
{
scanf( " %d%d\n " , & N, & M);
int a; char b;
for ( int i = 1 ;i <= M; ++ i){
scanf( " %c%d\n " , & b, & a);
G[a][b - ' A ' + 1 ] = 1 ;
}
for ( int i = 1 ;i <= N; ++ i)
G[i][ 0 ] = G[ 0 ][i] = G[i][N + 1 ] = G[N + 1 ][i] = 1 ;
}
void dfs( int x, int y, int cur, int k){
if (G[x][y] == 2 ){
if (cur > ans) ans = cur;
return ;
}
G[x][y] = 2 ;
if (cur > ans) ans = cur;
if (G[x + d[k][ 0 ]][y + d[k][ 1 ]] != 1 )
dfs(x + d[k][ 0 ],y + d[k][ 1 ],cur + 1 ,k);
else {
for ( int i = 0 ;i < 4 ; ++ i) if (k != i){
int xi = x + d[i][ 0 ],yi = y + d[i][ 1 ];
if (G[xi][yi] != 1 )
dfs(xi,yi,cur + 1 ,i);
}
}
G[x][y] = 0 ;
}
int main()
{
freopen( " snail.in " , " r " ,stdin);
freopen( " snail.out " , " w " ,stdout);
init();
dfs( 1 , 1 , 0 , 2 );
dfs( 1 , 1 , 0 , 3 );
printf( " %d\n " ,ans);
return 0 ;
}
/*
Executing
Test 1: TEST OK [0.000 secs, 2860 KB]
Test 2: TEST OK [0.000 secs, 2860 KB]
Test 3: TEST OK [0.000 secs, 2860 KB]
Test 4: TEST OK [0.000 secs, 2860 KB]
Test 5: TEST OK [0.000 secs, 2860 KB]
Test 6: TEST OK [0.000 secs, 2860 KB]
Test 7: TEST OK [0.000 secs, 2860 KB]
Test 8: TEST OK [0.011 secs, 2860 KB]
Test 9: TEST OK [0.000 secs, 2860 KB]
Test 10: TEST OK [0.000 secs, 2860 KB]
Test 11: TEST OK [0.000 secs, 2860 KB]
Test 12: TEST OK [0.043 secs, 2860 KB]
*/
/*
PROG: snail
LANG: C++
*/
#include < iostream >
using namespace std;
int N,M;
int ans = 0 ;
int map[ 121 ][ 121 ] = { 0 };
void init()
{
scanf( " %d%d\n " , & N, & M);
char b; int a;
for ( int i = 1 ;i <= M; ++ i){
scanf( " %c%d\n " , & b, & a);
map[a][b - ' A ' + 1 ] =- 1 ;
}
for ( int i = 1 ;i <= N; ++ i)
map[i][ 0 ] = map[i][N + 1 ] = map[ 0 ][i] = map[N + 1 ][i] =- 1 ;
}
void dfs( int x, int y, int res){
int dx,dy;
bool b;
for (dx = x - 1 ,dy = y,b = 0 ;dx >= 1 ; -- dx){ // 上
if (map[dx][dy] ==- 1 ) break ;
if (map[dx][dy] == 1 ){
if (res + x - dx > ans)
ans = res + x - dx;
b = 1 ;
break ;
}
map[dx][dy] = 1 ;
}
dx ++ ;
if ( ! b &&! (dx == x && dy == y)) dfs(dx,dy,res + x - dx);
for (;dx < x; ++ dx)
map[dx][dy] = 0 ;
for (dx = x + 1 ,dy = y,b = 0 ;dx <= N; ++ dx){ // 下
if (map[dx][dy] ==- 1 ) break ;
if (map[dx][dy] == 1 ){
if (res + dx - x > ans)
ans = res + dx - x;
b = 1 ;
break ;
}
map[dx][dy] = 1 ;
}
dx -- ;
if ( ! b &&! (dx == x && dy == y)) dfs(dx,dy,res + dx - x);
for (;dx > x; -- dx)
map[dx][dy] = 0 ;
for (dx = x,dy = y - 1 ,b = 0 ;dy >= 1 ; -- dy){ // 左
if (map[dx][dy] ==- 1 ) break ;
if (map[dx][dy] == 1 ){
if (res + y - dy > ans)
ans = res + y - dy;
b = 1 ;
break ;
}
map[dx][dy] = 1 ;
}
dy ++ ;
if ( ! b &&! (dx == x && dy == y)) dfs(dx,dy,res + y - dy);
for (;dy < y; ++ dy)
map[dx][dy] = 0 ;
for (dx = x,dy = y + 1 ,b = 0 ;dy <= N; ++ dy){ // 右
if (map[dx][dy] ==- 1 ) break ;
if (map[dx][dy] == 1 ){
if (res + dy - y > ans)
ans = res + dy - y;
b = 1 ;
break ;
}
map[dx][dy] = 1 ;
}
dy -- ;
if ( ! b &&! (dx == x && dy == y)) dfs(dx,dy,res + dy - y);
for (;dy > y; -- dy)
map[dx][dy] = 0 ;
}
int main()
{
freopen( " snail.in " , " r " ,stdin);
freopen( " snail.out " , " w " ,stdout);
init();
map[ 1 ][ 1 ] = 1 ;
dfs( 1 , 1 , 0 );
printf( " %d\n " ,ans);
return 0 ;
}
/*
Executing
Test 1: TEST OK [0.011 secs, 2856 KB]
Test 2: TEST OK [0.000 secs, 2856 KB]
Test 3: TEST OK [0.000 secs, 2856 KB]
Test 4: TEST OK [0.000 secs, 2856 KB]
Test 5: TEST OK [0.000 secs, 2856 KB]
Test 6: TEST OK [0.011 secs, 2856 KB]
Test 7: TEST OK [0.000 secs, 2856 KB]
Test 8: TEST OK [0.000 secs, 2856 KB]
Test 9: TEST OK [0.000 secs, 2856 KB]
Test 10: TEST OK [0.000 secs, 2856 KB]
Test 11: TEST OK [0.000 secs, 2856 KB]
Test 12: TEST OK [0.022 secs, 2856 KB]
*/
PROG: snail
LANG: C++
*/
#include < iostream >
using namespace std;
int N,M;
int ans = 0 ;
int map[ 121 ][ 121 ] = { 0 };
void init()
{
scanf( " %d%d\n " , & N, & M);
char b; int a;
for ( int i = 1 ;i <= M; ++ i){
scanf( " %c%d\n " , & b, & a);
map[a][b - ' A ' + 1 ] =- 1 ;
}
for ( int i = 1 ;i <= N; ++ i)
map[i][ 0 ] = map[i][N + 1 ] = map[ 0 ][i] = map[N + 1 ][i] =- 1 ;
}
void dfs( int x, int y, int res){
int dx,dy;
bool b;
for (dx = x - 1 ,dy = y,b = 0 ;dx >= 1 ; -- dx){ // 上
if (map[dx][dy] ==- 1 ) break ;
if (map[dx][dy] == 1 ){
if (res + x - dx > ans)
ans = res + x - dx;
b = 1 ;
break ;
}
map[dx][dy] = 1 ;
}
dx ++ ;
if ( ! b &&! (dx == x && dy == y)) dfs(dx,dy,res + x - dx);
for (;dx < x; ++ dx)
map[dx][dy] = 0 ;
for (dx = x + 1 ,dy = y,b = 0 ;dx <= N; ++ dx){ // 下
if (map[dx][dy] ==- 1 ) break ;
if (map[dx][dy] == 1 ){
if (res + dx - x > ans)
ans = res + dx - x;
b = 1 ;
break ;
}
map[dx][dy] = 1 ;
}
dx -- ;
if ( ! b &&! (dx == x && dy == y)) dfs(dx,dy,res + dx - x);
for (;dx > x; -- dx)
map[dx][dy] = 0 ;
for (dx = x,dy = y - 1 ,b = 0 ;dy >= 1 ; -- dy){ // 左
if (map[dx][dy] ==- 1 ) break ;
if (map[dx][dy] == 1 ){
if (res + y - dy > ans)
ans = res + y - dy;
b = 1 ;
break ;
}
map[dx][dy] = 1 ;
}
dy ++ ;
if ( ! b &&! (dx == x && dy == y)) dfs(dx,dy,res + y - dy);
for (;dy < y; ++ dy)
map[dx][dy] = 0 ;
for (dx = x,dy = y + 1 ,b = 0 ;dy <= N; ++ dy){ // 右
if (map[dx][dy] ==- 1 ) break ;
if (map[dx][dy] == 1 ){
if (res + dy - y > ans)
ans = res + dy - y;
b = 1 ;
break ;
}
map[dx][dy] = 1 ;
}
dy -- ;
if ( ! b &&! (dx == x && dy == y)) dfs(dx,dy,res + dy - y);
for (;dy > y; -- dy)
map[dx][dy] = 0 ;
}
int main()
{
freopen( " snail.in " , " r " ,stdin);
freopen( " snail.out " , " w " ,stdout);
init();
map[ 1 ][ 1 ] = 1 ;
dfs( 1 , 1 , 0 );
printf( " %d\n " ,ans);
return 0 ;
}
/*
Executing
Test 1: TEST OK [0.011 secs, 2856 KB]
Test 2: TEST OK [0.000 secs, 2856 KB]
Test 3: TEST OK [0.000 secs, 2856 KB]
Test 4: TEST OK [0.000 secs, 2856 KB]
Test 5: TEST OK [0.000 secs, 2856 KB]
Test 6: TEST OK [0.011 secs, 2856 KB]
Test 7: TEST OK [0.000 secs, 2856 KB]
Test 8: TEST OK [0.000 secs, 2856 KB]
Test 9: TEST OK [0.000 secs, 2856 KB]
Test 10: TEST OK [0.000 secs, 2856 KB]
Test 11: TEST OK [0.000 secs, 2856 KB]
Test 12: TEST OK [0.022 secs, 2856 KB]
*/