【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为前缀的字符串个数. 构 ...
随机推荐
- PAT_A1083#List Grades
Source: PAT A1083 List Grades (25 分) Description: Given a list of N student records with name, ID an ...
- node.js 中的 fs (文件)模块
记录 fs 模块的方法及使用 1. fs.stat 获取文件大小,创建时间等信息 // 引入 fs 模块 const fs = require('fs'); fs.stat('01.fs.js', ( ...
- 调用phone库,查询手机号码归属地(4)
需要安装pymysql,phone库 #!/usr/bin/python # -*- coding: utf-8 -*- import sys, pymysql, logging, phone fro ...
- 调用API接口,查询手机号码归属地(2)
使用pymysql pip install pymysql 创建mysql测试表 CREATE TABLE `userinfo` ( `id` int(20) NOT NULL AUTO_INCREM ...
- STL 队列模板实现
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u010016150/article/details/32715801 C++ Prime确实有点 ...
- win7+ubuntu双系统安装
XP+ubuntu双系统可參考 xp硬盘上安装ubuntu12.04双系统 1.XP安全不再,最终将吾等磨机的人逼上梁山,是时候做一个win7+ubuntu的双系统了. 废话少说,直接进入主题,先腾出 ...
- day04 mysql单表查询 多表查询 pymysql的使用
day04 mysql pymysql 一.单表查询 1.having过滤 一般用作二次筛选 也可以用作一次筛选(残缺的: 只能筛选select里面 ...
- 修改Ubuntu16.04默认主题标题栏的颜色
默认主题为Ambiance: sudo gedit /usr/share/themes/Ambiance/gtk-3.0/gtk-main.css 将: @define-color dark_bg_c ...
- 使用vue-cli 脚手架快速搭建单页面组件 -------webpack工具的介绍
在使用vue-cli时我们先了解一下什么是webpack. Webpack 是当下最热门的前端资源模块化管理和打包工具.它可以将许多松散的模块按照依赖和规则打包成符合生产环境部署的前端资源.还可以将按 ...
- delphi xe10 获取屏幕截图
//截取屏幕图片 function MakeScaleScreenshot(Sender: TControl): TBitmap; function GetScreenScale: Single; v ...