码迷,mamicode.com
首页 > 其他好文 > 详细

hiho 1290 —— Demo Day 【DP】

时间:2016-04-26 14:04:45      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

时间限制: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
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#define INF 0x3f3f3f3f
using namespace std;

char map[105][105];
int dp[105][105];
int n, m;

int f(int a, int b)
{
    if(dp[a][b] != -1)    return dp[a][b];
    
    int cur = INF, tot=map[a][b]==b;
    
    for(int i=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(int i=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++;
        }
    }
    return dp[a][b] = cur;
}

int main ()
{
    scanf("%d%d", &n, &m);
    
    for(int i=0; i<n; i++) {
        scanf("%s", map[i]);
    }
    
    memset(dp, -1, sizeof(dp));
    
    int tot=0;
    for(int j=0; j<m; j++) {
        if(map[0][j]==b)    tot++;
        dp[0][j] = tot;
    }
    
    printf("%d\n", f(n-1, 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】

标签:

原文地址:http://www.cnblogs.com/AcIsFun/p/5434745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!