时间限制: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]));
}
}

最新文章

  1. 玩转Windows服务系列——Windows服务小技巧
  2. Hibernate的性能优化问题
  3. 第11章 Windows线程池(3)_私有的线程池
  4. url匹配和match()方法
  5. 【转】Oracle 表空间与数据文件
  6. cdev_init函数
  7. Relativelayout属性
  8. Could not load db driver class: com.mysql.jdbc.Driver解决方法
  9. 数字证书简介及Java编码实现
  10. Mysql 5.6主从同步配置与解决方案
  11. 用include()和ob_get_contents( )方法 生成静态文件
  12. 03 编译安装apache的简易配置
  13. HDU--1195--bfs--Open the Lock
  14. 4.C++中的函数重载,C++调用C代码,new/delete关键字,namespace(命名空间)
  15. Java的小实验——各种测试以及说明
  16. [No0000190]vim8安装教程和vim中文帮助文档Vimcdoc安装方法-Vim使用技巧(5)
  17. 记录Centos7搭建ftp服务器以及遇到的各种坑
  18. hive 入门
  19. Eclipse修改XML默认打开方式
  20. baidu-aip-SDK node.js 身份证识别

热门文章

  1. PAT_A1070#Mooncake
  2. JOGL图形形状
  3. springboot+mybatis+layUI
  4. 三(1)、springcloud之Eureka服务注册与发现
  5. python调用tushare获取股票月线数据
  6. !vtop 命令
  7. Es6中let与const的区别:(神奇的块级作用域)
  8. vim 中 ctags的应用
  9. 笔记56 Mybatis快速入门(七)
  10. ./vimrc代码解析全