JZOJ1315.【USACO题库】5.2.1 Snail Trails蜗牛的旅行
来源:互联网 发布:快手视频解析软件 编辑:程序博客网 时间:2024/06/05 09:29
题目描述
萨丽·斯内尔(Sally Snail,蜗牛)喜欢在 N x N 的棋盘上闲逛(1 < n < 120)。她总是从棋盘的左上角出发。棋盘上有空的格子(用“.”来表示)和 B 个路障(用“#”来表示)。下面是这种表示法的示例棋盘:
A B C D E F G H 1 S . . . . . # . 2 . . . . # . . . 3 . . . . . . . . 4 . . . . . . . . 5 . . . . . # . . 6 # . . . . . . . 7 . . . . . . . . 8 . . . . . . . .
萨丽总是垂直(向上或者向下)或水平(向左或者向右)地走。她可以从出发地(总是记作 A1 )向下或者向右走。
一旦萨丽选定了一个方向,她就会一直走下去。如果她遇到棋盘边缘或者路障,她就停下来,并且转过 90 度。她不可能离开棋盘,或者走进路障当中。并且,萨丽从不跨过她已经经过的格子。当她再也不能走的时候,她就停止散步。
这里是上面的棋盘上的一次散步路线图示:
A B C D E F G H 1 S---------+ # . 2 . . . . # | . . 3 . . . . . | . . 4 . . . . . +---+ 5 . . . . . # . | 6 # . . . . . . | 7 +-----------+ | 8 +-------------+
萨丽向右走,再向下,向右,向下,然后向左,再向上,最后向右走。这时她遇到了一个她已经走过的格子,她就停下来了。但是,如果她在 F5 格遇到路障后选择另外一条路——向我们看来是左边的方向转弯,情况就不一样了。
你的任务是计算并输出,如果萨丽聪明地选择她的路线的话,她所能够经过的最多格子数。
PROGRAM NAME: snail
INPUT FORMAT
输入的第一行包括 N ——棋盘的大小,和 B ——路障的数量(1 <= B <= 200)。接下来的 B 行包含着路障的位置信息。下面的样例输入对应着上面的示例棋盘。下面的输出文件表示问题的解答。注意,当 N 〉26 时,输入文件就不能表示 Z 列以后的路障了。
SAMPLE INPUT (file snail.in) 8 4 E2 A6 G1 F5
OUTPUT FORMAT
输出文件应该只由一行组成,即萨丽能够经过的最多格子数。
SAMPLE OUTPUT (file snail.out)
33
输入
输出
样例输入
样例输出
数据范围限制
思路:
usaco第五部分水题是挺多的……
正解dfs
不用bfs的原因是空间会爆炸(这个自己想想,分叉会出现太多种情况了,数组装不下)
其实这道题还有个因吹斯听的东西:
注意,当 N 〉26 时,输入文件就不能表示 Z 列以后的路障了。
这个告诉我们路障最多只有26列。神奇……
然后,我们从[1,1]点开始dfs,每次更新答案,如果当前累加的tot>ans then ans:=tot
当下一格以前走过就return,是边缘或者障碍就改变方向往两边dfs(当然如果两边都走过也return)
然后……就没了,妥妥AC
注:从开始码到AC才花了我二十分钟……这……很无语……
代码:
const fx:array[0..3,1..2]of longint= ((0,1),(1,0),(0,-1),(-1,0));var a,bz:array[0..120,0..120]of boolean; n,m,i,j,x,y,ans:longint; s:string;procedure dfs(x,y,tot,way:longint);//way的值:0向左,1向下,2向右,3向上var i,j,k:longint;label lab;begin if tot>ans then ans:=tot; i:=x+fx[way,1]; j:=y+fx[way,2]; if (i>=1)and(i<=n)and(j>=1)and(j<=n) then begin if not bz[i,j]then goto lab; if not a[i,j]then exit; a[i,j]:=false; dfs(i,j,tot+1,way); a[i,j]:=true; exit; end else begin lab:; case way of 0,2:begin i:=x+1; j:=y; if (i>=1)and(i<=n)and(j>=1)and(j<=n) then begin if bz[i,j] and a[i,j] then begin a[i,j]:=false; dfs(i,j,tot+1,1); a[i,j]:=true; end; end; i:=x-1; j:=y; if (i>=1)and(i<=n)and(j>=1)and(j<=n)then begin if bz[i,j] and a[i,j] then begin a[i,j]:=false; dfs(i,j,tot+1,3); a[i,j]:=true; end; end; end; else begin i:=x; j:=y+1; if (i>=1)and(i<=n)and(j>=1)and(j<=n)then begin if bz[i,j] and a[i,j] then begin a[i,j]:=false; dfs(i,j,tot+1,0); a[i,j]:=true; end; end; i:=x; j:=y-1; if (i>=1)and(i<=n)and(j>=1)and(j<=n) then begin if bz[i,j] and a[i,j] then begin a[i,j]:=false; dfs(i,j,tot+1,2); a[i,j]:=true; end; end; end; end; end;end;begin //assign(input,'readin.pas');reset(input); readln(n,m); fillchar(bz,sizeof(bz),true); for i:=1 to m do begin readln(s); y:=ord(s[1])-64; val(copy(s,2,length(s)-1),x); bz[x,y]:=false; end; fillchar(a,sizeof(a),true); a[1,1]:=false; dfs(1,1,1,0); fillchar(a,sizeof(a),true); a[1,1]:=false; dfs(1,1,1,1); writeln(ans);end.
后注:
中间我为了省码字时间用了goto语句
然后整个code就变得奇奇怪怪了
- JZOJ1315.【USACO题库】5.2.1 Snail Trails蜗牛的旅行
- 【USACO题库】5.2.1 Snail Trails蜗牛的旅行
- 【USACO题库】5.2.1 Snail Trails蜗牛的旅行
- 【题目】[USACO5.2]蜗牛的旅行Snail Trails
- USACO 5.2 Snail Trails
- usaco training 5.2.1 Snail Trails 题解
- C++——【USACO 5.2.1】——Snail Trails
- usaco 5.2 Snail Trails(DFS)
- usaco snail trails(dfs)
- [DFS] 5.2.1 Snail Trails
- usaco 5.1.2 Snail Trails
- USACO Snail Trails 解题报告
- Section 5.2 Snail Trails
- Snail Trails
- 【USACO题库】2.4.3 Cow Tours牛的旅行
- 【USACO题库】2.4.3 Cow Tours牛的旅行
- USACO5.2.1 Snail Trails(snail)
- Snail—不是所有的牛都叫蜗牛
- 跟我开始从0学Android--写在前面
- Spring IOC容器基本原理
- SpringBoot系列(6)---SpringBoot-JPA
- HashMap和HashTable的区别
- R中的程序结构(for,while,repeat)
- JZOJ1315.【USACO题库】5.2.1 Snail Trails蜗牛的旅行
- 嵌入式每日学习心得2017.07.11
- 【codeforces 747B】Mammoth's Genome Decoding
- Java并发编程(九)《Executor框架》
- 一件数据库学习有趣的事情
- Java 基本数据类型
- python CV 趣味项目 答题卡识别
- VMware中Kali Linux添加源,更新和安装vmtools
- C++ demo:文本搜索以及'指针的引用'的思考