zoukankan      html  css  js  c++  java
  • hihocoder #1290 : Demo Day (2016微软编程测试第三题)

    #1290 : Demo Day

    时间限制: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

    题目的意思是:
    给你一个矩阵由‘.’和'b'组成 ‘.’表示可以走,'b'表示障碍物,机器人只能往右走直到不能走(碰到障碍物或者到头)和往下走直到不能走,两个方向,起始点是左上角,出口是右下角,为了机器人
    能够走到出口,你可以将'.'变为'b', 也可以将'b'变为'.',,求为了使机器人走到出口,最少的变换次数。
    (机器人从起点,一开始是往右走的,记住!!!!!!!)

    思路:
    应该算是动态规划吧,对于每一个点,到达这个点只能是经过上方点,或者从左方点到达该点,那么对于每一个点,一开始想的是用f[i][j],表示到达(i,j)需要变动的最小次数,但是发现到达每一个点是有方向的
    ,而且如果只用f[i][j],表示到达(i,j)的最小变动次数,不知道方向的话是无法递推下一个点的,所以,
    我们用up[i][j],表示从上方到达该点需要变动的最小次数,r[i][j]表示从左方到达该点需要变动的最小次数,
      (i-1,j) (i - 1,j+1)
      (i,j)  
     对于up[i][j],可以是从左方到达(i-1, j),再到达(i,j),但此时(i-1, j+1)必须为'b'或者边界,也可以从上方到达(i-1,j)再到达(i,j);
    还有就是如果(i,j)本身为'b'的话,则需要up[i][j]+1;

    up[i-1][j]
    up[i][j] = min
    r[i-1][j] + 1(如果(i-1,j)不为‘b’或者边界则需要+1);


    同理可得:

    up[i][j-1] (+1) (如果(i+1,j-1)不为'b'或者边界,则需要+1)
    r[i][j] = min
    r[i][j-1]

    为了处理方便在输入时,在矩阵的最右边和最下边添加了一行,一列'b';
     
     
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string>
     4 #include <string.h>
     5 #include <algorithm>
     6 #include <vector>
     7 #define maxn 110
     8 #define inf 9999999
     9 using namespace std;
    10 int n, m;
    11 int r[maxn][maxn];
    12 int up[maxn][maxn];
    13 char str[maxn][maxn];
    14 int GetUp(int i, int j)
    15 {
    16     int x = r[i - 1][j];
    17     int y = up[i - 1][j];
    18 
    19     if(str[i-1][j+1]!= 'b')x++;
    20     int ans = min(x,y);
    21     return ans;
    22 
    23 }
    24 int GetR(int i, int j)
    25 {
    26     int x = r[i][j - 1];
    27     int y = up[i][j - 1];
    28     if(str[i+1][j - 1]!='b')y++;
    29     return min(x,y);
    30 }
    31 int main()
    32 {
    33    while(scanf("%d%d", &n,&m)!=EOF)
    34    {
    35        for(int i = 0 ; i < n;i++)
    36        {
    37            scanf("%s", str[i]);
    38            int len = strlen(str[i]);
    39            str[i][len] = 'b';
    40            str[i][len+1] = '';
    41        }
    42 
    43 
    44 
    45        for(int i = 0 ; i <= m; i++)
    46        {
    47            str[n][i] = 'b';
    48        }
    49        //初始化第一行
    50        int cnt = 0;
    51        for(int j = 0 ; j < m;j++)
    52        {
    53            if(str[0][j] == 'b')
    54            {
    55                ++cnt;
    56 
    57            }
    58            r[0][j] = cnt;
    59            up[0][j] = inf;
    60        }
    61 
    62        cnt = 0;
    63        // 初始化第一列
    64        // 一开始往右走,所以要判断一下
    65        if(str[0][1]!='b')++cnt;
    66 
    67        for(int i = 0 ; i < n; i++)
    68        {
    69            if(str[i][0] == 'b')
    70            {
    71               ++cnt;
    72 
    73            }
    74            up[i][0] = cnt;
    75            r[i][0] = inf;
    76        }
    77 
    78        for(int i = 1 ; i < n;i++)
    79        {
    80            for(int j = 1;j < m;j++)
    81            {
    82                up[i][j] = GetUp(i,j);
    83                r[i][j] = GetR(i, j);
    84                if(str[i][j] == 'b')
    85                {
    86                    up[i][j]++;
    87                    r[i][j]++;
    88                }
    89            }
    90        }
    91 
    92 
    93 
    94        printf("%d
    ", min(up[n - 1][m - 1], r[n - 1][m - 1]));
    95 
    96    }
    97 }
    View Code











  • 相关阅读:
    解决MySql报错:1130
    Mac 安装 RabbitMQ 出现的问题
    Java 中的关键字
    ExtJS 去除水印
    MAC VMware fusion 12.1.0 Centos7 网络配置
    Linux常见目录结构
    CentOS 设置网络及安装 ifconfig
    Web项目部署到IIS的方法
    异常处理-System.IO.IOException: 由于远程方已关闭传输流,身份验证失败
    C#委托总结-匿名方法&Lambda表达式
  • 原文地址:https://www.cnblogs.com/acSzz/p/5362865.html
Copyright © 2011-2022 走看看