codeforces Inna and Dima:好题,记忆化搜索,dfs
来源:互联网 发布:windows.old文件夹 编辑:程序博客网 时间:2024/06/05 16:02
https://www.bnuoj.com/v3/contest_show.php?cid=8132#problem/E
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL __int64const int INF=0x3f3f3f3f;char mp[1001][1001], str[]="DIMA";int dp[1001][1001];
//i=0;i<4;i++分别为向下、上、左、右走int jx[]={0,0,1,-1};int jy[]={1,-1,0,0};int n, m;
//dfs、记忆化搜索int dfs(int x, int y, int tmp){
//记忆化搜索,如果有重复,则一定形成环,而且这时候肯定为无穷大 if(dp[x][y]!=-1) return dp[x][y]; dp[x][y]=INF;
//疑问:为什么每次t都要初始化为0?懂了,这个t等于0并不影响后面的取最优,去最优的时候t又不置0,相反,如果不置0的话找不到每次的最优解(还是不太清楚) int i, a, b, t=0;
//标记现在搜索到哪个字母,开始时传值0,后来每次传值temp,而且取模代表循环 tmp=(1+tmp)%4;
//如果D相邻有两个I,则取两个I中最大的 for(i=0;i<4;i++) { a=x+jx[i]; b=y+jy[i]; if(a>=0&&a<n&&b>=0&&b<m&&mp[a][b]==str[tmp]) {
//深度优先搜索,递归调用,直到循环节结束或形成环(循环节结束是由下面的return t,即为相邻四周都找不到,形成环是上面的记忆化搜索处返回的无穷大) t=max(t,dfs(a,b,tmp)); } }
//t只是这个字母之后的,加上1把自己也算在内 dp[x][y]=++t; return t;}int main(){ int i, j, max1=0; scanf("%d%d",&n,&m); for(i=0;i<n;i++) { scanf("%s",mp[i]); }
//初始化为-1,作为记忆化搜索的标志 memset(dp,-1,sizeof(dp));
//找出以不同的D开始的最大值 for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(mp[i][j]=='D') { max1=max(max1,dfs(i,j,0)); } } } if(max1/4==0) { puts("Poor Dima!"); } else if(max1>=INF) { puts("Poor Inna!"); } else printf("%d\n",max1/4); return 0;}
0 0
- codeforces Inna and Dima:好题,记忆化搜索,dfs
- codeforces 374C Inna and Dima (dfs+记忆化)
- Codeforces Round #220 (Div. 2)C题:Inna and Dima(记忆化搜索+DP)
- Codeforces Round #220 (Div. 2) C. Inna and Dima (记忆化搜索)
- codeforces 374C C. Inna and Dima(DFS)
- C. Inna and Dima----dfs
- codeforces Inna, Dima and Song
- C. Inna and Dima 深度搜索
- codeforces 390B Inna, Dima and Song
- Inna, Dima and Song
- Codeforces Round #229 (Div. 2) B. Inna, Dima and Song
- Codeforces Round #229 (Div. 2) B. Inna, Dima and Song
- Codeforces Round #229 (Div. 2)B. Inna, Dima and Song
- Codeforces Round #229 (Div. 2) B. Inna, Dima and Song
- Codeforces 390B Inna, Dima and Song(贪心)
- Codeforces Round #229 (Div. 2)B. Inna, Dima and Song
- Codeforces 366C Dima and Salad【01背包】好题
- 【简单题】-CF-390B-Inna, Dima and Song
- ANDROID 内存分析
- Activex签名方法和工具技巧(图)
- MATLAB函数句柄、单元数组和结构
- 51NOD 1016 水仙花数 V2(打表)
- kafka同步zookeeper前移偏移量
- codeforces Inna and Dima:好题,记忆化搜索,dfs
- JavaEE(3)
- RCNN(一):Rich feature hierarchies for accurate object detection and semantic segmentation
- SpringMVC简介
- Python学习笔记--2016.08.04
- 产生10个随机数放入数组,不允许有重复
- Web应用部署结构浅析
- Scala学习笔记(三)—chapter3
- mac下应该是一样的,ln命令