微软2016校园招聘4月:hihocoder- #1290 : Demo Day
2016-06-29 20:41
543 查看
http://hihocoder.com/problemset/problem/1290?sid=811882
时间限制: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.
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.
样例输入
样例输出
动态规划题
首先根据n,m值初始化输入数组map。同时为了处理方便在输入时,在map的最右边和最下边添加了一行和一列'b',原因是后面判断(i,j)值时候需要根据(i-1,j+1)或(i+1,j-1)的取值
因为到达(i,j)的方式可以是从(i-1,j)向下到达,或者从(i,j-1)向右到达,因此用了两个辅助数组up和left,up[i][j]表示从上方到达(i,j)需要变动的最小次数,left[i][j]表示从左方到达(i,j)需要变动的最小次数.
当i==0
up[0][j]=INT_MAX;表示无法从-1行向下
left[0][j]的取值由left[0][j - 1]和map[0][j]是否为'b'决定,因此left[0][j] = left[0][j - 1] + (map[0][j] == '.' ? 0 : 1)
当j==0
left[i][0]=INT_MAX;表示无法从-1列向右
up[i][0]的取值由up[i - 1][0]和map[i][0]是否为'b'决定,同时,因为一开始是向右,需要判断map[0][1]的值,因此
up[i][0] = up[i - 1][0] + (map[i][0] == '.' ? 0 : 1) + num; 其中num = map[0][1] == '.' ? 1 : 0;
当i>0,j>0
up[i][j]的取值可以的从map[i-1][j]继续向下(取值为up[i - 1][j]),或者在map[i-1][j]向右遇到b转向(b可以是原始的b或者是由'.'转化为b)。同时需要判断map[i][j]的值
关键代码
up:
left:
完整代码
#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 wayout.
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
动态规划题
首先根据n,m值初始化输入数组map。同时为了处理方便在输入时,在map的最右边和最下边添加了一行和一列'b',原因是后面判断(i,j)值时候需要根据(i-1,j+1)或(i+1,j-1)的取值
因为到达(i,j)的方式可以是从(i-1,j)向下到达,或者从(i,j-1)向右到达,因此用了两个辅助数组up和left,up[i][j]表示从上方到达(i,j)需要变动的最小次数,left[i][j]表示从左方到达(i,j)需要变动的最小次数.
当i==0
up[0][j]=INT_MAX;表示无法从-1行向下
left[0][j]的取值由left[0][j - 1]和map[0][j]是否为'b'决定,因此left[0][j] = left[0][j - 1] + (map[0][j] == '.' ? 0 : 1)
当j==0
left[i][0]=INT_MAX;表示无法从-1列向右
up[i][0]的取值由up[i - 1][0]和map[i][0]是否为'b'决定,同时,因为一开始是向右,需要判断map[0][1]的值,因此
up[i][0] = up[i - 1][0] + (map[i][0] == '.' ? 0 : 1) + num; 其中num = map[0][1] == '.' ? 1 : 0;
当i>0,j>0
up[i][j]的取值可以的从map[i-1][j]继续向下(取值为up[i - 1][j]),或者在map[i-1][j]向右遇到b转向(b可以是原始的b或者是由'.'转化为b)。同时需要判断map[i][j]的值
关键代码
num = map[i][j] == '.' ? 0 : 1; up[i][j] = min(up[i - 1][j], left[i - 1][j] + (map[i - 1][j + 1] == '.' ? 1 : 0)) + num; left[i][j] = min(left[i][j - 1], up[i][j - 1] + (map[i + 1][j - 1] == '.' ? 1 : 0)) + num;
up:
left:
完整代码
#include <vector>
#include <iostream>
using namespace std;
int main()
{
const int INT_MAX=2147483647;
int n, m, num;
cin >> n >> m;
vector<vector<char>> map;
vector<vector<int>> up, left;
for (int i = 0; i < n; i++)
{
vector<char> vec(m + 1);
for (int j = 0; j < m; j++)
cin >> vec[j];
vec[m] = 'b';
map.push_back(vec);
vector<int> vec2(m);
up.push_back(vec2);
left.push_back(vec2);
}
vector<char> vec3(m + 1, 'b');
map.push_back(vec3);
left[0][0] = up[0][0] = (map[0][0] == '.' ? 0 : 1);
for (int j = 1; j < m; j++)//初始化第一行
{
up[0][j] = INT_MAX;
left[0][j] = left[0][j - 1] + (map[0][j] == '.' ? 0 : 1);
}
num = map[0][1] == '.' ? 1 : 0;
for (int i = 1; i < n; i++)//初始化第一列
{
left[i][0] = INT_MAX;
up[i][0] = up[i - 1][0] + (map[i][0] == '.' ? 0 : 1) + num;
}
for (int i = 1; i < n; i++)
{
for (int j = 1; j < m; j++)
{
num = map[i][j] == '.' ? 0 : 1; up[i][j] = min(up[i - 1][j], left[i - 1][j] + (map[i - 1][j + 1] == '.' ? 1 : 0)) + num; left[i][j] = min(left[i][j - 1], up[i][j - 1] + (map[i + 1][j - 1] == '.' ? 1 : 0)) + num;
}
}
cout << min(up[n - 1][m - 1], left[n - 1][m - 1]);
return 0;
}
相关文章推荐
- 微软无线镭射简报鲨8000激光笔记本鼠标 - (2)
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- 对《大家都在点赞 Windows Terminal,我决定给你泼一盆冷水》一文的商榷
- 微软镜像下载
- 微软公布2013年必应搜索十大首页美图
- 微软Word 2007数学插件 Microsoft Math 提供下载
- 巧用微软EWF来保护系统
- 微软又爆预留后门 警方可轻松获取硬盘资料
- 微软推DreamSpark计划为学生提供免费软件下载地址
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客
- 月光微博客