https://www.codechef.com/problems/LEMOUSE


题意:

有一个n *m的网格。有一头大象,初始时在(1,1),要移动到(n,m),每
次只能向右或者向下走。有些格子中有老鼠,如果大象所在的格子和某个有老
鼠的格子的曼哈顿距离$\e$1,大象就会被那只老鼠吓到。
求一条移动路径,使得吓到过大象的老鼠数量最少。n;m $\le$100。


开始刷济南集训hzc的课件啦!

最简单的一道$DP$

一只老鼠只会吓大象一次$f[i][j][0/1]$记录从左还是上走来的,转移时讨论一下就行了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N=,INF=1e9+;
double eps=1e-;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int m,n,a[N][N],s[N][N];
char str[N];
int f[N][N][];
void dp(){
f[][][]=f[][][]=s[][];
for(int i=;i<=m;i++)
for(int j=;j<=n;j++){
if(i==&&j==) continue;
if(j-!=){
int t1=INF,t2=INF;
if(j-!=||i==)
t1=f[i][j-][];
if(i-!=)
t2=f[i][j-][]-a[i-][j];
f[i][j][]=min(t1,t2)+s[i][j]-a[i][j-]-a[i][j];
}
if(i-!=){
int t1=INF,t2=INF;
if(i-!=||j==)
t1=f[i-][j][];
if(j-!=)
t2=f[i-][j][]-a[i][j-];
f[i][j][]=min(t1,t2)+s[i][j]-a[i-][j]-a[i][j];
}
}
//for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) printf("f %d %d %d %d\n",i,j,f[i][j][0],f[i][j][1]);
//for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) printf("%d%c",f[i][j][0],j==n?'\n':' ');puts("");
//for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) printf("%d%c",f[i][j][1],j==n?'\n':' ');puts(""); printf("%d\n",min(f[m][n][],f[m][n][]));
}
int main(){
freopen("in","r",stdin);
int T=read();
while(T--){
m=read();n=read();
memset(s,,sizeof(s));
for(int i=;i<=m;i++){
scanf("%s",str+);
for(int j=;j<=n;j++){
a[i][j]=str[j]-'';
if(a[i][j]) s[i][j]++,s[i-][j]++,s[i+][j]++,s[i][j-]++,s[i][j+]++;
}
}
//for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) printf("%d%c",a[i][j],j==n?'\n':' ');puts("");
//for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) printf("%d%c",s[i][j],j==n?'\n':' ');puts(""); dp();
}
}

CodeChef Little Elephant and Mouses [DP]的更多相关文章

  1. CodeChef Little Elephant and Movies [DP 排列]

    https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2.. ...

  2. CodeChef Cards, bags and coins [DP 泛型背包]

    https://www.codechef.com/problems/ANUCBC n个数字,选出其一个子集.求有多少子集满足其中数字之和是m的倍数.n $\le$ 100000,m $\le$ 100 ...

  3. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

  4. CF 258B Little Elephant and Elections [dp+组合]

    给出1,2,3...m 任取7个互不同样的数a1,a2,a3,a4,a5,a6,a7 一个数的幸运度是数位上4或7的个数 比方244.470幸运度是2. 44434,7276727.4747,7474 ...

  5. codechef Little Elephant and Permutations题解

    The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...

  6. codechef Little Elephant and Bombs题解

    The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...

  7. CodeChef Little Elephant and Balance

    Given an array A1,A2...AN, you have to print the size of the largest contiguous subarray such that L ...

  8. CodeChef:Little Elephant and Colored Coins

    类似墨墨的等式 设f[2][j][k]表示a[i].c是否和当前颜色相同,到当前枚举到的颜色为止,颜色数为j,对mnv取模为k的最小数 这是个无限循环背包,用spfa优化 #include<cs ...

  9. CTSC 2018酱油记

    Day0 5.5 花了一上午的时间把codechef div2的前四题切了,又在zbq老司机的指导下把第五题切了 中午12:00 gryz电竞组从机房出发,临走的时候看到很多学长挺恋恋不舍的,毕竟可能 ...

随机推荐

  1. JFinal极速开发框架使用笔记(三) 分析Model和ActiveRecord

    JFinal框架的一些新发现的用法: 在JFinal框架中,实体类并不需要设置属性,更不需要配置getset方法就可以很方便的操作数据库,如果需要设置或者获取属性,可以直接使用一下方式: User u ...

  2. Result Maps collection does not contain value for com.man.impet.dao.OrderBeanMapper.map

    由于mapper.xml中resultMap = "map"  改为 resultType="map"即可,折腾了一下午

  3. 使用npm install报错-4048 operation not permitted解决

    刚刚使用npm install时一直报错-4048 operation not permitted,也尝试了多种方法,终于使问题得到解决,这里总结几种方法,先贴图: 一:权限问题 首先看到operat ...

  4. Eclipse安装JD-Eclipse反编译插件成功看源码

    Eclipse安装JD-Eclipse反编译插件 转载 2017年12月24日 15:19:27   http://heavengate.blog.163.com/blog/static/202381 ...

  5. ceil与intval区别

    float ceil(float value)ceil返回不小于value的最小整数,返回值仍是float型 int intval ( mixed value [, int base])    int ...

  6. putty 与winscp 区别

    https://zhidao.baidu.com/question/377968180.html putty 与winscp 有什么区别, 装了 winscp 可以由 putty 替换么 ? 具体用法 ...

  7. 从#65279字符看dede模板页面编码问题

    今天一位朋友让帮忙给解决一个dede模板的问题,问题主要是:模板文件生成html文件之后会在body开头处加入一个可见的控制符&#65279,导致页面头部会出现一个空白行. 接到"& ...

  8. jquery自定义进度条与h5原生进度条

      介绍一款自定义的进度条 <div class="box-nine"> <div class="progress"> <!--一 ...

  9. linux_ssh

    什么是ssh? 配置文件位置:/etc/ssh/sshd_config 远程登录和为其他网络提供安全的加密数据传输协议,默认端口22,默认协议是SSH2 # 远程终端通过ssh连接服务器管理服务器 # ...

  10. linkin大话面向对象--组合

    继承是实现类重用的重要手段,但是它有一个最大的坏处:破坏封装.相比之下,组合也是实现类重用的重要方式,但是采用组合方式实现类重用则能提供更好的封装性.比如人有手一样,在人的类中增加一个手的属性. 何时 ...