USACO 5.2 Snail Trails

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 +-------------+

———————————————————————————————————————题解
这道题深搜不会超时,连优化都不用加……
但是宽搜会爆空间,内心好荒凉啊…………
  1 /*
  2 ID: ivorysi
  3 LANG: C++
  4 PROG: snail
  5 */
  6 #include <iostream>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <queue>
 11 #include <set>
 12 #include <vector>
 13 #include <string.h>
 14 #include <cmath>
 15 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
 16 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
 17 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
 18 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
 19 #define inf 0x7fffffff
 20 #define ivorysi
 21 #define mo 97797977
 22 #define hash 974711
 23 #define base 47
 24 #define pss pair<string,string>
 25 #define MAXN 5000
 26 #define fi first
 27 #define se second
 28 #define pii pair<int,int>
 29 #define esp 1e-8
 30 typedef long long ll;
 31 using namespace std;
 32 int n,b;
 33 char a[10];
 34 int graph[130][130];
 35 int ans;
 36 bool g[130][130];
 37 void dfs(int x,int y,int step) {
 38     int t1,z;
 39     ans=max(ans,step);
 40     if(graph[x-1][y]!=1) {
 41         t1=x-1,z=step;
 42         while(graph[t1][y]!=1) {
 43             if(g[t1][y]) {ans=max(ans,z);goto fail1;}
 44             g[t1][y]=1;
 45             ++z;
 46             --t1;
 47         }
 48         dfs(t1+1,y,z);
 49         fail1://如果不符合仍要更新回来
 50         siji(i,t1+1,x-1) g[i][y]=0;
 51     }
 52     
 53     if(graph[x+1][y]!=1) {
 54         t1=x+1,z=step;
 55         while(graph[t1][y]!=1) {
 56             if(g[t1][y]) {ans=max(ans,z);goto fail2;}
 57             g[t1][y]=1;
 58             ++z;
 59             ++t1;
 60         }
 61         dfs(t1-1,y,z);
 62         fail2:
 63         siji(i,x+1,t1-1) g[i][y]=0;
 64     }
 65     
 66     if(graph[x][y-1]!=1) {
 67         t1=y-1,z=step;
 68         while(graph[x][t1]!=1) {
 69             if(g[x][t1]) {ans=max(ans,z);goto fail3;}
 70             g[x][t1]=1;
 71             ++z;
 72             --t1;
 73         }
 74         dfs(x,t1+1,z);
 75         fail3:
 76         siji(i,t1+1,y-1) g[x][i]=0;
 77     }
 78     
 79     if(graph[x][y+1]!=1) {
 80         t1=y+1,z=step;
 81         while(graph[x][t1]!=1) {
 82             if(g[x][t1]) {ans=max(ans,z);goto fail4;}
 83             g[x][t1]=1;
 84             ++z;
 85             ++t1;
 86         }
 87         dfs(x,t1-1,z);
 88         fail4:
 89         siji(i,y+1,t1-1) g[x][i]=0;
 90     }
 91 }
 92 void init() {
 93     scanf("%d%d",&n,&b);
 94     int c;
 95     siji(i,1,b) {
 96         scanf("%s",a);
 97         sscanf(a+1,"%d",&c);
 98         graph[c][a[0]-'A'+1]=1;
 99     }
100     siji(i,0,n+1) {graph[0][i]=1;graph[n+1][i]=1;}
101     siji(i,0,n+1) {graph[i][0]=1;graph[i][n+1]=1;}
102 }
103 void solve() {
104     init();
105     g[1][1]=1;
106     dfs(1,1,1);
107     printf("%d\n",ans);
108 } 
109 int main(int argc, char const *argv[])
110 {
111 #ifdef ivorysi
112     freopen("snail.in","r",stdin);
113     freopen("snail.out","w",stdout);
114 #else
115     freopen("f1.in","r",stdin);
116 #endif
117     solve();
118     return 0;
119 }

 

 
posted @ 2017-02-09 18:55  sigongzi  阅读(376)  评论(0编辑  收藏  举报