#1290 : 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

题目的意思是:
给你一个矩阵由‘.’和'b'组成 ‘.’表示可以走,'b'表示障碍物,机器人只能往右走直到不能走(碰到障碍物或者到头)和往下走直到不能走,两个方向,起始点是左上角,出口是右下角,为了机器人
能够走到出口,你可以将'.'变为'b', 也可以将'b'变为'.',,求为了使机器人走到出口,最少的变换次数。
(机器人从起点,一开始是往右走的,记住!!!!!!!) 思路:
应该算是动态规划吧,对于每一个点,到达这个点只能是经过上方点,或者从左方点到达该点,那么对于每一个点,一开始想的是用f[i][j],表示到达(i,j)需要变动的最小次数,但是发现到达每一个点是有方向的
,而且如果只用f[i][j],表示到达(i,j)的最小变动次数,不知道方向的话是无法递推下一个点的,所以,
我们用up[i][j],表示从上方到达该点需要变动的最小次数,r[i][j]表示从左方到达该点需要变动的最小次数,
  (i-1,j) (i - 1,j+1)
  (i,j)  
 对于up[i][j],可以是从左方到达(i-1, j),再到达(i,j),但此时(i-1, j+1)必须为'b'或者边界,也可以从上方到达(i-1,j)再到达(i,j);
还有就是如果(i,j)本身为'b'的话,则需要up[i][j]+1; up[i-1][j]
up[i][j] = min
r[i-1][j] + 1(如果(i-1,j)不为‘b’或者边界则需要+1); 同理可得: up[i][j-1] (+1) (如果(i+1,j-1)不为'b'或者边界,则需要+1)
r[i][j] = min
r[i][j-1] 为了处理方便在输入时,在矩阵的最右边和最下边添加了一行,一列'b';
 
 
 #include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#define maxn 110
#define inf 9999999
using namespace std;
int n, m;
int r[maxn][maxn];
int up[maxn][maxn];
char str[maxn][maxn];
int GetUp(int i, int j)
{
int x = r[i - ][j];
int y = up[i - ][j]; if(str[i-][j+]!= 'b')x++;
int ans = min(x,y);
return ans; }
int GetR(int i, int j)
{
int x = r[i][j - ];
int y = up[i][j - ];
if(str[i+][j - ]!='b')y++;
return min(x,y);
}
int main()
{
while(scanf("%d%d", &n,&m)!=EOF)
{
for(int i = ; i < n;i++)
{
scanf("%s", str[i]);
int len = strlen(str[i]);
str[i][len] = 'b';
str[i][len+] = '\0';
} for(int i = ; i <= m; i++)
{
str[n][i] = 'b';
}
//初始化第一行
int cnt = ;
for(int j = ; j < m;j++)
{
if(str[][j] == 'b')
{
++cnt; }
r[][j] = cnt;
up[][j] = inf;
} cnt = ;
// 初始化第一列
// 一开始往右走,所以要判断一下
if(str[][]!='b')++cnt; for(int i = ; i < n; i++)
{
if(str[i][] == 'b')
{
++cnt; }
up[i][] = cnt;
r[i][] = inf;
} for(int i = ; i < n;i++)
{
for(int j = ;j < m;j++)
{
up[i][j] = GetUp(i,j);
r[i][j] = GetR(i, j);
if(str[i][j] == 'b')
{
up[i][j]++;
r[i][j]++;
}
}
} printf("%d\n", min(up[n - ][m - ], r[n - ][m - ])); }
}

hihocoder #1290 : Demo Day (2016微软编程测试第三题)的更多相关文章

  1. hihocoder #1289 : 403 Forbidden (2016 微软编程笔试第二题)

    #1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Sometimes ...

  2. ACM学习历程—Hihocoder 1290 Demo Day(动态规划)

    http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...

  3. hihocoder #1290 : Demo Day

    传送门 #1290 : Demo Day 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You work as an intern at a robotics star ...

  4. hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)

    时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device w ...

  5. AC日记——C’s problem(c) TYVJ P4746 (清北学堂2017冬令营入学测试第三题)

    P4746 C’s problem(c)   时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试 描述 题目描述 小C是一名数学家,由于它自制力比 ...

  6. 2016微软技术大会Azure相关回顾

    3 天的时间稍纵即逝,伴随着本届大会压轴大奖的揭晓,2016 年度的微软技术大会完美落幕.以“数字化转型”为主题,来自微软全球的近百位顶尖技术专家.工程师和业务负责人拔冗而至,在 130 余场的专业技 ...

  7. C#编程总结(三)线程同步

    C#编程总结(三)线程同步 在应用程序中使用多个线程的一个好处是每个线程都可以异步执行.对于 Windows 应用程序,耗时的任务可以在后台执行,而使应用程序窗口和控件保持响应.对于服务器应用程序,多 ...

  8. 并发编程之第三篇(synchronized)

    并发编程之第三篇(synchronized) 3. 自旋优化 4. 偏向锁 撤销-其它线程使用对象 撤销-调用wait/notify 批量重偏向 批量撤销 5. 锁消除 4.7 wait/notify ...

  9. 计算概论(A)/基础编程练习2(8题)/5:点和正方形的关系

    #include<stdio.h> #include<math.h> int main() { // 输入坐标 float x, y; while(scanf("%f ...

随机推荐

  1. 源代码jar包中中文注释乱码

    目前公司开发的多个组件有打包源代码并发布到nexus,但是很多同事通过maven使用组件时,直接通过eclipse浏览源代码时,发现中文注释为乱码的问题.其实这个eclipse默认编码造成的问题.可以 ...

  2. (转) VS2012程序打包部署详解

    程序编写测试完成后接下来我们要做的是打包部署程序,但VS2012让人心痛的是没有了打包工具.不知道出于什么原因微软没有将打包工具集成在开发环境中,但是我知道总会有解决办法的.     经过翻阅资料发现 ...

  3. javascript RegExp类型 学习小记

    在js里面,可以有两种方法来定义正则表达式,第一种是通过字变量的形式,第二种则是通过构造函数的形式: 1 字变量形式,格式是长这样子的 var expression = /pattern/flag p ...

  4. 10_HTTP协议_入门知识

    [什么是HTTP协议] 对 浏览器客户端 和  服务器端之间的数据传输的格式规范. 客户端连上web服务器后,若想获得web服务器中的某个web资源,需遵循一定的通讯格式,HTTP协议用于定义客户端与 ...

  5. HDOJ 1042 N! -- 大数运算

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1042 Problem Description Given an integer N(0 ≤ N ≤ 1 ...

  6. Xml通用操作类

    using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml ...

  7. Android_Json实例

    概要: 最近由于自己的兴趣,想在Android开发一个自己的App,需要使用服务器,所以交换数据是逃不掉了的,但是学生党没有固定的服务器,因此使用的新浪的SAE,在学习的前期下可以尝试一下,挺不错的一 ...

  8. el-get

    el-get Table of Contents 1. 依赖 2. 安装 3. 配置 3.1. 自定义包配置 4. 命令 5. 管理扩展 el-get 是一个emacs下的扩展管理工具.就像apt-g ...

  9. jQuery Mobile里xxx怎么用呀?(控件篇)

    jQuery Mobile里都有什么控件? http://api.jquerymobile.com/category/widgets/ jQuery Mobile里slider控件的change事件怎 ...

  10. js 数字,金额 用逗号 隔开。数字格式化

    例如: 12345格式化为12,345.00 12345.6格式化为12,345.60 12345.67格式化为 12,345.67 只留两位小数. 回来后写了个格式化函数.可以控制小数位数,自动四舍 ...