【hihocoder】Demo Day
描述
You work as an intern at a robotics startup. Today is your company's demo day. During the demo your company's robot will be put in a maze and without any information about the maze, it should be able to find a way out.
The maze consists of N * M grids. Each grid is either empty(represented by '.') or blocked by an obstacle(represented by 'b'). The robot will be release at the top left corner and the exit is at the bottom right corner.
Unfortunately some sensors on the robot go crazy just before the demo starts. As a result, the robot can only repeats two operations alternatively: keep moving to the right until it can't and keep moving to the bottom until it can't. At the beginning, the robot keeps moving to the right.
rrrrbb..
...r.... ====> The robot route with broken sensors is marked by 'r'.
...rrb..
...bb...
While the FTEs(full-time employees) are busy working on the sensors, you try to save the demo day by rearranging the maze in such a way that even with the broken sensors the robot can reach the exit successfully. You can change a grid from empty to blocked and vice versa. So as not to arouse suspision, you want to change as few grids as possible. What is the mininum number?
输入
Line 1: N, M.
Line 2-N+1: the N * M maze.
For 20% of the data, N * M <= 16.
For 50% of the data, 1 <= N, M <= 8.
For 100% of the data, 1<= N, M <= 100.
输出
The minimum number of grids to be changed.
样例输入
4 8
....bb..
........
.....b..
...bb...
样例输出
1
题意:改变最少的次数,使得机器人能到达右下角。
考虑动态规划,dp[i][j]表示到达(i-1,j-1)处改变的最少次数,因为有两个方向,所有再加一维,表示方向,所以dp[i][j][k],k=1表示向右,k=0表示向下
1. dp[i][j][1] = min(dp[i][j-1][1], dp[i-1][j][0])
2. dp[i][j][0] = min(dp[i-1][j][0], dp[i][j-1][1])
注意以下几种情况:
1、(i,j)处为障碍
2、到达边界
3、初始情况,即(0,0)处的处理
import java.util.Arrays;
import java.util.Scanner; public class DemoDay {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String line = in.nextLine();
int m = Integer.parseInt(line.split(" ")[0]);
int n = Integer.parseInt(line.split(" ")[1]);
char [][] grid = new char[m][n];
for (int i = 0; i < m; ++i)
grid[i] = in.nextLine().toCharArray();
int [][][] dp = new int[m+1][n+1][2];
for (int i = 0; i <= m; ++i)
for ( int j = 0; j <= n; ++j)
Arrays.fill(dp[i][j], m*n);
// (0,0)处处理
dp[1][1][1] = 0;
dp[1][1][0] = (n == 1 || grid[0][1] == 'b') ? 0 : 1;
for (int i = 1; i <= m; ++i){
for (int j = 1; j <= n; j++){
if (i == 1 && j == 1) // 已处理过(0,0),跳过
continue;
if (grid[i-1][j-1] == 'b') // 考虑是否为障碍,为障碍多一次改变次数
dp[i][j][0]=dp[i][j][1]=1;
else dp[i][j][0]=dp[i][j][1]=0;
int step = 0; // 考虑向下
if (j == n || grid[i-1][j] == 'b')
step = dp[i][j-1][1];
else step = dp[i][j-1][1]+1;
dp[i][j][0] += Math.min(step, dp[i-1][j][0]); // 考虑向右
if (i == m || grid[i][j-1] == 'b')
step = dp[i-1][j][0];
else step = dp[i-1][j][0]+1;
dp[i][j][1] += Math.min(step, dp[i][j-1][1]);
}
}
System.out.println(Math.min(dp[m][n][0], dp[m][n][1]));
}
}
【hihocoder】Demo Day的更多相关文章
- 【hihoCoder】1148:2月29日
问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...
- 【hihoCoder】1288 : Font Size
题目:http://hihocoder.com/problemset/problem/1288 手机屏幕大小为 W(宽) * H(长),一篇文章有N段,每段有ai个字,要求使得该文章占用的页数不超过P ...
- 【hihoCoder】1082: 然而沼跃鱼早就看穿了一切
题目:http://hihocoder.com/problemset/problem/1082 输入一个字符串,将其中特定的单词替换成另一个单词 代码注意点: 1. getline(istre ...
- 【hihoCoder】1121:二分图一·二分图判定
题目 http://hihocoder.com/problemset/problem/1121 无向图上有N个点,两两之间可以有连线,共有M条连线. 如果对所有点进行涂色(白/黑),判定是否存 ...
- 【hihoCoder】1036 Trie图
题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...
- 【hihoCoder】1039 : 字符消除
题目:http://hihocoder.com/problemset/problem/1039 给定一个字符串s,只包含'A', 'B', 'C'三种字符 1. 向 s 的任意位置 (包括头和尾) 中 ...
- 【hihoCoder】1037 : 数字三角形
题目:http://hihocoder.com/problemset/problem/1037 一个迷宫有n层,第 i 层有 i 个房间 从第i层的第i个房间(i, i)可以走到第i+1层的第i个房间 ...
- 【hihoCoder】1033: 交错和
初探数位dp 介绍了数位类统计的基础知识.以下列出其中的基础点: 基本问题 统计在区间[l, r]中满足条件的数的个数 思路 1. [l, r] 将问题转换为 在[0, r]中满足条件的个数 - 在[ ...
- 【Hihocoder】1014 : Trie树
问题:http://hihocoder.com/problemset/problem/1014 给定一个字符串字典dict,输入字符串str, 要求从dict中找出所有以str为前缀的字符串个数. 构 ...
随机推荐
- 常用内置模块(二)——logging模块
logging模块 一.logging作用 1. 控制日志级别 2. 控制日志格式 3. 控制输出的目标为文件 二.日志级别 logging.debug( logging.info( loggin ...
- PHP对象在内存中的分配(转载)
http://www.cnblogs.com/hongfei/archive/2012/06/12/2547120.html 对像在PHP 里面和整型.浮点型一样,也是一种数据类,都是存储不同类型数据 ...
- Firefox好用的快捷键
1,Alt+D 你可以使用该快捷键直接把光标转到火狐的地址栏.非常有用 2,Ctrl + T和Ctrl + Shift + T Ctrl+T帮你打开一个新标签,Ctrl+Shift+T重新打开上次关闭 ...
- http over git server
编译安装git 参考 <CentOS7编译安装git> 安装httpd yum install httpd -y 安装gitweb yum install gitweb -y 创建项目根目 ...
- Unity中嵌入网页插件Embedded Browser2.1.0
背景 最近刚换了工作,新公司不是做手游的,一开始有点抵触,总觉得不是做游戏自己就是跨行了,认为自己不对口,但是慢慢发现在这可以学的东西面很广,所以感觉又到了打怪升级的时候了,老子就在这进阶了. 一进公 ...
- 实体类Json串转成DataTable
private DataTable GetJsonToDataTable(string json) { List<Object_DeclareInfo> arrayList = JsonC ...
- VMware1设备与主机共享网络的问题
问题的提出: 最近需要用到VMware1设备来配置网络,顺便将VMware1设备与主机进行共享网络,这样master就能直接访问网络了,但是原本以为直接在wlan设备上选择网络共享就行了,但是却没法收 ...
- nginx 简单使用
一,下载 http://nginx.org/en/download.html 这个是我下载的windows版本 二,解压后目录 三,修改配置文件 (由于80端口很可能被 SQL Server Repo ...
- idea-----idea中“cannot resolve symbol servlet”的解决
原文章链接: 传送器>>>>>>>>>>>>>>>>. 第一次使用IntelliJ IDEA时我遇到了& ...
- flyway 管理数据库版本
Flyway 和 Liquibase 都是 Java 项目中常用的 DB migration 工具, 从使用简便性看,Flyway 比 Liquibase 更简单, 从 github 的 star 数 ...