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 +-------------+ ———————————————————————————————————————题解
这道题深搜不会超时,连优化都不用加……
但是宽搜会爆空间,内心好荒凉啊…………
/*
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的更多相关文章
- 洛谷——P1560 [USACO5.2]蜗牛的旅行Snail Trails
P1560 [USACO5.2]蜗牛的旅行Snail Trails 题目描述 萨丽·斯内尔(Sally Snail,蜗牛)喜欢在N x N 的棋盘上闲逛(1 < n <= 120). 她总 ...
- 洛谷 P1560 [USACO5.2]蜗牛的旅行Snail Trails(不明原因的scanf错误)
P1560 [USACO5.2]蜗牛的旅行Snail Trails 题目描述 萨丽·斯内尔(Sally Snail,蜗牛)喜欢在N x N 的棋盘上闲逛(1 < n <= 120). 她总 ...
- [题解]USACO 5.2.1 Snail Trails
链接:http://cerberus.delos.com:791/usacoprob2?S=snail&a=uzElkgTaI9d 描述:有障碍的棋盘上的搜索,求从左上角出发最多经过多少个格子 ...
- [USACO5.2]蜗牛的旅行Snail Trails(有条件的dfs)
题目描述 萨丽·斯内尔(Sally Snail,蜗牛)喜欢在N x N 的棋盘上闲逛(1 < n <= 120). 她总是从棋盘的左上角出发.棋盘上有空的格子(用“.”来表示)和B 个路障 ...
- [USACO5.2]Snail Trails
嘟嘟嘟 一道很水的爆搜题,然后我调了近40分钟…… 错误:输入数据最好用cin,因为数字可能不止一位,所以用scanf后,单纯的c[0]为字母,c[1]数字………………………… #include< ...
- 洛谷 P1560 [USACO5.2]蜗牛的旅行Snail Trails
题目链接 题解 一看题没什么思路.写了个暴力居然可过?! Code #include<bits/stdc++.h> #define LL long long #define RG regi ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- [JLOI 2011]飞行路线&[USACO 09FEB]Revamping Trails
Description Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司.该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并 ...
- 道路翻新 (Revamping Trails, USACO 2009 Feb)
题意:给定m<=50000的1-n有联通的图,求最多可以使K<=20条边变为0的情况下的最短路是多少.. 思路:简单的分层图最短路,对于每个点拆成K个点.. 然后求一边最短路.. code ...
随机推荐
- Java基础-Collection子接口之Set接口
Java基础-Collection子接口之Set接口 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 学习Collection接口时,记得Collection中可以存放重复元素,也可 ...
- Bittersweet——NOIP2018 游记
p { font-size: 16px; line-height: 1.5em; } blockquote { font-family: 'Times New Roman', 楷体; text-ali ...
- python核心编程笔记——Chapter2
对于.py文件,任何一个空的式子都不会有什么输出,如下: #!/usr/bin/env python #-*-coding=utf-8-*- #无任何效果,空语句 1 + 2 * 4 对于i++,++ ...
- NameValuePair方式传参数
今天工作中联调外部的一个接口用post方式传输,我按照文档封装参数成Jason字符串传入,但是对方一直接受参数为空,折腾了半天也没找到问题.很苦恼,检查代码都没有错误,可是为什么对方接受参数为空呢?然 ...
- HDU 1074 Doing Homework (dp+状态压缩)
题目链接 Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot ...
- Python练习-生成器-一个生成器被坑的体无完肤
代码如下,尽可能独立阅读: # 编辑者:闫龙 from urllib.request import urlopen #导入一个包,这就是egon留的一个坑 def get(url):#这是为了保证题目 ...
- 【译】第五篇 Replication:事务复制-How it works
本篇文章是SQL Server Replication系列的第五篇,详细内容请参考原文. 这一系列包含SQL Server事务复制和合并复制的详细内容,从理解基本术语和设置复制的方法,到描述它是如何工 ...
- 关于UNIX的exec函数
在UNIX系统中,系统为进程相关提供了一系列的控制原语,包括:进程fork,进程exit,进程exec,进程wait等服务. 该篇文章主要与进程exec服务有关,并记录了几个需要注意留意的点. 照例给 ...
- SqlServer存储过程(增删改查)
* IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值. CREATE PROCEDURE [dbo].[PR_NewsAffiche_AddNewsEntity] ( ...
- python3之pymysql模块
1.python3 MySQL数据库链接模块 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PyMySQL 遵循 Pyt ...