时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

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的更多相关文章

  1. 【hihoCoder】1148:2月29日

    问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...

  2. 【hihoCoder】1288 : Font Size

    题目:http://hihocoder.com/problemset/problem/1288 手机屏幕大小为 W(宽) * H(长),一篇文章有N段,每段有ai个字,要求使得该文章占用的页数不超过P ...

  3. 【hihoCoder】1082: 然而沼跃鱼早就看穿了一切

      题目:http://hihocoder.com/problemset/problem/1082 输入一个字符串,将其中特定的单词替换成另一个单词   代码注意点: 1. getline(istre ...

  4. 【hihoCoder】1121:二分图一·二分图判定

      题目   http://hihocoder.com/problemset/problem/1121 无向图上有N个点,两两之间可以有连线,共有M条连线. 如果对所有点进行涂色(白/黑),判定是否存 ...

  5. 【hihoCoder】1036 Trie图

    题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...

  6. 【hihoCoder】1039 : 字符消除

    题目:http://hihocoder.com/problemset/problem/1039 给定一个字符串s,只包含'A', 'B', 'C'三种字符 1. 向 s 的任意位置 (包括头和尾) 中 ...

  7. 【hihoCoder】1037 : 数字三角形

    题目:http://hihocoder.com/problemset/problem/1037 一个迷宫有n层,第 i 层有 i 个房间 从第i层的第i个房间(i, i)可以走到第i+1层的第i个房间 ...

  8. 【hihoCoder】1033: 交错和

    初探数位dp 介绍了数位类统计的基础知识.以下列出其中的基础点: 基本问题 统计在区间[l, r]中满足条件的数的个数 思路 1. [l, r] 将问题转换为 在[0, r]中满足条件的个数 - 在[ ...

  9. 【Hihocoder】1014 : Trie树

    问题:http://hihocoder.com/problemset/problem/1014 给定一个字符串字典dict,输入字符串str, 要求从dict中找出所有以str为前缀的字符串个数. 构 ...

随机推荐

  1. 拾遗:git pull 与 push 远程分支与本地分支顺序识别问题

    最后放置的都是数据最终到达的仓库分支名称 对于pull来说,是拉到本地,所以本地仓库分支名称写在最后 git pull [--force] [remote repo]:[my repo] 对于push ...

  2. 2019秋Java课程总结&实验总结一

    1.打印输出所有的"水仙花数",所谓"水仙花数"是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个"水仙花数". 实验源码: ...

  3. 46-Ubuntu-系统信息-1-date和cal查看系统时间

    序号 命令 作用 01 date 查看系统时间 02 cal calendar查看日历,-y选项可以查看一年的日历

  4. 总分 Score Inflation

    题目背景 学生在我们USACO的竞赛中的得分越多我们越高兴. 我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助 题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类& ...

  5. webpack 配置之入门二(css 篇)

    在项目中我们通过 css 来美化页面,css 也成为了网站不可或缺的一部分,这章节主要介绍 webpack 处理 css 部分, 1.webpack 处理 css 在 webpack 中,我们通过 s ...

  6. @Conditional系列注解例子

    1. @Conditional 说明:指定的Condition实现类,matches方法返回true则注入bean,false则不注入 @Configuration public class Bean ...

  7. selenium 操作键盘事件

    一.key包提供按键方法 使用必须先引用key包:from selenium.webdriver.common.keys import Keys 键盘事件,在现实操作中我们习惯性的按tab见切换到写一 ...

  8. mysql-5.5.17-win64 安装方法

    双击mysql-5.5.17-win64.msi,即开始安装,出现如下界面 2 点击next,出现如下界面,默认为Typical,改选为Custom,单击Next 3 选择MySQL Server后, ...

  9. net core静态文件 访问除默认目录文件配置

    在我们项目的实际应用中,不光是需要访问默认静态文件夹 wwwroot ,还有可能要要去访问除默认目录以外的文件夹,接下来我们进行配置 1.在根目录创建一个文件夹,继续创建它的子文件夹Images,在I ...

  10. Spring容器对Bean组件的管理

    Bean对象创建 默认是随着容器创建 可以使用 lazy-init=true:在调用 getBean 延迟创建 也可以用 <beans default-lazy-init="true& ...