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) 包含网址的 二维码生成 是大家平时最常接触 ...
随机推荐
- java 正则表达式如何提取中文的问题
String regex="([\u4e00-\u9fa5]+)"; String str="132更新至456"; Matcher matcher = Pat ...
- PAT (Basic Level) Practise (中文)-1037. 在霍格沃茨找零钱(20)
PAT (Basic Level) Practise (中文)-1037. 在霍格沃茨找零钱(20) http://www.patest.cn/contests/pat-b-practise/1037 ...
- JdbcTemplate类对sql的操作使用
<!--方式一: dbcp 数据源配置,在测试环境使用单连接 --> <bean id="dataSource" class="org.apache.c ...
- python-DB模块实例
MySQLdb其实有点像php或asp中连接数据库的一个模式了,只是MySQLdb是针对mysql连接了接口,我们可以在python中连接MySQLdb来实现数据的各种操作. python连接mysq ...
- bootstrap历练实例:复选框或单选按钮作为输入框组的前缀或后缀
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- 在Xcode中编辑运行 Python 脚本
http://www.zhihu.com/question/19872198 打开Xcode,File->New->Project选中OS X下的Other点击External Build ...
- node爬虫(简版)
做node爬虫,首先像如何的去做这个爬虫,首先先想下思路,我这里要爬取一个页面的数据,要调取网页的数据,转换成页面格式(html+div)格式,然后提取里面独特的属性值,再把你提取的值,传送给你的页面 ...
- Ubuntu sudo 出现 is not in the sudoers file解决方案
前言: 自己想额外创建一个Linux账户,但是发现新创建的用户(lgq)并不能使用sudo指令. 但是在安装系统时创建的用户(abc)是可以正常使用的. 原因是新创建的用户并没有被赋予使用sudo指令 ...
- 如何用DOM 元素就能画出国宝熊猫
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/odKrpy 可交互视频教 ...
- python--前端CSS
一.CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义了如何显示HTML元素,给HTML设置样式,让他更加美观. 当浏览器读到这个样式表, 他就会按照这个样式来对文档进行 ...