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 ...
随机推荐
- aspx文件移动到新建的文件夹中设置路径的问题
项目中仅仅把aspx移动到想要的文件夹内是会出错的,不用想也知道是路径问题.这里我就说这个路径该如何去修改. 两个地方需要修改:1.母版路径修改方法: <link href="Styl ...
- 利用绝对定位与margin实现元素居中
例: 要让一个width:100px ; height: 100px;的div,相对body居中. div{ width:100px; height:100px; border: 1px solid ...
- VS2010 error RC2135: file not found
VS2010 C++ win32 DLL 工程, 添加 rc 文件, 编辑 String Table. 默认情况下英文版本的 rc 文件能够顺序编译通过,为了让工程支持多语言,将字符串修改为其他语言时 ...
- Head First 设计模式系列之二----备忘录模式(java版)
申明:这几天无意中关注到备忘录模式,比较陌生回家一番参考书,只在附录里记录了该模式.后来在园子里有发现了有专门写设计模式的博客,并且写的也得牛逼.附上链接 http://www.cnblogs.com ...
- C++字符串函数与C字符串函数比较
赋值拷贝: #include <iostream> #include <string> using namespace std; void main(){ string a=& ...
- WF4的数据库 表
WF4的数据库 表 SQL 建表 SqlPersistenceProviderSchema.sql InstanceData 实例数据表 SqlPersistenceService_Schema.sq ...
- javascript学习笔记2
二.下列哪些变量是不正确的 说明原因 var a1; var b1 = 3; c1 ='good'; var d1 = c1 = e1; × 连等必须赋值 var g1 = 'hei' goo ...
- 解决IE6下Position:fixed问题(只用css)
在IE6.0及以下版本的浏览器里是不支持position:fixed.而在IE7,IE8,firefox,opera,chrome都可以完美的支持此特性的.解决此问题的要点主要有: 1).容器要有一个 ...
- java根据url获取json对象
package test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; ...
- TCP UDP 协议的选择
行业应用中TCP/IP传输协议和UDP协议的选择! 中国移动.中国联通推行的GPRS网络.CDMA网络已覆盖大量的区域,通过无线网络实现数据传输成为可 能.无线Modem采用GPRS.CDMA模块通过 ...