hihocoder #1290 : Demo Day (2016微软编程测试第三题)
#1290 : 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 题目的意思是:
给你一个矩阵由‘.’和'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微软编程测试第三题)的更多相关文章
- hihocoder #1289 : 403 Forbidden (2016 微软编程笔试第二题)
#1289 : 403 Forbidden 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi runs a web server. Sometimes ...
- ACM学习历程—Hihocoder 1290 Demo Day(动态规划)
http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...
- hihocoder #1290 : Demo Day
传送门 #1290 : Demo Day 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You work as an intern at a robotics star ...
- hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)
时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device w ...
- AC日记——C’s problem(c) TYVJ P4746 (清北学堂2017冬令营入学测试第三题)
P4746 C’s problem(c) 时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试 描述 题目描述 小C是一名数学家,由于它自制力比 ...
- 2016微软技术大会Azure相关回顾
3 天的时间稍纵即逝,伴随着本届大会压轴大奖的揭晓,2016 年度的微软技术大会完美落幕.以“数字化转型”为主题,来自微软全球的近百位顶尖技术专家.工程师和业务负责人拔冗而至,在 130 余场的专业技 ...
- C#编程总结(三)线程同步
C#编程总结(三)线程同步 在应用程序中使用多个线程的一个好处是每个线程都可以异步执行.对于 Windows 应用程序,耗时的任务可以在后台执行,而使应用程序窗口和控件保持响应.对于服务器应用程序,多 ...
- 并发编程之第三篇(synchronized)
并发编程之第三篇(synchronized) 3. 自旋优化 4. 偏向锁 撤销-其它线程使用对象 撤销-调用wait/notify 批量重偏向 批量撤销 5. 锁消除 4.7 wait/notify ...
- 计算概论(A)/基础编程练习2(8题)/5:点和正方形的关系
#include<stdio.h> #include<math.h> int main() { // 输入坐标 float x, y; while(scanf("%f ...
随机推荐
- VS2010 VS2012 如何连接Oracle 11g数据库
oracle是开发者常用的数据库,在做.NET开发是,由于Vs自带的驱动只能连接oracle 10g及以下版本,那么如何连接oracle 11g呢? 工具/原料 事先安装VS2010或者VS201 ...
- IO流01_File类
[分类] Java的IO通过java.io包下的类和接口来支持. 1.按照流向: 输入流 输出流 2.按照操作数据的大小: 字节流( 8位字节 ) 字符流( 16位字节 ) 3.按照角 ...
- 学习C++ Primer 的个人理解(十)
标准库没有给每个容器都定义成员函数来实现 查找,替换等操作.而是定义了一组泛型算法,他们可以用于不同类型的元素或多种容器类型. 迭代器令算法不依赖与容器 算法永远不会执行容器的操作 算法本身不会执行容 ...
- 九度OJ 1408 吃豆机器人 -- 动态规划
题目地址:http://ac.jobdu.com/problem.php?pid=1408 题目描述: 淘宝公司内部有许多新鲜的小玩具,例如淘宝智能机器人.小时候,大家都玩过那个吃豆子的游戏吧,这机器 ...
- javascript控制子页面对父页面控件操作
//赋值 window.parent.document.getElementById("partyid_trade_edit").value = data.data.partyid ...
- HTML5的简介
前言:作为IOS开发工程师,终会接触到网页前端开发,甚至可能会有 用HTML5开发IOS的app客户端的需求.比如现在上架的app就有比如理财类型的app有的就用HTML开发的,从理财类型的app需求 ...
- 布局时margin会影响父元素
布局时margin会影响父元素.md 在布局使用margin时 <div class="login-bg"> <div class="login&quo ...
- android适应屏幕
生产android手机的厂商多不胜数,造就了android手机的屏幕尺寸也是不计其数.开发者为了使应用在各个品牌,各个型号的手机屏幕上保持一致的用户体验,就需要运用多种使应用的UI能适应不同屏幕尺寸的 ...
- java JDBC操作MySQL数据库
一,首先在MYSQL建立一个数据库,例如Geek99DB: create database Geek99DB; use Geek99DB; 然后建立一个表CustomerTab: create tab ...
- UI表单
Monk.UI表单美化插件诞生记! 阅读目录 背景 预览效果 表单组件 普通文本框 多行文本框 复选框 切换滑块 单选框 下拉选择框 数字输入框 时间选择 文件选择 显示文本 按钮 开源地址 背景 ...