CodeChef Little Elephant and Mouses [DP]
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]的更多相关文章
- CodeChef Little Elephant and Movies [DP 排列]
https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2.. ...
- CodeChef Cards, bags and coins [DP 泛型背包]
https://www.codechef.com/problems/ANUCBC n个数字,选出其一个子集.求有多少子集满足其中数字之和是m的倍数.n $\le$ 100000,m $\le$ 100 ...
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
- 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 ...
- codechef Little Elephant and Permutations题解
The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...
- codechef Little Elephant and Bombs题解
The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy bui ...
- 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 ...
- CodeChef:Little Elephant and Colored Coins
类似墨墨的等式 设f[2][j][k]表示a[i].c是否和当前颜色相同,到当前枚举到的颜色为止,颜色数为j,对mnv取模为k的最小数 这是个无限循环背包,用spfa优化 #include<cs ...
- CTSC 2018酱油记
Day0 5.5 花了一上午的时间把codechef div2的前四题切了,又在zbq老司机的指导下把第五题切了 中午12:00 gryz电竞组从机房出发,临走的时候看到很多学长挺恋恋不舍的,毕竟可能 ...
随机推荐
- [国嵌攻略][119][Linux中断处理程序设计]
裸机中断: 1.中断统一入口. 2.注册中断处理程序. 3.根据中断源编号,调用中断处理程序. Linux中断 1.在entry-armv.S中的_irq_svc是中断统一入口. 2.获取产生中断源的 ...
- HDU 5538 House Building(模拟——思维)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5538 Problem Description Have you ever played the vi ...
- JS高级用法
1.重复定时器 setTimeout(function() { // 处理中 setTimeout(arguments.callee, 1000); }, 1000) 这种模式链式调用了 setTim ...
- YUI3组件框架之plugin
plugin相关源码分析: plugin功能包括如下几个模块, 简单分析如下: pluginhost-base 维护对象 this._plugins = {}: 并提供方法: plug.unplug. ...
- oracle01
01-基本的查询语句 A:一些命令 sqlplus sys/sys@192.168.22.220:1521/orcl as sysdba; conn scott/123456@192.168.22.2 ...
- banner无缝轮播【小封装】
转载:http://www.qdfuns.com/notes/23446/d1691a1edf5685396813cc85ae6ab10f.html 一直在重复的写banner,写了了好几个,然后每次 ...
- JavaScript return 最简单解释
一.return 返回值 1)函数名字 +括号 :fun() ==> retrun 后面的值 2)所以函数的模范返回值是为未定义 3)return; 后面的任何代码都不会执行了 二.arguem ...
- linux_通配符
通配符和正则表达式区别? 通配符用在用户命令行bash环境,而正则表达式用于linux三剑客(awk, sed, grep) 那,有哪些通配符? * 所有字符 五星 ls *.txt # 列举目 ...
- Windows核心编程&内核对象
1. 一个进程在初始化时,系统将会他分配一个空的句柄表,这个句柄表仅供内核对象使用,不供用户对象和GDI对象使用.进程在首次 初始化时,该句柄表为空.句柄表是一个由数据结构组成的数组,包含一个内核对象 ...
- The POM for * is invalid
The POM for yanan:jar:1.0-SNAPSHOT is invalid, transitive dependencies (if any) will not be availabl ...