时间限制: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. node express 会话管理中间件 --- cookie-parser

    本文转载自:https://www.cnblogs.com/bq-med/p/8995100.html cookie是由服务器发送给客户端(浏览器)的小量信息. 我们知道,平时上网时都是使用无状态的H ...

  2. python+tushare获取股票每日停复牌信息

    接口:suspend 更新时间:不定期 描述:获取股票每日停复牌信息 注:tushare模块下载和安装教程,请查阅我之前的文章 输入参数 名称      |      类型      |      必 ...

  3. python调用tushare获取A股上市公司基础信息

    接口:stock_company 描述:获取上市公司基础信息 积分:用户需要至少120积分才可以调取,具体请参阅最下方积分获取办法 注:tushare库下载和初始化教程,请查阅我之前的文章 输入参数 ...

  4. 2019ICPC南京网络赛A The beautiful values of the palace

    题意:蛇形填数超大版本,需要求出一些给定坐标的值的数位和,然后q次询问,一个矩形区域内值的和是多少 解题思路:二维偏序前缀和的经典题 二维偏序:求(x,y)左下角点的个数,思路是对x,y升序排序,用树 ...

  5. zabbix--添加host

    在client配置好zabbix_agent后,如果server端没配置自动发现,那就需要创建添加host. 首先找到地方.Configuration--Hosts--Create Host 创建ho ...

  6. SpringBoot Controller 中使用多个@RequestBody的正确姿势

    最近遇到Controller中需要多个@RequestBody的情况,但是发现并不支持这种写法, 这样导致 1.单个字符串等包装类型都要写一个对象才可以用@RequestBody接收: 2.多个对象需 ...

  7. 精选 Dubbo RPC 面试题,比较全面,含答案

    精选 Dubbo RPC 面试题,比较全面,含答案 hu1991die 搜云库技术团队 搜云库技术团队 微信号 souyunku 功能介绍 专注于分享最有价值的互联网技术干货文章,内容覆盖,Java后 ...

  8. mysql数据库中某字段一部分乱码

    笔者问题:mysql表(表中数据就是乱码,可能是插入时编码问题,这个问题以后解决)导出excel时数据中有乱码(但是在页面上查看是正常的),我们希望能导出一份没有中文乱码的excel 根据热力站中一次 ...

  9. sublime里面几个个人觉得比较实用的快捷键

    Alt+F3 选中文本按下快捷键,即可一次性选择全部的相同文本进行同时编辑.举个栗子:快速选中并更改所有相同的变量名.函数名等. Ctrl+L 选中整行,继续操作则继续选择下一行,效果和 Shift+ ...

  10. ubuntu查看时间同步服务器的匹配源

    当服务器时间与设定好的同步时间源的时间有差异的时候,一般都需要先查看本机的时间同步服务功能是否在正常的运转,以及同步的时间源是哪里,在这里为大家提供一个检查时间用的命令. ubuntu版本 servi ...