- 浏览: 37743 次
- 性别:
- 来自: 北京
最新评论
-
andyshar:
请问如何在现有的hadoop环境中安装?
Hadoop集群监控系统Ambari安装 -
qingtangpaomian:
失败123 写道您好楼主: 我装好之后为啥老是最后一 ...
Hadoop集群监控系统Ambari安装 -
失败123:
您好楼主: 我装好之后为啥老是最后一步Cluster ...
Hadoop集群监控系统Ambari安装
USACO - 2.4.2 - Overfencing
原创文章转载请注明出处
摘要:动态规划
一. 题目翻译
1. 描述:
农夫John在外面的田野上搭建了一个巨大的用栅栏围成的迷宫。幸运的是,他在迷宫的边界上留出了两段栅栏作为迷宫的出口。更幸运的是,他所建造的迷宫是一个“完美的”迷宫:即你能从迷宫中的任意一点找到一条走出迷宫的路。给定迷宫的宽度W(1<=W<=38)及高度H(1<=H<=100)。 2*H+1行,每行2*W+1的字符以下面给出的格式表示一个迷宫。然后计算从迷宫中最“糟糕”的那一个点走出迷宫所需的步数(就是从最“糟糕”的一点,走出迷宫的最少步数)。(即使从这一点以最优的方式走向最靠近的出口,它仍然需要最多的步数)当然了,牛们只会水平或垂直地在X或Y轴上移动,他们从来不走对角线。每移动到一个新的方格算作一步(包括移出迷宫的那一步)这是一个W=5,H=3的迷宫:
如上图的例子,栅栏的柱子只出现在奇数行或奇数列。每个迷宫只有两个出口。
2. 格式:
INPUT FORMAT:
(file maze1.in)
第一行: W和H(用空格隔开)
第二行至第2 * H + 1行: 每行2 * W + 1个字符表示迷宫
OUTPUT FORMAT:
(file maze1.out)
输出一个单独的整数,表示能保证牛从迷宫中任意一点走出迷宫的最小步数。
SAMPLE INPUT:
5 3
+-+-+-+-+-+
| |
+-+ +-+ + +
| | | |
+ +-+-+ + +
| | |
+-+ +-+-+-+
SAMPLE OUTPUT:
9
二. 题解
1. 题意理解(将问题分析清楚,大致用什么思路):
我们首先从输入输出中查找出迷宫的两个入口,然后以这两个入口为起点做floodfill为每个点产生同入口之间距离。然后遍历整个迷宫找出距离最远的输出即可。
2. 具体实现(具体实现过程中出现的问题):
floodfill图论当中的基本问题,注意积累一下。
三. 代码
/* ID:fightin1 LANG:JAVA TASK:maze1 */ package session_2_4_2; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; import java.util.Scanner; public class maze1 { public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); // Scanner in = new Scanner(new BufferedReader(new FileReader( // "maze1.in"))); // PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter( // "maze1.out"))); int width = in.nextInt(); int height = in.nextInt(); in.nextLine(); char[][] maze = new char[2 * height + 1][2 * width + 1]; int[][] cnt = new int[2 * height + 1][2 * width + 1]; int x1 = 0, y1 = 0; int x2 = 0, y2 = 0; for (int i = 0; i < 2 * height + 1; i++) { String line = in.nextLine(); for (int j = 0; j < 2 * width + 1; j++) { char temp = line.charAt(j); maze[i][j] = temp; cnt[i][j] = Integer.MAX_VALUE; if (temp == ' ') { int tempx = 0; int tempy = 0; if (i == 0) { tempx = i + 1; tempy = j; } if (i == 2 * height) { tempx = i - 1; tempy = j; } if (j == 0) { tempx = i; tempy = j + 1; } if (j == 2 * width) { tempx = i; tempy = j - 1; } if (tempx!=0||tempy!=0){ if (x1 == 0 && y1 == 0) { x1 = tempx; y1 = tempy; } else { x2 = tempx; y2 = tempy; } } } } } cnt[x1][y1] = 1; flood(x1, y1, cnt, maze, width, height); cnt[x2][y2] = 1; flood(x2, y2, cnt, maze, width, height); int max = 0; for (int i = 1; i < 2 * height + 1; i += 2) { for (int j = 1; j < 2 * width + 1; j += 2) { int temp = cnt[i][j]; if (cnt[i][j] != Integer.MAX_VALUE) { if (max < cnt[i][j]) { max = cnt[i][j]; } } } } pw.println(max); pw.close(); } public static void flood(int x, int y, int[][] cnt, char[][] maze, int width, int height) { if (cnt[x][y] != 0) { if (x + 2 < 2 * height + 1 && maze[x + 1][y] == ' ') { if (cnt[x + 2][y] > cnt[x][y] + 1) { cnt[x + 2][y] = cnt[x][y] + 1; flood(x + 2, y, cnt, maze, width, height); } } if (x - 2 >= 0 && maze[x - 1][y] == ' ') { if (cnt[x - 2][y] > cnt[x][y] + 1) { cnt[x - 2][y] = cnt[x][y] + 1; flood(x - 2, y, cnt, maze, width, height); } } if (y + 2 < 2 * width + 1 && maze[x][y+1] == ' ') { if (cnt[x][y + 2] > cnt[x][y] + 1) { cnt[x][y + 2] = cnt[x][y] + 1; flood(x, y + 2, cnt, maze, width, height); } } if (y - 2 >= 0 && maze[x][y-1] == ' ') { if (cnt[x][y - 2] > cnt[x][y] + 1) { cnt[x][y - 2] = cnt[x][y] + 1; flood(x, y - 2, cnt, maze, width, height); } } } } }
发表评论
-
USACO - 3.2.2 - Stringsobits
2012-08-23 16:02 810原创文章转载请注明 ... -
USACO - 3.2.1 - Factorials
2012-08-23 16:01 702原创文章转载请注明出处 摘要:动态规划 ... -
USACO - 3.1.6 - Stamps
2012-08-23 16:01 1038原创文章转载请注明 ... -
USACO - 3.1.5 - Contact
2012-08-23 16:01 911原创文章转载请注明出处 摘要:二叉树的应用 , ... -
USACO - 3.1.3 - Humble Numbers
2012-08-23 16:00 715原创文章转载请注明 ... -
USACO - 3.1.2 - Score Inflation
2012-08-22 10:05 907原创文章转载请注明出处 摘要:动态规划 ... -
USACO - 3.1.1 - Agri-Net
2012-08-22 10:04 857原创文章转载请注明出处 摘要:Prim算法 , ... -
USACO - 2.4.5 - Fractions to Decimals
2012-08-22 10:04 962原创文章转载请注明出处 摘要:模拟 , 数论 ... -
USACO - 2.4.4 - Bessie Come Home
2012-08-22 10:04 907原创文章转载请注明出处 摘要:Dijkstra ... -
USACO - 2.4.1 - The Tamworth Two
2012-08-21 10:37 719原创文章转载请注明出处 摘要:模拟 ... -
USACO - 2.3.5 - Controlling Companies
2012-08-21 10:37 1304原创文章转载请注明出处 摘要:BFS , 模拟 ... -
USACO - 2.3.4 - Money Systems
2012-08-21 10:37 869原创文章转载请注明 ... -
USACO - 2.3.3 - Zero Sum
2012-08-21 10:36 746原创文章转载请注明出处 摘要:dfs , 枚举 ... -
USACO - 2.3.2 - Cow Pedigrees
2012-08-21 10:36 1010原创文章转载请注明 ... -
USACO - 2.3.1 - Longest Prefix
2012-08-20 20:31 1041原创文章转载请注明 ... -
USACO - 2.2.4 - Party Lamps
2012-08-20 20:30 1209原创文章转载请注明出处 摘要:枚举,三星 ... -
USACO - 2.2.3 - Runaround Numbers
2012-08-20 20:30 659原创文章转载请注明 ... -
USACO - 2.2.2 - Subset Sums
2012-08-20 20:30 695原创文章转载请注明出处 摘要:动态规划 ,0- ... -
USACO - 2.2.1 - Preface Numbering
2012-08-20 20:29 891原创文章转载请注明出处 摘要:模拟 , 数学分析 ... -
USACO - 2.1.5 - Hamming Codes
2012-08-18 19:22 782原创文章转载请注明出处 摘要:枚举、暴力 ...
相关推荐
USACO题目,Greedy Gift Givers
此c++代码实现了USACO上Bessie Come Home的问题,并运用了弗洛伊德算法
此C++程序是实现了USACO网站上的Magic Squares的问题。
该题来自USACO,为最长串的查找,此处方法很笨,有更好方法
USACO chapter one.May hope it useful to someone
USACO chapter two.Useful for beginners.
usaco 上的题目barn1,beads,calfflac,可到那里查看具体题目
Notes-USACO-2021-弹簧
USACO-Cpp
C-Usaco-Work:Usaco在C中的工作
USACO-实践USACO 培训网站的工作实践代码! 100% 工作 - 大部分优化 - 混合语言
这是USACO2001-2007月赛全集。 usaco是美国中学生的官方竞赛网站。是美国著名在线题库,专门为信息学竞赛选手准备。推荐直接阅读英语原文,既准确可靠又可提高英语水平。做题方式模拟正式比赛,采用标准测评机、文件...
资源包包括USACO 2001-2007年月赛的测试数据;usaco月赛十年题典(2000-2009),usaco月赛2002-2008题解。单独下载需资源分30分以上。为了方便编程爱好者,我这边统一下载打包。欢迎下载。
USACO培训网站 我为章节解决方案。 每个文件的多行USACO标识信息注释 第1章全部的解决方案 第2章全部的解决方案
USACO-TurtleCamera 该存储库包含我对USACO问题的所有解决方案。 CSE 199工作区目录将是我用来帮助开发USACO课程的主要目录。
usaco 2010-2011 nov news,喜欢usaco的朋友可以看看
我的USACO题解和程序
Java中的USACO金问题 YYMM 姓名 文件夹 笔记 代码 1812 美食 1812 牛适应性 1812 团队合作
USACO-Guide
USACO培训页面美国计算机奥林匹克训练页2015年6月17日开始