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. 转:iPhone上关于相机拍照的图片的imageOrientation的问题

    用相机拍摄出来的照片含有EXIF信息,UIImage的imageOrientation属性指的就是EXIF中的orientation信息.如果我们忽略orientation信息,而直接对照片进行像素处 ...

  2. python操作mongo脚本

    #!/usr/bin/python# -*- coding: utf-8 -*- import sysimport osimport jsonfrom pymongo import MongoClie ...

  3. hdu1286 找新朋友

    找新朋友 http://acm.hdu.edu.cn/showproblem.php?pid=1286 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  4. soj1036. Crypto Columns

    1036. Crypto Columns Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description The columnar en ...

  5. 说说JavaScript中的事件模型

    1.javascript中为元素添加事件处理程序的方法有以下几种方式,可以为javascript元素添加事件处理程序 (1) 直接将事件处理代码写在html中(2) 定义一个函数,赋值给html元素的 ...

  6. 小程序web-view wx.miniProgram.postMessage 坑记录

    web-view吧,其实微信官方应该是非常不支持在小程序上嵌套web的,它希望你直接用小程序上的代码,而放弃web的实现,当然,也是为了防止用小程序去嵌套别的广告页面.所以官方对web-view的操作 ...

  7. java后台调用http请求

    1:代码   @Value("${sms.username}")  可以将sms.properties配置文件中的值注入到username //这种方式是将sms.properti ...

  8. Python中的and和or

    引子: 出现以上情况的原因是什么呢? print(bool('')) # False print(bool(0)) # False 所有变量的位操作都是通过强制转换成bool实现的,并且表达式的值是从 ...

  9. gradle 构建工具,与Ant Maven关系

    1   基本概念 gradle是一个基于Apache ant 和apache maven概念的项目自动化建构工具.它使用一种基于Groovy的特定领域语言来声明项目设置,而不是传统的xml.当前其支持 ...

  10. MODULE_DEVICE_TABLE【转】

    转自:http://blog.csdn.net/tangkegagalikaiwu/article/details/8444249 This pci_device_id structure needs ...