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. UEP-级联下拉

    级联查询在UEP中采用动态下拉的形式,cascadeid为关键字,注意jsp页面的id的相互嵌套关系, 数据库字段的数值的设置,和动态下拉SQL语句的书写. <td align="ce ...

  2. 自己封装一个简单的ajax插件

    function myAjax(obj) { var xmlHttp; //保存xmlHttpRequest对象 var type = obj.requestType; //保存请求方式 var ca ...

  3. 程序员之殇 —— (The Beginning of the End)噩梦、崩坏

    Look at all those faces out there (当我环视周遭的一张张脸孔) We are so different(我们是如此的不同) But we have one thing ...

  4. Guake!

    快捷键及其定制: [全局快捷键] F12:显示/隐藏Guake的程序界面. [局部快捷键] Ctrl+Shift+T:新建标签页: Ctrl+Shift+W:关闭标签页: Ctrl+Shift+C:复 ...

  5. Python 3 生成手写体数字数据集

    0.引言 平时上网干啥的基本上都会接触验证码,或者在机器学习学习过程中,大家或许会接触过手写体识别/验证码识别之类问题,会用到手写体的数据集: 自己尝试写了一个生成手写体图片的python程序,在此分 ...

  6. jQuery hover() 方法

    $("p").hover(function(){ $("p").css("background-color","yellow&qu ...

  7. ESP8266重点知识

    每一个mcu对应一个库文件,所以每次上传烧写arduino都需要对应的库文件,修改起来很麻烦,不如记录下来每次的库文件,或者每次都自动生成.这样效果最佳.而且我想应该是比较稳定的, 现在比较朦胧一个问 ...

  8. 更改Patrol Agent的密码

    大家可以使用P3console去做,具体方法请见:http://wenku.baidu.com/link?url=HbSzxNV2SPrlpk_Bfmcg0CNZuAlyX4jgdp4vbrxmynv ...

  9. 流API--缩减操作

    在Stream流操作中,比如说min(),max(),count()方法,这几个操作都会将一个流缩减成一个值,流API将这些操作称为特例缩减.另外,流API同时泛华了缩减这种概念,提供了reduce( ...

  10. MyEclipse中导出javadoc文档

    1.选中要生成文档的类或者项目→File→Export→Java→Javadoc. 2.在Javadoc command中选择JDK下对应的javadoc.exe:Select types for w ...