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. windows安装filebeat服务报错

    cmd进入filebeat目录下   用以下命令执行: PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-fil ...

  2. poj 1419 Graph Coloring

    http://poj.org/problem?id=1419 题意: 一张图黑白染色,相邻点不能都染黑色,最多能染几个黑色点 最大点独立集 但是图不能同构为二分图,不能用二分图匹配来做 那就爆搜吧 还 ...

  3. Java使用UDP发送数据到InfluxDB

    最近在做压测引擎相关的开发,需要将聚合数据发送到InfluxDB保存以便实时分析和控制QPS. 下面介绍对InfluxDB的使用. 什么是InfluxDB InfluxDB是一款用Go语言编写的开源分 ...

  4. 【转】Graphics.DrawImage 方法 IntPtr 结构 GDI 句柄 知识收集

    Graphics.DrawImage 方法 在指定的位置使用原始物理大小绘制指定的 Image. 命名空间:System.Drawing 程序集:System.Drawing(在 system.dra ...

  5. tmux终端工具

    本文原始地址:http://www.cnblogs.com/chinas/p/7094172.html,转载请注明出处,谢谢!!! 1.介绍 tmux(终端复用工具):一个很有趣的工具,类似GNU S ...

  6. POJ 2449 Remmarguts' Date (K短路 A*算法)

    题目链接 Description "Good man never makes girls wait or breaks an appointment!" said the mand ...

  7. linux查看及设置别名,权限,生成ssh秘钥

    1.alias :查看系统中所有的命令别名 2.设定别名 alias 别名='原命令' 3.删除别名 unalias 别名 4.使别名永久生效    vi  ~/.bashrc  写入这个文件中即可永 ...

  8. npm_一个有意思的npm包

    $ npm install yosay const yosay = require('yosay'); console.log(yosay('Hello, and welcome to my fant ...

  9. 弹性盒模型justify-content属性

    justify-content是应用于父容器上来规定子元素在水平方向上的对齐方式的. flex-start 左对齐 flex-end 右对齐 center 居中 space-betten 两端对齐,两 ...

  10. c# List 所有操作方法例子

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Ref ...