Transmission Door
#1290: Demo Day time limit: 10000ms single point limit: 1000ms memory limit: 256MB description
Intern at a robotics startup. Today is your company's demo day. During the demo your company ' s robot'll be put in a maze and without any information about the maze, it should be able t O Find A is out.
The maze consists of N * M grids. Each of the grids is either empty (represented by '. ') or blocked by a obstacle (represented by ' B '). The robot'll be release on the top left corner and the exit are 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-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.
RRRRBB. ..... R .... ====> the robot route with broken sensors was marked by ' R '. ..... rrb.....bb ...
While the FTEs (full-time employees) is busy working on the sensors, you try-to-Save the demo day by rearranging the maze In such a-a-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 is arouse suspision, you 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 be changed.
-
Sample input
-
4 8....bb...............b.....bb ...
-
Sample output
-
1
1290 |
Demo Day |
AC |
g++ |
0ms |
0MB |
1 minutes ago |
View |
Exercises
Transferred from: http://www.cnblogs.com/acSzz/p/5362865.html
The meaning of the title is:
Give you a matrix by '. ' and ' B ' composition '. ' Said can go, ' B ' means the obstacle, the robot can only go to the right until it can not walk (encounter obstacles or head) and go down until you can not go, two directions, starting point is the upper left corner, the exit is the lower right corner, for the robot
Can walk to the exit, you can put '. ' To ' B ', you can also change ' B ' to '. ', in order to make the robot go to the exit, the minimum number of transformations.
(The robot starts from the beginning and goes right, remember!!!!!!! )
Ideas:
Should be a dynamic planning, for each point, to reach this point can only be through the top point, or from the left point to reach the point, then for each point, the first thought is to use f[i][j], the arrival (i,j) need to change the minimum number of times, but found that the arrival of each point is a direction
, and if only use f[i][j], indicating the minimum number of changes to arrive (I,J), do not know the direction of the word is not able to push the next point, so,
We use UP[I][J] to indicate the minimum number of changes required to reach the point from above, and r[i][j] to indicate the minimum number of changes required to reach that point from the left.
For Up[i][j], can be reached from the left (I-1, J), and then arrived (I,j), but at this time (i-1, j+1) must be ' B ' or the boundary, can also be reached (I-1,J) from above (I,J);
Also is if (I,J) itself is ' b ' words, then need up[i][j]+1;
UP[I-1][J]
Up[i][j] = min
R[I-1][J] + 1 (if (i-1,j) is not ' B ' or the boundary requires + 1);
In the same vein, you get:
Up[i][j-1] (+1) (if (i+1,j-1) is not ' B ' or boundary, then + 1 is required)
R[i][j] = min
R[I][J-1]
In order to handle the convenience at the input, the right and bottom of the matrix added a row, a column of ' B ';
Code:
Core Transfer equation:
intdp[n][n][3];//0xia,1youintCheckxia (intXinty) { if(x = = N)return 0; if(s[x+1][y] = ='b')return 0; Else return 1;}intCheckyou (intXinty) { if(y = = m)return 0; if(s[x][y+1] =='b')return 0; Else return 1;} Fxia=Checkxia (I,J); Fyou=checkyou (I,J); dp[i][j][0] + = min (dp[i-1][j][0],dp[i][j-1][1] +fyou); dp[i][j][1] + = min (dp[i][j-1][1],dp[i-1][j][0] + Fxia);
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <string>6#include <cstring>7#include <vector>8 9 #defineN 105Ten #definell Long Long One #defineINF 0X3FFFFFFF A - using namespacestd; - the intn,m; - intdp[n][n][3];//0xia,1you - CharS[n][n]; - + intCheckxia (intXinty) - { + if(x = = N)return 0; A if(s[x+1][y] = ='b')return 0; at Else return 1; - } - - intCheckyou (intXinty) - { - if(y = = m)return 0; in if(s[x][y+1] =='b')return 0; - Else return 1; to } + - intMain () the { * //freopen ("In.txt", "R", stdin); $ inti,j,k;Panax Notoginseng intfxia,fyou; - while(SCANF ("%d%d", &n,&m)! =EOF) the { + for(i=0; i<=n+1; i++){ A for(j=0; j<=m+1; j + +){ the for(k=0; k<=1; k++){ +dp[i][j][k]=inf; - } $ } $ } - for(i =1; I <= N; i++){ -scanf"%s", s[i]+1); the } - for(i =1; I <= n;i++){Wuyi for(j =1; J <= m;j++){ theFxia =Checkxia (i,j); -Fyou =checkyou (i,j); Wu for(k =0; K <=1; k++){ -DP[I][J][K] =0; About if(S[i][j] = ='b'){ $DP[I][J][K] =1; - } - } - if(i = =1&& j==1){ Adp[i][j][0] = dp[i][j][1] +fyou; + Continue; the } -dp[i][j][0] + = min (dp[i-1][j][0],dp[i][j-1][1] +fyou); $dp[i][j][1] + = min (dp[i][j-1][1],dp[i-1][j][0] +fxia); the } the } the /* the For (i = 1;i <= n;i++) { - For (j = 1;j <= m;j++) { in the For (k = 0; k <= 1;k++) { the printf ("I =%d j=%d k=%d dp=%d\n", I,j,k,dp[i][j][k]); About } the the } the }*/ +printf"%d\n", Min (dp[n][m][0],dp[n][m][1])); - } the return 0;Bayi}
Hihocoder #1290: Demo Day