[题解]USACO 5.2.1 Snail Trails
链接:http://cerberus.delos.com:791/usacoprob2?S=snail&a=uzElkgTaI9d
描述:有障碍的棋盘上的搜索,求从左上角出发最多经过多少个格子。
思路:暴搜
我的实现:
1 /*
2 ID:zhangyi20
3 PROG:snail
4 LANG:C++
5 */
6 #include <iostream>
7 #include <cstdio>
8 #include <cmath>
9 using namespace std;
10 #define MaxN 120
11 int N,B;
12 int mat[MaxN+5][MaxN+5];//0空地 1障碍 2走过
13 int Dfs(int i,int j,int Dir)//方向:0向上,1向右,2向下,3向左
14 {
15 mat[i][j]=2;
16 int Ret=0,i_=i,j_=j;
17 int tmp1=-1,tmp2=-1;
18 if(Dir==0)//向上
19 {
20 for(i=i-1; ;--i,++Ret)
21 {
22 if(mat[i][j])
23 break;
24 mat[i][j]=2;
25 }
26 if(mat[i][j]==1)//转向
27 {
28 if(mat[i+1][j-1])
29 tmp1=0;
30 else
31 tmp1=Dfs(i+1,j,3);
32 if(mat[i+1][j+1])
33 tmp2=0;
34 else
35 tmp2=Dfs(i+1,j,1);
36 Ret+=max(tmp1,tmp2);
37 }
38 for(i=i+1;i<=i_;++i)
39 mat[i][j]=0;
40 }
41 else if(Dir==1)//向右
42 {
43 for(j=j+1; ;++j,++Ret)
44 {
45 if(mat[i][j])
46 break;
47 mat[i][j]=2;
48 }
49 if(mat[i][j]==1)//转向
50 {
51 if(mat[i-1][j-1])
52 tmp1=0;
53 else
54 tmp1=Dfs(i,j-1,0);
55 if(mat[i+1][j-1])
56 tmp2=0;
57 else
58 tmp2=Dfs(i,j-1,2);
59 Ret+=max(tmp1,tmp2);
60 }
61 for(j=j-1;j>=j_;--j)
62 mat[i][j]=0;
63 }
64 else if(Dir==2)//向下
65 {
66 for(i=i+1; ;++i,++Ret)
67 {
68 if(mat[i][j])
69 break;
70 mat[i][j]=2;
71 }
72 if(mat[i][j]==1)//转向
73 {
74 if(mat[i-1][j-1])
75 tmp1=0;
76 else
77 tmp1=Dfs(i-1,j,3);
78 if(mat[i-1][j+1])
79 tmp2=0;
80 else
81 tmp2=Dfs(i-1,j,1);
82 Ret+=max(tmp1,tmp2);
83 }
84 for(i=i-1;i>=i_;--i)
85 mat[i][j]=0;
86 }
87 else//向左
88 {
89 for(j=j-1; ;--j,++Ret)
90 {
91 if(mat[i][j])
92 break;
93 mat[i][j]=2;
94 }
95 if(mat[i][j]==1)//转向
96 {
97 if(mat[i-1][j+1])
98 tmp1=0;
99 else
100 tmp1=Dfs(i,j+1,0);
101 if(mat[i+1][j+1])
102 tmp2=0;
103 else
104 tmp2=Dfs(i,j+1,2);
105 Ret+=max(tmp1,tmp2);
106 }
107 for(j=j+1;j<=j_;++j)
108 mat[i][j]=0;
109 }
110 return Ret;
111 }
112 int main()
113 {
114 freopen("snail.in","r",stdin);
115 freopen("snail.out","w",stdout);
116 scanf("%d%d",&N,&B);
117 int i,j;
118 char c;
119 for(i=1;i<=B;++i)
120 {
121 do
122 {
123 scanf("%c",&c);
124 }while(c<'A'||c>'Z');
125 scanf("%d",&j);
126 mat[j][c-'A'+1]=1;
127 }
128 for(i=1;i<=N;++i)
129 mat[0][i]=mat[N+1][i]=mat[i][0]=mat[i][N+1]=1;//给地图四周打上障碍
130 mat[1][1]=2;
131 printf("%d\n",max(Dfs(1,1,1),Dfs(1,1,2))+1);
132 return 0;
133 }
[题解]USACO 5.2.1 Snail Trails的更多相关文章
- USACO 5.2 Snail Trails
Snail TrailsAll Ireland Contest Sally Snail likes to stroll on a N x N square grid (1 <n <= 12 ...
- 洛谷——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 1.3 Ski Course Design
Ski Course Design Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer ...
- [题解]USACO 1.3 Wormholes
Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfire ...
- 洛谷 P1560 [USACO5.2]蜗牛的旅行Snail Trails
题目链接 题解 一看题没什么思路.写了个暴力居然可过?! Code #include<bits/stdc++.h> #define LL long long #define RG regi ...
- [USACO5.2]蜗牛的旅行Snail Trails(有条件的dfs)
题目描述 萨丽·斯内尔(Sally Snail,蜗牛)喜欢在N x N 的棋盘上闲逛(1 < n <= 120). 她总是从棋盘的左上角出发.棋盘上有空的格子(用“.”来表示)和B 个路障 ...
- 题解 [USACO Mar08] 奶牛跑步
[USACO Mar08] 奶牛跑步 Description Bessie准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她只准备沿着下坡的路跑到池塘,然后走回牛棚. Bessie也不想跑得太远,所 ...
- [USACO5.2]Snail Trails
嘟嘟嘟 一道很水的爆搜题,然后我调了近40分钟…… 错误:输入数据最好用cin,因为数字可能不止一位,所以用scanf后,单纯的c[0]为字母,c[1]数字………………………… #include< ...
随机推荐
- 【机器学习】GMM和EM算法
机器学习算法-GMM和EM算法 目录 机器学习算法-GMM和EM算法 1. GMM模型 2. GMM模型参数求解 2.1 参数的求解 2.2 参数和的求解 3. GMM算法的实现 3.1 gmm类的定 ...
- HttpServletRequest类介绍
HttpServletRequest类介绍 1,HttpServletRequest类作用: 每次只要有请求进入Tomcat服务器,Tomcat服务器就会把请求过来的HTTP协议信息解析好封装到Req ...
- Windows如何搭建SSL通信(非Web)
自己研究了会儿,把结论发出来给有需要的人 第一步:准备环境 首先需要一台服务器(这不是废话吗),我这边用的windows2003, 还需要一台客户端,我用的是windwos2008 第二步:服务器环境 ...
- Method com/mchange/v2/c3p0/impl/NewProxyResultSet.isClosed()Z is abstract
HTTP Status 500 - Handler dispatch failed; nested exception is java.lang.AbstractMethodError: Method ...
- Nginx命令(全局配置文件与模块)
目录 一:Nginx命令 二:Nginx全局配置文件 1.nginx全局配置 2.过滤出Nginx 三:Nginx网址模块(解析) 一:Nginx命令 1.-v : 打印版本号 [root@web01 ...
- 使用Hot Chocolate和.NET 6构建GraphQL应用(3) —— 实现Query基础功能
系列导航 使用Hot Chocolate和.NET 6构建GraphQL应用文章索引 需求 在本文中,我们通过一个简单的例子来看一下如何实现一个最简单的GraphQL的接口. 实现 引入Hot Cho ...
- 二进制方式搭建Kubernetes高可用集群
转:https://jiangxl.blog.csdn.net/article/details/120428703 详细:https://developer.aliyun.com/article/78 ...
- MySQL专题1: 字段和索引
合集目录 MySQL专题1: 字段和索引 Float.Decimal 存储金额的区别? MySQL中存在 float, double 等非标准数据类型, 也有 decimal 这种标准数据类型 其区别 ...
- Sweetalert模态对话框与Swiper轮播插件、Bootstrap样式组件、AdminLTE后台管理模板地址
Sweetalert纯JS模态对话框插件地址:http://mishengqiang.com/sweetalert/ AdminLTE后台管理模板系统地址(基于Bootstrap):https://a ...
- halcon视觉入门钢珠识别
halcon视觉入门钢珠识别 经过入门篇,我们有了基础的视觉识别知识.现在加以应用. 有如下图片: 我们需要识别图片中比较明亮的中间区域,有黑色的钢珠,我们需要知道他的位置和面积. 分析如何识别 编写 ...