【原题】
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 H1 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 H1 S . . . . . # .2 | . . . # . . .3 | . . . +-----+4 | . . . | . . |5 +-------+ # . |6 # . . . . . . |7 +------------ |8 +-------------+

【译题】
描述

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

     A B C D E F G H1 S . . . . . # .2 . . . . # . . .3 . . . . . . . .4 . . . . . . . .5 . . . . . # . .6 # . . . . . . .7 . . . . . . . .8 . . . . . . . .

萨丽总是垂直(向上或者向下)或水平(向左或者向右)地走。她可以从出发地(总是记作 A1 )向下或者向右走。 一旦萨丽选定了一个方向,她就会一直走下去。如果她遇到棋盘边缘或者路障,她就停下来,并且转过 90 度。她不可能离开棋盘,或者走进路障当中。并且,萨丽从不跨过她已经经过的格子。当她再也不能走的时候,她就停止散步。
这里是上面的棋盘上的一次散步路线图示:

     A B C D E F G H1 S---------+ # .2 . . . . # | . .3 . . . . . | . .4 . . . . . +---+5 . . . . . # . |6 # . . . . . . |7 +-----------+ |8 +-------------+

萨丽向右走,再向下,向右,向下,然后向左,再向上,最后向右走。这时她遇到了一个她已经走过的格子,她就停下来了。但是,如果她在 F5 格遇到路障后选择另外一条路——向我们看来是左边的方向转弯,情况就不一样了。
你的任务是计算并输出,如果萨丽聪明地选择她的路线的话,她所能够经过的最多格子数。
[编辑]格式

