codeforces 812b Sagheer, the Hausmeister
来源:互联网 发布:directx9安装需要网络 编辑:程序博客网 时间:2024/06/09 19:04
题目大意: 从左下角开始出发,把每一行的灯都关闭了,最左和最右是楼梯,向上只能通过楼梯,并且每一层都关完才能上一层,上楼和走过房间的时间都是一分钟,问最短时间
这道题就是一个线性的DP 考虑从左和从右两种情况就好了
一开始的时候看错题目了,把note 看成是限制条件,就做错了
另外一个就是需要仔细考虑全零行的情况
#include <bits/stdc++.h>#define inf 0x3f3f3f3fusing namespace std;int dp[20][2];string a[20];int main(){ memset(dp,inf,sizeof(dp)); int n,m; scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) { cin>>a[i]; } int le=n; int flag2=0; for(int i=1; i<=n; i++) { for(int j=0; j<a[i].size(); j++) { if(a[i][j]=='1') { le=i; flag2=1; break; } } if(flag2)break; } // cout<<le<<endl; int pos[20]; int pre[20]; memset(pre,0,sizeof(pre)); memset(pos,0,sizeof(pos)); int flag=0; for(int i=0; i<a[n].size(); i++) { if(a[n][i]=='1') flag=1; } for(int i=1; i<=n; i++) { pre[i]=m+1; for(int j=0; j<a[i].size(); j++) { if(a[i][j]=='1') pos[i]=j; } for(int k=a[i].size()-1; k>=0; k--) { if(a[i][k]=='1')pre[i]=k; } // cout<<pos[i]<<" "<<pre[i]<<endl; } dp[n][0]=pos[n]; // dp[n][1]=pos[n]; for(int i=n-1; i>=1; i--) { dp[i][1]=min(dp[i+1][0]+m+2-pos[i+1],dp[i+1][1]+m+2-pre[i+1])+m+1-pre[i]; dp[i][0]=min(dp[i+1][0]+pos[i+1]+1,dp[i+1][1]+pre[i+1]+1)+pos[i]; // if(flag) dp[n-1][0]=inf; } /* for(int i=1; i<=n; i++) { for(int j=0; j<=1; j++) { cout<<dp[i][j]<<" "; } cout<<endl; }*/ printf("%d\n",min(dp[le][0],dp[le][1])); return 0;}
阅读全文
0 0
- Codeforces 812B-Sagheer, the Hausmeister
- Codeforces 812B Sagheer, the Hausmeister[dp]
- codeforces 812 B Sagheer, the Hausmeister(枚举)
- Codeforces 812 B Sagheer, the Hausmeister
- codeforces 812B Sagheer, the Hausmeister dp
- codeforces 812b Sagheer, the Hausmeister
- codeforces Sagheer, the Hausmeister 812B
- Codeforces 812B-Sagheer, the Hausmeister
- Sagheer, the Hausmeister CodeForces
- Sagheer, the Hausmeister CodeForces
- B. Sagheer, the Hausmeister
- Codeforecs 812B Sagheer, the Hausmeister DP
- CF 812B Sagheer, the Hausmeister
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister
- CodeFroces 812B Sagheer, the Hausmeister(BFS)
- Codeforces Round #417 (Div. 2) B. Sagheer, the Hausmeister dfs深度优先搜索
- Ubuntu输入sudo cd提示找不到命令,su提示认证失败
- 复习
- 离散题目10
- HBase开发中RpcRetryingCaller Call exception问题
- Android Picasso实现圆形图片和圆角图片
- codeforces 812b Sagheer, the Hausmeister
- Linux系统内存管理
- 【脚本语言系列】关于PythonWeb服务器Bottle,你需要知道的事
- grep、sed、awk命令使用参数说明
- CSS3线性渐变与径向渐变
- BZOJ 2428 [HAOI2006]均分数据 模拟退火
- bash脚本基础(二)
- Flask成长笔记--依赖包操作
- Leetcode 332 Reconstruct Itinerary