Hiho 1290--demo Day "DP"

Source: Internet
Author: User

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"

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.