#dp#CodeChef Little Elephant and Mouses
分析
由于被单只老鼠吓到只能算一次,所以前两次走的位置也可能会被老鼠吓到。
设 \(dp[n][m][o][p]\) 表示走到 \((n,m)\) 上一步走的是 \(o\) 这种方式,再上一步走的是 \(p\) 这种方式的最小惊吓次数。
转移就直接判断一下是否作为第一次被吓到即可。
代码
#include <cstdio>
#include <cctype>
#include <cstring>
using namespace std;
const int N=111,dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
int n,m,v[N][N],dp[N][N][2][2],upd; char s[N][N];
int iut(){
int ans=0; char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans;
}
void print(int ans){
if (ans>9) print(ans/10);
putchar(ans%10+48);
}
void Min(int &x,int y){x=x<y?x:y;}
int calc(int x,int y,int zx,int zy,int Zx,int Zy){
int sum=0;
v[zx][zy]=v[Zx][Zy]=++upd;
for (int i=0;i<4;++i){
if (zx+dx[i]>0&&zy+dy[i]>0) v[zx+dx[i]][zy+dy[i]]=upd;
if (Zx+dx[i]>0&&Zy+dy[i]>0) v[Zx+dx[i]][Zy+dy[i]]=upd;
}
for (int i=0;i<4;++i)
if (s[x+dx[i]][y+dy[i]]==49&&v[x+dx[i]][y+dy[i]]!=upd)
++sum;
return sum;
}
int main(){
for (int T=iut();T;--T){
n=iut(),m=iut(),upd=0;
memset(v,0,sizeof(v));
memset(s,'\0',sizeof(s));
memset(dp,42,sizeof(dp));
for (int i=1;i<=n;++i) scanf("%s",s[i]+1);
dp[1][1][0][0]=dp[1][1][1][1]=(s[1][1]==49)+(s[1][2]==49)+(s[2][1]==49);
for (int i=2;i<=m;++i) dp[1][i][0][0]=dp[1][i-1][0][0]+(s[2][i]==49)+(s[1][i+1]==49);
for (int i=2;i<=n;++i) dp[i][1][1][1]=dp[i-1][1][1][1]+(s[i][2]==49)+(s[i+1][1]==49);
for (int i=2;i<=n;++i)
for (int j=2;j<=m;++j)
for (int o=0;o<2;++o)
for (int p=0;p<2;++p){
if (dp[i][j-1][o][p]!=dp[0][0][0][0])
Min(dp[i][j][0][o],dp[i][j-1][o][p]+calc(i,j,i,j-1,i-dx[o],j-1-dy[o]));
if (dp[i-1][j][o][p]!=dp[0][0][0][0])
Min(dp[i][j][1][o],dp[i-1][j][o][p]+calc(i,j,i-1,j,i-1-dx[o],j-dy[o]));
}
int ans=dp[0][0][0][0];
for (int o=0;o<2;++o)
for (int p=0;p<2;++p)
Min(ans,dp[n][m][o][p]);
printf("%d\n",ans);
}
return 0;
}
#dp#CodeChef Little Elephant and Mouses的更多相关文章
- CodeChef Little Elephant and Mouses [DP]
https://www.codechef.com/problems/LEMOUSE 题意: 有一个n *m的网格.有一头大象,初始时在(1,1),要移动到(n,m),每次只能向右或者向下走.有些格子中 ...
- CodeChef Little Elephant and Movies [DP 排列]
https://www.codechef.com/FEB14/problems/LEMOVIE 题意: 对于一个序列,定义其“激动值”为序列中严格大于前面所有数的元素的个数.给定n个数p1;,p2.. ...
- 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 ...
- CodeChef Cards, bags and coins [DP 泛型背包]
https://www.codechef.com/problems/ANUCBC n个数字,选出其一个子集.求有多少子集满足其中数字之和是m的倍数.n $\le$ 100000,m $\le$ 100 ...
- Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp
Little Elephant and Broken Sorting 怎么感觉这个状态好难想到啊.. dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率.转移好像比较显然. #incl ...
- CodeChef - BLACKCOM 可行性dp转最优化树dp
https://www.codechef.com/problems/BLACKCOM 题意:一颗5000个黑白结点的树,10W个查询寻找是否存在大小s并且有t和黑节点的子图 一开始就觉得应当是一个树d ...
- Codeforces Round #157 (Div. 1) B. Little Elephant and Elections 数位dp+搜索
题目链接: http://codeforces.com/problemset/problem/258/B B. Little Elephant and Elections time limit per ...
随机推荐
- Kubernetes leader election 源码分析
0. 前言 Kubernetes:kube-scheduler 源码分析 介绍了 kube-scheduler 调度 Pod 的逻辑.文中有一点未提的是,在 Kubernetes 集群中,kube-s ...
- MySQL Boolean类型的坑
MySQL中,Boolean只是 tinyint(1) 的别名,也就是说,MySQL中并没有真正的bool类型. 而SQLAlchemy生成SQL的时候并没有检测到 这一点,这就导致一个问题,当使用 ...
- day02---虚拟机上网模式
修改虚拟网络编辑器 虚拟软件网络模式介绍 NAT网络模式 特点:虚拟主机和宿主机网络信息 可以不一致 优点:不容易出现局域网中IP地址冲突 缺点:其它宿主机不能直接访问虚拟机 桥接网络模式 特点:虚拟 ...
- 老生常谈的iOS- weak原理,你真的懂得还是为了应付面试
前言 weak对于iOS开发来说只要解决一些对象相互引用的时候,避免出现强强引用,对象不能被释放,出现内存泄露的问题. weak 关键字的作用域弱引用,所引用对象的计数器不会加一,并在引用对象被释放的 ...
- 【Azure 媒体服务】记录使用Java调用Media Service API时候遇见的一些问题
问题一:java.lang.IllegalArgumentException: Parameter this.client.subscriptionId() is required and canno ...
- 【Azure 应用服务】App Service 项目部署成功后,应用连接 Azure Redis时报错 Could not get a resource from the pool
问题描述 App Service 项目部署成功后,需要连接到同在云上的Redis服务, Redis启动了专用终结点,只能在于Redis同一个VNET(虚拟网络)的资源能够访问.在进入App Servi ...
- Geospatial Data 在 Nebula Graph 中的实践
本文首发于 Nebula Graph Community 公众号 本文主要介绍了地理空间数据(Geospatial Data)以及它在 Nebula Graph 中的具体实践. Geospatial ...
- Python列表转换成字典、嵌套列表转字典、多个列表转为字典嵌套列表
目录 两列表转为字典 多列表转为字典嵌套列表 嵌套列表转字典 方法一:直接内置dict 方法二: for循环 一个列表转字典 两列表转为字典 list1=["key1"," ...
- 那些.NET中的连接池
前言 在.NET中,连接池被广泛用于管理和优化不同类型资源的连接.连接池可以减少建立和关闭连接所需的时间和资源消耗,从而提高了应用程序的性能和响应能力. HttpClient中的连接池 System. ...
- 远程服务调用(RPC与Rest本质区别)
一.背景 远程服务将计算机程序的工作范围从单机扩展到网络,从本地延伸至远程,是构建分布式系统的首要基础.远程服务调用(Remote Procedure Call,RPC)在计算机科学中已经存在了超过四 ...