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 ...
随机推荐
- linux下yum错误:[Errno 14] problem making ssl connection Trying other mirror.
今天是要yum命令安装EPEL仓库后 yum install epel-release 突然发现yum安装其他的软件出错. 错误:[Errno 14] problem making ssl conne ...
- linux diff 命令
diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...
- HTTP协议(1)-------- 网络编程
1. HTTP简介 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传送协议.它可以使浏览器更加高效,使网络传输减少. ...
- CentOS安装Confluence Wiki步骤
参考:http://supernetwork.blog.51cto.com/2304163/1187066 参考:http://yjiang.tk/?p=1085 需要的文件 CentOS-6.5 ...
- 数据库(二)之SQL Server
什么是SQL SERVER? Microsoft发布的SQL Server产品是一个典型的关系型数据库管理系统. 功能强大 操作简便 可靠的安全性 SQL Server 2008体系结构 S ...
- c++程序设计中的函数重载
函数重载的意思是在一个作用域内(命名空间内)定义了某个或某些具有相同名称的函数,但是他们的参数列表和定义(实现)不相同,如果相同的话,就没啥意义了.当调用一个重载函数时,编译器会通过所使用的参数类型. ...
- Linux日志文件/var/log详解
更多内容推荐微信公众号,欢迎关注: 如果愿意在Linux环境方面花费些时间,首先就应该知道日志文件的所在位置以及它们包含的内容.在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时 ...
- tensorflow环境安装
tensorflow环境安装1.安装虚拟机Virtrualbox下载地址:https://www.virtualbox.org/wiki/Downloads 2.下载安装Ubuntu镜像下载地址:ht ...
- linux 进程内存解析【转】
转自:http://blog.csdn.net/lile269/article/details/6460807 之前我所了解的linux下进程的地址空间的布局的知识,是从APUE第2版的P430得来的 ...
- 手动实现图片预览-放大缩小全屏支持IE9以上
#{extends '/Index/index.html' /} #{set title:'意见反馈' /} <script src="/public/mgr/javascripts/ ...