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 +-------------+ ———————————————————————————————————————题解
这道题深搜不会超时,连优化都不用加……
但是宽搜会爆空间,内心好荒凉啊…………
 /*
ID: ivorysi
LANG: C++
PROG: snail
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#include <cmath>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x7fffffff
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
int n,b;
char a[];
int graph[][];
int ans;
bool g[][];
void dfs(int x,int y,int step) {
int t1,z;
ans=max(ans,step);
if(graph[x-][y]!=) {
t1=x-,z=step;
while(graph[t1][y]!=) {
if(g[t1][y]) {ans=max(ans,z);goto fail1;}
g[t1][y]=;
++z;
--t1;
}
dfs(t1+,y,z);
fail1://如果不符合仍要更新回来
siji(i,t1+,x-) g[i][y]=;
} if(graph[x+][y]!=) {
t1=x+,z=step;
while(graph[t1][y]!=) {
if(g[t1][y]) {ans=max(ans,z);goto fail2;}
g[t1][y]=;
++z;
++t1;
}
dfs(t1-,y,z);
fail2:
siji(i,x+,t1-) g[i][y]=;
} if(graph[x][y-]!=) {
t1=y-,z=step;
while(graph[x][t1]!=) {
if(g[x][t1]) {ans=max(ans,z);goto fail3;}
g[x][t1]=;
++z;
--t1;
}
dfs(x,t1+,z);
fail3:
siji(i,t1+,y-) g[x][i]=;
} if(graph[x][y+]!=) {
t1=y+,z=step;
while(graph[x][t1]!=) {
if(g[x][t1]) {ans=max(ans,z);goto fail4;}
g[x][t1]=;
++z;
++t1;
}
dfs(x,t1-,z);
fail4:
siji(i,y+,t1-) g[x][i]=;
}
}
void init() {
scanf("%d%d",&n,&b);
int c;
siji(i,,b) {
scanf("%s",a);
sscanf(a+,"%d",&c);
graph[c][a[]-'A'+]=;
}
siji(i,,n+) {graph[][i]=;graph[n+][i]=;}
siji(i,,n+) {graph[i][]=;graph[i][n+]=;}
}
void solve() {
init();
g[][]=;
dfs(,,);
printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("snail.in","r",stdin);
freopen("snail.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
 

USACO 5.2 Snail Trails的更多相关文章

  1. 洛谷——P1560 [USACO5.2]蜗牛的旅行Snail Trails

    P1560 [USACO5.2]蜗牛的旅行Snail Trails 题目描述 萨丽·斯内尔(Sally Snail,蜗牛)喜欢在N x N 的棋盘上闲逛(1 < n <= 120). 她总 ...

  2. 洛谷 P1560 [USACO5.2]蜗牛的旅行Snail Trails(不明原因的scanf错误)

    P1560 [USACO5.2]蜗牛的旅行Snail Trails 题目描述 萨丽·斯内尔(Sally Snail,蜗牛)喜欢在N x N 的棋盘上闲逛(1 < n <= 120). 她总 ...

  3. [题解]USACO 5.2.1 Snail Trails

    链接:http://cerberus.delos.com:791/usacoprob2?S=snail&a=uzElkgTaI9d 描述:有障碍的棋盘上的搜索,求从左上角出发最多经过多少个格子 ...

  4. [USACO5.2]蜗牛的旅行Snail Trails(有条件的dfs)

    题目描述 萨丽·斯内尔(Sally Snail,蜗牛)喜欢在N x N 的棋盘上闲逛(1 < n <= 120). 她总是从棋盘的左上角出发.棋盘上有空的格子(用“.”来表示)和B 个路障 ...

  5. [USACO5.2]Snail Trails

    嘟嘟嘟 一道很水的爆搜题,然后我调了近40分钟…… 错误:输入数据最好用cin,因为数字可能不止一位,所以用scanf后,单纯的c[0]为字母,c[1]数字………………………… #include< ...

  6. 洛谷 P1560 [USACO5.2]蜗牛的旅行Snail Trails

    题目链接 题解 一看题没什么思路.写了个暴力居然可过?! Code #include<bits/stdc++.h> #define LL long long #define RG regi ...

  7. USACO 完结的一些感想

    其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...

  8. [JLOI 2011]飞行路线&[USACO 09FEB]Revamping Trails

    Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并 ...

  9. 道路翻新 (Revamping Trails, USACO 2009 Feb)

    题意:给定m<=50000的1-n有联通的图,求最多可以使K<=20条边变为0的情况下的最短路是多少.. 思路:简单的分层图最短路,对于每个点拆成K个点.. 然后求一边最短路.. code ...

随机推荐

  1. Linux 下搭建 Svn+Apache

    一.安装apache 1.检查apache是否安装 rpm -qa|grep httpd 2.使用yum安装apache yum -y install httpd 3.记住安装的版本号 httpd.x ...

  2. day20 GUI(Graphics User Interface)

    顶层容器:JWindow.JFrame.JDialge.JAsplet JFrame,默认布局是边界布局 JFrame的内容面板是:Container. 面板容器:JPanel,默认布局是流布局. 布 ...

  3. linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime

    time()提供了秒级的精确度 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在的秒 ...

  4. 2017 清北济南考前刷题Day 2 morning

    期望得分:100+30+60=190 实际得分:100+30+30=160 T1 最优方案跳的高度一定是单调的 所以先按高度排序 dp[i][j] 跳了i次跳到j 枚举从哪儿跳到j转移即可 #incl ...

  5. 微信 js-sdk

    使用方法 http://mp.weixin.qq.com/wiki/11/74ad127cc054f6b80759c40f77ec03db.html Demo http://203.195.235.7 ...

  6. 网络编程之python zeromq学习系列之一

    简介: zeromq中间件,他是一个轻量级的消息中间件,传说是世界上最快的消息中间件,为什么这么说呢? 因为一般的消息中间件都需要启动消息服务器,但是zeromq这厮尽然没有消息服务器,他压根没有消息 ...

  7. 实现asp.net的文件压缩、解压、下载

    很早前就想做文件的解压.压缩.下载 了,不过一直没时间,现在项目做完了,今天弄了下.不过解压,压缩的方法还是看的网上的,嘻嘻~~不过我把它们综合了一下哦.呵呵~~ 1.先要从网上下载一个icsharp ...

  8. 【方法】纯jQuery实现星巴克官网导航栏效果

    前言 大冬天的没得玩,只能和代码玩. 所以就无聊研究了一下星巴克官网,在我看来应该是基本还原吧~ 请各位大神指教! 官网效果图 要写的就是最上方的会闪现的白色条条 效果分析 1.在滚动条往下拉到一定距 ...

  9. JS设计模式——11.适配器模式

    适配器模式概述 适配器模式可用来在现有接口和不兼容的类之间进行适配.使用这种模式的对象又叫包装器(wrapper). 适配器特点 从表面看,适配器模式很像门面模式.她们都要对别的对象进行包装并改变其呈 ...

  10. 20155303 2016-2017-2 《Java程序设计》第九周学习总结

    20155303 2016-2017-2 <Java程序设计>第九周学习总结 目录 学习内容总结(Linux命令) 教材学习中的问题和解决过程 代码调试中的问题和解决过程 代码托管 上周考 ...