微软2016校园招聘4月:hihocoder- #1290 : Demo Day

http://hihocoder.com/problemset/problem/1290?sid=811882


#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


动态规划题

首先根据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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值