微软2016校园招聘4月在线笔试 hihocoder 1290 Demo Day (dp)
2016-04-07 14:38
525 查看
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
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.
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 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.
样例输入
样例输出
题目链接:http://hihocoder.com/problemset/problem/1290
题目大意:一个机器人从左上要走到右下,每次先一直往右走,被挡了就一直往下走,'b'为障碍,每个点可以把'b'变成'.',也可以把'.'变成'b',问使得机器人按规则走到终点最少要改变几个点的状态
题目分析:dp[i][j][k]表示走到点(i, j)方向为k (0为右,1为下)时所要改变的最小次数,状态转移的时候如果本来就有b挡着那可以直接转,否则要转的话次数要加1,因为相当于改变一个'.'的状态来让它转向,初始状态dp[1][1][0] = 0
单点时限: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
题目链接:http://hihocoder.com/problemset/problem/1290
题目大意:一个机器人从左上要走到右下,每次先一直往右走,被挡了就一直往下走,'b'为障碍,每个点可以把'b'变成'.',也可以把'.'变成'b',问使得机器人按规则走到终点最少要改变几个点的状态
题目分析:dp[i][j][k]表示走到点(i, j)方向为k (0为右,1为下)时所要改变的最小次数,状态转移的时候如果本来就有b挡着那可以直接转,否则要转的话次数要加1,因为相当于改变一个'.'的状态来让它转向,初始状态dp[1][1][0] = 0
#include <cstdio> #include <algorithm> using namespace std; int const MAX = 1e2 + 5; int n, m; int dp[MAX][MAX][2]; //0 -> 右, 1 -> 下 char s[MAX][MAX]; int main() { scanf("%d %d", &n, &m); for(int i = 1; i <= n; i++) scanf("%s", s[i] + 1); for(int i = 0; i <= n; i++) for(int j = 0; j <= m; j++) dp[i][j][0] = dp[i][j][1] = 0x3fffffff; dp[1][1][0] = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(i == 1 && j == 1) continue; dp[i][j][1] = dp[i - 1][j][1]; if(s[i - 1][j + 1] == 'b' || j == m) dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][0]); else dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][0] + 1); dp[i][j][0] = dp[i][j - 1][0]; if(s[i + 1][j - 1] == 'b' || i == n) dp[i][j][0] = min(dp[i][j][0], dp[i][j - 1][1]); else dp[i][j][0] = min(dp[i][j][0], dp[i][j - 1][1] + 1); if(s[i][j] == 'b') { dp[i][j][0] ++; dp[i][j][1] ++; } } } printf("%d\n", min(dp [m][0], dp [m][1])); }
相关文章推荐
- iOS开发出错whose view is not in the window hierarchy!的解决
- 真机调试问题:No matching provisioning profile found
- iOS开发出错whose view is not in the window hierarchy!的解决
- iOS开发出错whose view is not in the window hierarchy!的解决
- android.os.TransactionTooLargeException异常
- xshell中Ctrl+S锁定屏幕
- android中sharedPreferences的用法
- 6.1.3 qmgr模块函数调用关系
- opencv如何载入内存中的图像文件
- java JPA 介绍
- ASP.NET中TextBox设置只读时不能取值的处理
- iOS字体笔记
- 关于matlab2014a中生成dll文件,打包成com组件出现的问题和解决方法
- 获取 SVN 一段时间内文件改动列表
- 去除chrome浏览器自动添加的默认样式
- 6.1.2 qmgr模块调度策略
- 2016.4.2 动态规划练习--讲课整理
- android 关于提高第三方app的service优先级
- POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
- Ubuntu防火墙ufw安装配置