Microsoft 2016 Intern's written test--the third demo day

Source: Internet
Author: User
Tags time limit
Microsoft 2016 Campus Recruitment April Online written test (iii) Sat April 2016 by ICTLXB Filed under C/cpp Tags CPP algorithm Hihocoder

If All you are have is a hammer, everything looks like a nail. ---Maslow C. Demo Day Problem

Time limit: 10000ms single point time limit: 1000ms memory limit: 256MB description

You are work as a intern at a robotics startup. This is your company's demo day. During the demo your company ' s robot would be put in a maze and without any information about the maze, it should to be able t o Find a way out.

The maze consists of N * M grids. Each grid are either empty (represented by '. ') or blocked by a obstacle (represented by ' B '). The robot would be a release in the top-left corner and the exit are at the bottom right corner.

Unfortunately some sensors on the robot go crazy just the demo before. As a result, the robot can only repeats two operations Alternatively:keep moving to the right until it can ' t and keep mov ing 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 was marked by ' R '. 
... rrb
. ... bb ...

While the FTEs (full-time employees) are busy working on the sensors, you are try to save the demo day by rearranging the maze In such a way that even and 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, your want to change as few grids as possible. What is the mininum number? Input

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. Output

The minimum number of grids to is changed.

Sample input

4 8 ...
BB.
........
..... b
.. ... bb ...

Sample output

1
Analysis

Think of me as a rookie, I do not expect to provide a good way to think about the problem.

I will only a little bit simple DP, see this problem is to DP rely on, did not consider there is no overlapping sub-problem, there is no optimal substructure. (This is all what.) )

Roughness fast, row equation:

Dp[i][j][k] represents the smallest flip that is required when the robot runs to the I row J column and is currently moving in the direction K. of which 0 <= i <= n,0 <= j <= m,k = Right/down

           j-1    J
//     i-1          R '  
//                  |       i    R '--?--> Right////     i+1          ?
Dp[i][j][right] = min (Dp[i][j-1][right], Dp[i-1][j][down] + (I+1 < n && maze[i+1][j]!= ' B ')) + (maze[i][j) = = ' B ');          j-1    J   j+1
//    i-1          R '  
//                 |      i    R '--?    ?
//                 |                 v
//                 down
dp[i][j][down] = min (Dp[i-1][j][down], Dp[i][j-1][right] + (J+1 < M && Maze I [j+1]!= ' B ') + (maze[i][j] = = ' B ');

With these conclusions, the code is more easily written.

#include <iostream> #include <vector> #include <algorithm> using namespace std;
	int Solve (vector<vector<char> > &maze) {const int n = maze.size ();
	const int m = Maze.front (). Size ();
	vector<vector<vector<int> > > DP (N, vector<vector<int> > (M, vector<int> (2)));
	Dp[0][0][0] = maze[0][0] = = ' B ';

	DP[0][0][1] = dp[0][0][0] + (M > 1 && maze[0][1]!= ' B '); for (int i = 1; i < n; i++) {dp[i][0][1] = min (dp[i-1][0][1], dp[i-1][0][0] + (M > 1 && maze[i-1][1]!= '
		B ') + (maze[i][0] = = ' B ');
	Dp[i][0][0] = dp[i][0][1] + (I+1 < n && maze[i+1][0]!= ' B '); for (int i = 1; i < m i++) {dp[0][i][0] = min (dp[0][i-1][0], dp[0][i-1][1] + (n > 1 && maze[1][i-1]
		!= ' B ')) + (maze[0][i] = = ' B ');
	DP[0][I][1] = dp[0][i][0] + (I+1 < M && Maze[0][i+1]!= ' B '); for (int i = 1; i < n; i++) {for (int j = 1; j < m; j) {dp[i][j][0] = min (dp[i][j-1][0], dp[i-1][j][1] + (I+1 < n && maze[i+1][j]!= ' B ')) + (maze[i][j] = = ' B ');
		Dp[i][j][1] = min (dp[i-1][j][1], dp[i][j-1][0] + (J+1 < M && Maze[i][j+1]!= ' B ')) + (maze[i][j] = = ' B ');
} return min (Dp[n-1][m-1][0], dp[n-1][m-1][1]);
	int main () {int n, m;
		while (CIN >> n >> m) {vector<vector<char> > Maze (N, vector<char> (m));
		for (int i = 0; i < n; i++) for (int j = 0; J < m; j +) Cin >> Maze[i][j];
	cout << Solve (maze) <<endl;
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.