374C - Inna and Dima

思路:dfs+记忆化搜索

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll unsigned long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int INF=0x3f3f3f3f;
char mp[][];
bool vis[][];
int dp[][];
int dir[][]={,,,,,-,-,};
int n,m;
int dfs(int x,int y){
if(dp[x][y]!=-)return dp[x][y];
dp[x][y]=;//当前路径染色,方便判环
int ans=;
bool f=false;
for(int i=;i<;i++){
int tx=x+dir[i][];
int ty=y+dir[i][];
if(x<||x>n||y<||y>m)continue;
if(mp[x][y]=='D'&&mp[tx][ty]=='I'){
if(dp[tx][ty]==)return INF;
ans=max(ans,+dfs(tx,ty));
f=true;
}
if(mp[x][y]=='I'&&mp[tx][ty]=='M'){
if(dp[tx][ty]==)return INF;
ans=max(ans,+dfs(tx,ty));
f=true;
}
if(mp[x][y]=='M'&&mp[tx][ty]=='A'){
if(dp[tx][ty]==)return INF;
ans=max(ans,+dfs(tx,ty));
f=true;
}
if(mp[x][y]=='A'&&mp[tx][ty]=='D'){
if(dp[tx][ty]==)return INF;
ans=max(ans,+dfs(tx,ty));
f=true;
}
}
if(!f)return dp[x][y]=;
else return dp[x][y]=ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie();
cin>>n>>m;
for(int i=;i<=n;i++){
cin>>(mp[i]+);
}
mem(dp,-);
int ANS=;
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mp[i][j]=='D'){
ANS=max(ANS,dfs(i,j));
}
}
}
//cout<<ANS<<endl;
if(ANS>=INF)cout<<"Poor Inna!"<<endl;
else{
if(ANS/==)cout<<"Poor Dima!"<<endl;
else cout<<ANS/<<endl;
}
return ;
}

Codeforces 374C - Inna and Dima的更多相关文章

  1. Codeforces 374 C Inna and Dima (DFS)

    Inna and Dima 题意:从图上的任意一个D点按着DIMADIMA的顺序走,问一共可以经过多少个DIMA,如果经过0个DIMA就输出“Pool DIma!“,如果可以有无数多个DIMA就输出” ...

  2. cf C. Inna and Dima

    http://codeforces.com/contest/374/problem/C 记忆化搜索,题意:求按照要求可以记过名字多少次,如果次数为无穷大,输出Poor Inna!,如果不经过一次输出P ...

  3. codeforces 374A Inna and Pink Pony 解题报告

    题目链接:http://codeforces.com/problemset/problem/374/A 题目意思:给出一个 n 行  m 列 的棋盘,要将放置在坐标点为(i, j)的 candy 移动 ...

  4. Codeforces 374A - Inna and Pink Pony

    原题地址:http://codeforces.com/contest/374/problem/A 好久没写题目总结了,最近状态十分不好,无论是写程序还是写作业还是精神面貌……NOIP挂了之后总觉得缺乏 ...

  5. cf374C Inna and Dima dfs判环+求最长链

    题目大意是有一个DIMA四种字母组成的矩阵,要在矩阵中找最长的DIMADIMADIMA……串,连接方式为四方向连接,问最长能找到多少DIMA.字母可以重复访问,如果DIMA串成环,即可以取出无限长的D ...

  6. CodeForces 400A Inna and Choose Options

    Inna and Choose Options Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on Cod ...

  7. Codeforces I. Inna and Nine(组合)

    题目描述: Inna and Nine time limit per test 1 second memory limit per test 256 megabytes input standard ...

  8. 【题解】CF374C Inna and Dima

    题面传送门 解决思路 本题是找最长路的图上问题,所以先考虑如何建图. 首先把每一个字母转化为数字,然后对于每一个点枚举四个方向,如果有下一个字母,就向那个点建一条边,可以用 \(vector\) 存图 ...

  9. codeforces 499A.Inna and Pink Pony 解题报告

    题目链接:http://codeforces.com/problemset/problem/499/A 题目意思:有两种按钮:1.如果当前观看的时间是 t,player 可以自动处理下一分钟,姑且理解 ...

随机推荐

  1. v-bind属性的绑定

    v-bind:属性绑定: 当我们并没有使用v-bind使用的时候,突破不能显示出来,会提示错误,提示我们使用v-bind: 当我们使用v-bind时图片就可以显示: v-bind的简写是冒号: 使用v ...

  2. Keil uVision4 创建51单片机工程

    Keil uVision4 创建51单片机工程 版权声明:未经授权,严禁转载! 在学习51单片机的过程当中,我们需要使用 Keil uVision4 来创建一个项目,今天就来图示一下创建的流程. 首先 ...

  3. Springbooot +Mybaties 配置数据库多数据源

    前言 在实际项目中,我们可能会碰到在一个项目中会访问多个数据库的情况.针对这种情况,我们就需要配置动态的数据源了.一般按照以下步骤即可 一.在启动类上添加注解 二.在application.prope ...

  4. 线性回归、Logistic回归、Softmax回归

    线性回归(Linear Regression) 什么是回归? 给定一些数据,{(x1,y1),(x2,y2)…(xn,yn) },x的值来预测y的值,通常地,y的值是连续的就是回归问题,y的值是离散的 ...

  5. 前缀和与差分之IncDec sequence

    参考链接:https://blog.csdn.net/hzk_cpp/article/details/80407014 题目链接:https://www.acwing.com/problem/cont ...

  6. oracle数据库数据库表空间查询及扩充

    1.查询表空间,及表空间的大小 SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size FROM dba_tabl ...

  7. ImageView控件有关问题

    了解了一下ImageView控件,这个控件本身及其属性倒没有什么特别之处.只是在看<第一行代码>时,郭大神写到创建drawable-xhdpi文件有些问题,首先先说drawable和mip ...

  8. Python3 tkinter基础 Frame bind 捕捉多键同时按

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  9. Android Java层,Native层,Lib层打印Log简介【转】

    本文转载自:https://blog.csdn.net/AndroidMage/article/details/52225068 说明: 这里我根据个人工作情况说明在各个层打印log.如有问题欢迎拍砖 ...

  10. Cmder + Babun 打造 Windows 好用的终端工具

    Babun a windows shell you will love Babun features the following: Pre-configured Cygwin with a lot o ...