usaco 5.2 Snail Trails(DFS)

Snail Trails
All Ireland Contest

Sally Snail likes to stroll on a N x N square grid (1 <n <= 120). She always starts in the upper left corner of the grid. The grid has empty squares (denoted below by `.') and a number (B) of barriers (denoted below by `#'). Here is a depiction of a grid including a demonstration of the grid labelling algorithm:

          A B C D E F G H
        1 S . . . . . # .
        2 . . . . # . . .
        3 . . . . . . . .
        4 . . . . . . . .
        5 . . . . . # . .
        6 # . . . . . . .
        7 . . . . . . . .
        8 . . . . . . . .

Sally travels vertically (up or down) or horizontally (left or right). Sally can travel either down or right from her starting location, which is always A1.

Sally travels as long as she can in her chosen direction. She stops and turns 90 degrees whenever she encounters the edge of the board or one of the barriers. She can not leave the grid or enter a space with a barrier. Additionally, Sally can not re-cross any square she has already traversed. She stops her traversal altogether any time she can no longer make a move.

Here is one sample traversal on the sample grid above:

          A B C D E F G H
        1 S---------+ # .
        2 . . . . # | . .
        3 . . . . . | . .
        4 . . . . . +---+
        5 . . . . . # . |
        6 # . . . . . . |
        7 +-----------+ |
        8 +-------------+

Sally traversed right, down, right, down, left, up, and right. She could not continue since she encountered a square already visited. Things might have gone differently if she had chosen to turn back toward our left when she encountered the barrier at F5.

Your task is to determine and print the largest possible number of squares that Sally can visit if she chooses her turns wisely. Be sure to count square A1 as one of the visited squares.

PROGRAM NAME: snail

INPUT FORMAT

The first line of the input has N, the dimension of the square, and B, the number of barriers (1 <= B <= 200). The subsequent B lines contain the locations of the barriers. The sample input file below describes the sample grid above. The sample output file below is supposed to describe the traversal shown above. Note that when N > 26 then the input file can not specify barriers to the right of column Z.

SAMPLE INPUT (file snail.in)

8 4
E2
A6
G1
F5

OUTPUT FORMAT

The output file should consist of exactly one line, the largest possible number of squares that Sally can visit.

SAMPLE OUTPUT (file snail.out)

33
Using this traversal:
          A B C D E F G H
        1 S . . . . . # .
        2 | . . . # . . .
        3 | . . . +-----+
        4 | . . . | . . |
        5 +-------+ # . |
        6 # . . . . . . |
        7 +------------ |
        8 +-------------+

题意:一只欧牛在方格里移动,每次从A1开始,移动到遇到障碍物或之前走过的格子,如果是障碍物就向左或向右转,遇到走过的格子就停下来,为最多能走过几个格子

分析:题目描述比较唬人,不过没什么难的,直接爆搜就行。。。

写搜索的能力降得太厉害了,这题写了近一个小时,还wa了一次= =

代码:

/*
ID: 15114582
PROG: snail
LANG: C++
*/
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int mm=222;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
int map[mm][mm];
int i,j,n,m,ans;
char po;
void dfs(int x0,int y0,int d0,int s0)
{
    int x,y,d,s;
    for(d=(d0+3)%4,x=x0+dx[d],y=y0+dy[d],s=s0;!map[x][y];x+=dx[d],y+=dy[d])map[x][y]=1,++s;
    d=(d+2)%4;
    if(map[x][y]<0&&s>s0)dfs(x+=dx[d],y+=dy[d],(d+2)%4,s);
    else ans=max(ans,s),x+=dx[d],y+=dy[d];
    for(;x!=x0||y!=y0;x+=dx[d],y+=dy[d])map[x][y]=0;

    for(d=(d0+1)%4,x=x0+dx[d],y=y0+dy[d],s=s0;!map[x][y];x+=dx[d],y+=dy[d])map[x][y]=1,++s;
    d=(d+2)%4;
    if(map[x][y]<0&&s>s0)dfs(x+=dx[d],y+=dy[d],(d+2)%4,s);
    else ans=max(ans,s),x+=dx[d],y+=dy[d];
    for(;x!=x0||y!=y0;x+=dx[d],y+=dy[d])map[x][y]=0;
}
int main()
{
    freopen("snail.in","r",stdin);
    freopen("snail.out","w",stdout);
    while(~scanf("%d%d",&n,&m))
    {
        memset(map,-1,sizeof(map));
        for(i=1;i<=n;++i)
            for(j=1;j<=n;++j)
                map[i][j]=0;
        while(m--)
        {
            getchar();
            scanf("%c%d",&po,&i);
            map[i][po-'A'+1]=-1;
        }
        ans=0;
        map[1][1]=1;
        dfs(1,1,0,1);
        dfs(1,1,1,1);
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值