hihocoder #1290 : Demo Day
#1290 : Demo Day
描述
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
| 1290 | Demo Day | AC | G++ | 0ms | 0MB | 1分钟前 | 查看 |
题解:
转自:http://www.cnblogs.com/acSzz/p/5362865.html
题目的意思是:
给你一个矩阵由‘.’和'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'; 代码: 核心转移方程:
int dp[N][N][]; //0xia,1you
int checkxia(int x,int y)
{
if(x == n) return ;
if(s[x+][y] == 'b') return ;
else return ;
} int checkyou(int x,int y)
{
if(y == m) return ;
if(s[x][y+] == 'b') return ;
else return ;
}
fxia = checkxia(i,j);
fyou = checkyou(i,j); dp[i][j][] += min(dp[i-][j][],dp[i][j-][] + fyou);
dp[i][j][] += min(dp[i][j-][],dp[i-][j][] + fxia);
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector> #define N 105
#define ll long long
#define inf 0x3fffffff using namespace std; int n,m;
int dp[N][N][]; //0xia,1you
char s[N][N]; int checkxia(int x,int y)
{
if(x == n) return ;
if(s[x+][y] == 'b') return ;
else return ;
} int checkyou(int x,int y)
{
if(y == m) return ;
if(s[x][y+] == 'b') return ;
else return ;
} int main()
{
//freopen("in.txt","r",stdin);
int i,j,k;
int fxia,fyou;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=;i<=n+;i++){
for(j=;j<=m+;j++){
for(k=;k<=;k++){
dp[i][j][k]=inf;
}
}
}
for(i = ;i <= n ;i++){
scanf("%s",s[i]+);
}
for(i = ;i <= n;i++){
for(j = ;j <= m;j++){
fxia = checkxia(i,j);
fyou = checkyou(i,j);
for(k = ;k <= ;k++){
dp[i][j][k] = ;
if(s[i][j] == 'b'){
dp[i][j][k] = ;
}
}
if(i == && j== ){
dp[i][j][] = dp[i][j][] + fyou;
continue;
}
dp[i][j][] += min(dp[i-][j][],dp[i][j-][] + fyou);
dp[i][j][] += min(dp[i][j-][],dp[i-][j][] + fxia);
}
}
/*
for(i = 1;i <= n;i++){
for(j = 1;j <= m;j++){ for(k = 0 ;k <= 1;k++){
printf(" i =%d j=%d k=%d dp=%d\n",i,j,k,dp[i][j][k]);
} }
}*/
printf("%d\n",min(dp[n][m][],dp[n][m][]));
}
return ;
}
hihocoder #1290 : Demo Day的更多相关文章
- hihocoder #1290 : Demo Day (2016微软编程测试第三题)
#1290 : Demo Day 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You work as an intern at a robotics startup. ...
- ACM学习历程—Hihocoder 1290 Demo Day(动态规划)
http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...
- 【hihocoder】Demo Day
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You work as an intern at a robotics startup. Today is your co ...
- hihoCoder Demo Day dp
题意:有一个机器人被困在一个的迷宫中,机器人的初始位置是,目的地是,并且它的移动方式很奇怪:只能一直向右,直到不能再向右才能把方向变成向下:只能一直向下,直到不能再向下才能把方向变成向右.迷宫中的每个 ...
- BZOJ1695 : [Usaco2007 Demo]Walk the Talk
观察单词表可以发现: 对于长度为3的单词,前两个字母相同的单词不超过7个 对于长度为4的单词,前两个字母相同的单词不超过35个 于是首先$O(26*26*nm)$预处理出 s1[x][i][j]表示( ...
- Echarts图表常用功能配置,Demo示例
先看下效果图: 就如上图所示,都是些常用的基本配置. Legend分页,X轴设置,Y轴设置,底部缩放条设置, 数值显示样式设置,工具箱设置,自定义工具按钮, 绑定点击事件等等.这些配置代码中都做了简单 ...
- Mysql错误积累001-load data导入文件数据出现1290错误
错误出现情景 在cmd中使用mysql命令,学生信息表添加数据.使用load data方式简单批量导入数据. 准备好文本数据: xueshengxinxi.txt 文件 数据之间以tab键进行分割 ...
- 通过一个demo了解Redux
TodoList小demo 效果展示 项目地址 (单向)数据流 数据流是我们的行为与响应的抽象:使用数据流能帮我们明确了行为对应的响应,这和react的状态可预测的思想是不谋而合的. 常见的数据流框架 ...
- 很多人很想知道怎么扫一扫二维码就能打开网站,就能添加联系人,就能链接wifi,今天说下这些格式,明天做个demo
有些功能部分手机不能使用,网站,通讯录,wifi基本上每个手机都可以使用. 在看之前你可以扫一扫下面几个二维码先看看效果: 1.二维码生成 网址 (URL) 包含网址的 二维码生成 是大家平时最常接触 ...
随机推荐
- dubbo 分布式治理
1. dubbo 仅支持java语言之间的通讯. 2. dubbo 产生的背景是因为分布式的情况下产生的. 3. 解决服务治理的问题,通过注册中心进行管理 4.SOA 治理方案 tcp 协议传输,只需 ...
- 理解Vue
Vue.js是JavaScript MVVM(Model-View-ViewModel)库,十分简洁,Vue核心只关注视图层,相对AngularJS提供更加简洁.易于理解的API.Vue尽可能通过简单 ...
- Synchronized关键字整理
Synchronized关键字整理 作用:能够保证在同一时刻最多只有一个线程执行该段代码,以达到保证并发安全效果. 两个用法: 1.对象锁: 包括方法锁(默认锁对象为this当前实例对象)和同步代码块 ...
- python常用模块之json和pickle模块
json模块 json.dumps 将 Python 对象编码成 JSON 字符串 json.loads 用于解码 JSON 数据.该函数返回 Python 字段的数据类型. pi ...
- IIS应用程序池"启用32位"导致服务不可用的503错误
原来运行正常的站点,突然不正常了,出现503错误.查看操作系统的日志查看器显示: 由于配置问题,无法加载模块 DLL“C:\Program Files (x86)\IIS\Asp.Net Core M ...
- maven项目创建(eclipse配置
Eclipse相关配置: eclipse 设置默认编码为Utf-8 需要设置的几处地方为: Window --> Preferences --> General --> Conten ...
- fclose - 关闭流
SYNOPSIS 总览 #include <stdio.h> int fclose(FILE *stream); DESCRIPTION 描述 函数 fclose 将名为 stream 的 ...
- lucene4.7实例详解
java.lang.UnsupportedClassVersionError: org/apache/lucene/index/IndexableField : Unsupported major.m ...
- SSH中的jar包讲解
我们在搭建SSH框架的时候,需要引入各自的一些jar包 首先,先来看一下我们使用的SSH的各自版本及引入的jar包. struts2.3.1.2: struts2-core-2.3.1.jar j ...
- CSS - position属性小结
Relative: 属于文档流,针对自身进行偏移: Absolute: 脱离文档流,针对最近的定位元素进行偏移,如果没有,则针对根元素,即body标签尽心偏移: Fixed: 和absolute基本一 ...