Demo Day time limit: 10000mssingle point time limit: 1000msmemory limit: 256MBDescribe
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
#include <cstdio>#include<iostream>#include<cstring>#include<string>#defineINF 0x3f3f3f3fusing namespacestd;Charmap[ the][ the];intdp[ the][ the];intN, M;intFintAintb) { if(Dp[a][b]! =-1)returnDp[a][b]; intcur = INF, tot=map[a][b]=='b'; for(inti=b-1; i>=0; i--) {cur= min (cur, f (A, I) + tot + (a<n-1&& map[a+1][i]!='b')); if(map[a][i]=='b') {tot++; }} tot=map[a][b]=='b'; for(inti=a-1; i>=0; i--) {cur= min (cur, f (i, B) + tot + (b<m-1&& map[i][b+1]!='b')); if(map[i][b]=='b') {tot++; } } returnDP[A][B] =cur;}intMain () {scanf ("%d%d", &n, &m); for(intI=0; i<n; i++) {scanf ("%s", Map[i]); } memset (DP,-1,sizeof(DP)); inttot=0; for(intj=0; j<m; J + +) { if(map[0][j]=='b') tot++; dp[0][J] =tot; } printf ("%d\n", F (n1, M-1)); //for (int i=0; i<n; i++) {//for (int j=0; j<m; J + +) {//printf ("%d", f (i,j));// }//printf ("\ n");// } return 0;}
Hiho 1290--demo Day "DP"