【hihocoder】Demo Day

时间限制: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)处的处理

 1 import java.util.Arrays;
 2 import java.util.Scanner;
 3 
 4 public class DemoDay {
 5     public static void main(String[] args) {
 6         Scanner in = new Scanner(System.in);
 7         String line = in.nextLine();
 8         int m = Integer.parseInt(line.split(" ")[0]);
 9         int n = Integer.parseInt(line.split(" ")[1]);
10         char [][] grid = new char[m][n];
11         for (int i = 0; i < m; ++i)
12             grid[i] = in.nextLine().toCharArray();
13         int [][][] dp = new int[m+1][n+1][2];
14         for (int i = 0; i <= m; ++i)
15             for ( int j = 0; j <= n; ++j)
16                 Arrays.fill(dp[i][j], m*n);
17         // (0,0)处处理
18         dp[1][1][1] = 0;
19         dp[1][1][0] = (n == 1 || grid[0][1] == 'b') ? 0 : 1;
20         for (int i = 1; i <= m; ++i){
21             for (int j = 1; j <= n; j++){
22                 if (i == 1 && j == 1)   // 已处理过(0,0),跳过
23                     continue;
24                 if (grid[i-1][j-1] == 'b')  // 考虑是否为障碍,为障碍多一次改变次数
25                     dp[i][j][0]=dp[i][j][1]=1;
26                 else dp[i][j][0]=dp[i][j][1]=0;
27                 int step = 0;
28                 
29                 // 考虑向下
30                 if (j == n || grid[i-1][j] == 'b')
31                     step = dp[i][j-1][1];
32                 else step = dp[i][j-1][1]+1;
33                 dp[i][j][0] += Math.min(step, dp[i-1][j][0]);
34                 
35                 // 考虑向右
36                 if (i == m || grid[i][j-1] == 'b')
37                     step = dp[i-1][j][0];
38                 else step = dp[i-1][j][0]+1;
39                 dp[i][j][1] += Math.min(step, dp[i][j-1][1]);
40             }
41         }
42         System.out.println(Math.min(dp[m][n][0], dp[m][n][1]));
43     }
44 }
原文地址:https://www.cnblogs.com/shizhh/p/5362625.html