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电竞组从机房出发,临走的时候看到很多学长挺恋恋不舍的,毕竟可能 ...
随机推荐
- lombok使用基础教程
前言 lombok是一个编译级别的插件,它可以在项目编译的时候生成一些代码.在很多工具类的项目中都有这个功能.比如dagger. 通俗的说,lombok可以通过注解来标示生成getter settte ...
- [国嵌笔记][011][Linux密码破解]
破解步骤 1.在系统启动时进入grub选项菜单 2.在grub选项菜单中按e进入编辑模式 3.编辑kernel行,添加 /init 1 (表示进入单用户启动模式,在单用户启动模式中不会要求输入密码) ...
- Tree Recovery(由先、中序列构建二叉树)
题目来源: http://poj.org/problem?id=2255 题目描述: Description Little Valentine liked playing with binary tr ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
- LNMP一键安装包
http://www.aliweihu.com/333.html LNMP一键安装包是什么? LNMP一键安装包是一个用Linux Shell编写的可以为CentOS/RadHat.Debian/Ub ...
- EditText之边框颜色
EditText的自带属性里没有设置边框颜色的 有俩种方式可以达到效果 一种是网上比较推崇的用图作背景,另一种则是自绘 图作背景的: 首先重新定义一个style.在values文件夹下新建一个styl ...
- [机器学习]-[数据预处理]-中心化 缩放 KNN(二)
上次我们使用精度评估得到的成绩是 61%,成绩并不理想,再使 recall 和 f1 看下成绩如何? 首先我们先了解一下 召回率和 f1. 真实结果 预测结果 预测结果 正例 反例 正例 TP 真 ...
- Reflection and array
java.lang.Reflect.Array类提供了动态创建和访问数组元素的各种静态方法. package com.sunchao.reflection; import java.lang.refl ...
- LinkedList 源码分析(JDK 1.8)
1.概述 LinkedList 是 Java 集合框架中一个重要的实现,其底层采用的双向链表结构.和 ArrayList 一样,LinkedList 也支持空值和重复值.由于 LinkedList 基 ...
- ASP.NET MVC5 中百度ueditor富文本编辑器的使用
随着网站信息发布内容越来越多,越来越重视美观,富文本编辑就是不可缺少的了,众多编辑器比较后我选了百度的ueditor富文本编辑器. 百度ueditor富文本编辑器分为两种一种是完全版的ueditor, ...