#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 ...
随机推荐
- [BUUCTF][Web][GXYCTF2019]Ping Ping Ping 1
打开靶机对应URL 提示有ip参数 尝试构造url http://714ad4a2-64e2-452b-8ab9-a38df80dc584.node4.buuoj.cn:81/?ip=127.0.0. ...
- 项目实战:C#上位机+arduino下位机+控制点亮LED灯
前言 当前比较流行的arduino开发,联动做一个Demo. 应用构架 上位机:C#上位机通过串口发送接收控制协议,来控制下位机: 下位机:arduino下位机主控,接受上位机串口协议控 ...
- 【Azure 媒体服务】记使用 Media Service 的官网示例代码 Audio Analyzer 出现卡顿在 Creating event processor host .. 直到 Timeout 问题
问题描述 在使用Azure Media Service的官网示例 (media-services-v3-java --> AudioAnalytics --> AudioAnalyzer ...
- 【Azure Developer】使用 Azure Python 查看 Azure 所有的 Alert rule
问题描述 在Azure Alert 门户中,可以列举出所有Azure资源的Alert rule信息,如下图: 如果像通过Python SDK来获取所有的Alert Rule,有什么可以参考的代码吗? ...
- 用java实现书城项目(简单增删改查2)
书城项目 登录 dao 接口:UserDao Users login(String username,String password); 实现:UserDaoImpl QueryRunner quer ...
- CodeGeeX vscode代码提示,智能问答
CodeGeeX 官网 https://codegeex.cn/zh-CN/ CodeGeeX vscode代码提示,智能问答
- matting tool by cs - 视频扣绿幕 1.4 - 软件推荐
本地软件,还没用,先留个档,试试 第一个下载还套了个广告壳,醉了,但是能用 https://www.123pan.com/s/X3jA-POMQv 这个装了,是原版,没套壳 https://pan.b ...
- Pod中断预算 PodDisruptionBudget(PDB)
PodDisruptionBudget(PDB)是Kubernetes中的一个资源对象,用于确保在进行维护.升级或其他操作时,系统中的Pod不会被意外中断或终止.PDB提供了一种机制,通过限制在给定时 ...
- 基于python的opus编解码实力解析
一 opus pyogg是一个非常不错的库,用这个做音频的编码和解码非常方便. 二 源码解析 import wave from pyogg import OpusEncoder from ...
- kettle多个表一起迁移-通过配置文件配置需要同步的字段,非全字段同步
kettle多个表一起迁移-通过配置文件配置需要同步的字段,非全字段同步 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章, 关注回复「999」获取本例源文件, 免费领取全网最热的 ...