PROGRAM NAME: snail
INPUT FORMAT
输入的第一行包括 N ——棋盘的大小,和 B ——路障的数量(1 <= B <= 200)。接下来的 B 行包含着路障的位置信息。下面的样例输入对应着上面的示例棋盘。下面的输出文件表示问题的解答。注意,当 N 〉26 时,输入文件就不能表示 Z 列以后的路障了。(ps:这句话的理解请参见题解 //from Error)
OUTPUT FORMAT
输出文件应该只由一行组成,即萨丽能够经过的最多格子数。
[编辑]SAMPLE INPUT (file snail.in)

8 4
E2
A6
G1
F5

[编辑]SAMPLE OUTPUT (file snail.out)

33
样例说明

     A B C D E F G H1 S . . . . . # .2 | . . . # . . .3 | . . . +-----+4 | . . . | . . |5 +-------+ # . |6 # . . . . . . |7 +------------ |8 +-------------+

思路

一道很好的宽搜题目。
首先,先处理输入,字母代表列(也可以是行),数字代表行(同样也可以是列),从(1,1)出发,直到搜到走不动为止,走不动代表周围有墙,或者路都走过了,就停止,输出最大路径。

有墙或越界时,可以向垂直方向走,即水平走变竖直走,或竖直走变水平走,开一个变量c记录塔上一步走的方向,进行bfs+回溯。

设向上为 0
向下为 1
向左为 2
向右为3
移动方向
a[1][1]=a[3][0]=1;
a[0][1]=a[2][0]=-1;
0代表水平方向的移动,1代表竖直方向的移动。
其他例如a[0][0]没有赋值代表此方向不动。

代码如下
(其实这题和八数码有点像)

/*设向上为 0向下为 1向左为 2向右为3
*/
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
bool map[125][125];
int a[4][2];
int sum,n,ss,x,t;
bool f[125][125];
bool jie(int x,int y)
{if(x>0&&x<=n&&y>0&&y<=n)return 1;else return 0;
}
void bfs(int xx,int yy,int c,int step)
{int x,y;if(f[xx][yy])return;f[xx][yy]=true;sum=max(step,sum);x=xx+a[c][0];y=yy+a[c][1];if(!jie(x,y)||map[x][y]){if(c==0||c==1){x=xx+a[2][0];y=yy+a[2][1];if(jie(x,y)&&!map[x][y])bfs(x,y,2,step+1);x=xx+a[3][0];y=yy+a[3][1];if(jie(x,y)&&!map[x][y])bfs(x,y,3,step+1);}else{x=xx+a[0][0];y=yy+a[0][1];if(jie(x,y)&&!map[x][y])bfs(x,y,0,step+1);x=xx+a[1][0];y=yy+a[1][1];if(jie(x,y)&&!map[x][y])bfs(x,y,1,step+1);}}else bfs(x,y,c,step+1);f[xx][yy]=false;
}
int main()
{char s;a[1][1]=a[3][0]=1;a[0][1]=a[2][0]=-1;cin>>n>>ss;for(int i=1;i<=ss;i++){cin>>s;cin>>x;t=s-64;map[x][t]=1;}bfs(1,1,1,1);memset(f,0,sizeof(f));bfs(1,1,3,1);cout<<sum<<endl;return 0;
}

Codevs 3100 蜗牛的旅行相关推荐

  1. bzoj 1050: [HAOI2006]旅行comf(codevs.cn 1001 舒适的路线) 快排+并查集乱搞

    没用的话:好像很久没发博客了,主要是懒太蒟找不到水题.我绝对没弃坑...^_^ 还用些话:本文为博主原创文章,若转载请注明原网址和作者. 进入正题: 先pa网址: bzoj :http://www.l ...

  2. codevs——1036 商务旅行

    1036 商务旅行  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果 题目描述 Description 某首都城市的商人要经常到各城镇 ...

  3. [CODEVS 1036]商务旅行

    [问题描述] 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇之间都有道路连接,任意两个城镇之间如果有 ...

  4. C++题解:蜗牛旅行

            目录 题目 题解 题目 1000ms 524288K 蜗牛在制定今天的旅游计划,有 nn 个景点可选,它已经把这些景点按照顺路游览的顺序排成一排了,每个地方有相应的景观,这里用一个整数 ...

  5. 【codevs 1450】小天昊的旅行2333333

    我滴妈这个题好神啊-- Q == Question A == LOI_a Q:我去这题怎么做 A:我看看-- A:数据范围? Q:200000-- A:这题能做? Q:QAQ A:我回去看看 (两分钟 ...

  6. codevs 1531 山峰

    codevs 1531 山峰 题目描述 Description Rocky山脉有n个山峰,一字排开,从西向东依次编号为1, 2, 3, --, n.每个山峰的高度都是不一样的.编号为i的山峰高度为hi ...

  7. 转载 程序员上帝视角解读“旅行青蛙”,你的呱真的在旅行嘛? (手机游戏)...

    程序员上帝视角解读"旅行青蛙",你的呱真的在旅行嘛? 2018-02-05 黄小秋 数据与算法之美 来源:知乎 作者:黄小秋 原文链接:https://www.zhihu.com/ ...

  8. Codevs 1021 玛丽卡

    Codevs 1021 玛丽卡 题目地址:http://codevs.cn/problem/1021/ 题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他 ...

  9. 上线10天iOS流水破亿,万字长文剖析《最强蜗牛》

    实力沙雕,尽力卖梗,这是上半年表现最无厘头的放置游戏<最强蜗牛>.2020年6月23日,这款由青瓷游戏自主研发的沙雕游戏上线首日登顶iOS免费榜榜首,打进苹果畅销榜前五.根据七麦数据显示, ...

  10. 《最强蜗牛》运营分析:这个奇葩放置游戏的乐趣在哪里?

    如果你关注了国服手游畅销榜的话,一定会注意到最近半个月来,有一款放置类游戏<最强蜗牛>风头甚劲,从6月23日登陆双平台后,下载量和收入都非常可观.以iOS国服为例,<最强蜗牛> ...

最新文章

  1. Serializable Parcelable
  2. spring mvc + freemarker 整合
  3. numpy拼接_巧用numpy切分图片
  4. 计算机一级b和小高考,2021年小高考B是几分相关内容
  5. 浪潮K1 Power通过ISO/IEC 20243标准认证
  6. zabbix server 迁移步骤
  7. Python PCA降维小例子
  8. greenplum配置高可用_GREENPLUM介绍之数据库管理(七)- 配置数据库高可用性之master镜像 | 学步园...
  9. 不随意付钱,不随意签字。
  10. (NeurIPS 2019) Gated CRF Loss -一种用于弱监督图像语义分割的新型损失函数
  11. arduino电风扇程序_【NO.7】智能风扇控制器-
  12. 献给2012——易水寒的心声
  13. SLA是什么意思 ?
  14. 从产品经理招聘信息分析现代产品经理职责
  15. 如何制作网页-初学者入门HTML+CSS
  16. a16z:私钥屡被攻破?Web3安全还得从钱包说起
  17. Ubuntu、Debian 系统安装 PHP 7.4 教程,超简单,一把梭!
  18. 谈谈反爬虫“政策与对策”
  19. Unity3D显示Kinect线条图
  20. Leetcode——121. Best Time to Buy and Sell Stock

热门文章

  1. SpringBoot 整合ActiveMQ
  2. 如何让工作更有活力?社科院与杜兰大学金融管理硕士项目帮你充电续航
  3. 记一次完整的npm包开发 --- 发布过程
  4. pycharm中TODO注释
  5. C语言刷题训练营-第一讲
  6. 用c++从头开始实现决策树
  7. 安全架构--5--SDL安全与企业办公安全落地实践
  8. 魔兽世界燃烧的远征服务器状态,暴雪战网读入经典TBC服务器,魔兽世界燃烧的远征即将起航...
  9. 云服务器防火墙开放端口访问--电信云服务器
  10. 2021/04/10 OJ每日一题 1190: 按出生日期排序(结构体专题)python