Codeforces 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的更多相关文章
- Codeforces 374 C Inna and Dima (DFS)
Inna and Dima 题意:从图上的任意一个D点按着DIMADIMA的顺序走,问一共可以经过多少个DIMA,如果经过0个DIMA就输出“Pool DIma!“,如果可以有无数多个DIMA就输出” ...
- cf C. Inna and Dima
http://codeforces.com/contest/374/problem/C 记忆化搜索,题意:求按照要求可以记过名字多少次,如果次数为无穷大,输出Poor Inna!,如果不经过一次输出P ...
- codeforces 374A Inna and Pink Pony 解题报告
题目链接:http://codeforces.com/problemset/problem/374/A 题目意思:给出一个 n 行 m 列 的棋盘,要将放置在坐标点为(i, j)的 candy 移动 ...
- Codeforces 374A - Inna and Pink Pony
原题地址:http://codeforces.com/contest/374/problem/A 好久没写题目总结了,最近状态十分不好,无论是写程序还是写作业还是精神面貌……NOIP挂了之后总觉得缺乏 ...
- cf374C Inna and Dima dfs判环+求最长链
题目大意是有一个DIMA四种字母组成的矩阵,要在矩阵中找最长的DIMADIMADIMA……串,连接方式为四方向连接,问最长能找到多少DIMA.字母可以重复访问,如果DIMA串成环,即可以取出无限长的D ...
- CodeForces 400A Inna and Choose Options
Inna and Choose Options Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on Cod ...
- Codeforces I. Inna and Nine(组合)
题目描述: Inna and Nine time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 【题解】CF374C Inna and Dima
题面传送门 解决思路 本题是找最长路的图上问题,所以先考虑如何建图. 首先把每一个字母转化为数字,然后对于每一个点枚举四个方向,如果有下一个字母,就向那个点建一条边,可以用 \(vector\) 存图 ...
- codeforces 499A.Inna and Pink Pony 解题报告
题目链接:http://codeforces.com/problemset/problem/499/A 题目意思:有两种按钮:1.如果当前观看的时间是 t,player 可以自动处理下一分钟,姑且理解 ...
随机推荐
- P4890 Never·island(dp)
P4890 Never·island 求门开的最小时间,其实也就是求门关的最大时间. 坐标这么大....显然坐标要离散化 离散化排序后,我们发现x轴被这些点划分成若干条线段$(l,r)$,并且有4种情 ...
- JavaWeb中的资源映射
一./与/* <url-pattern>/</url-pattern> 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url< url- ...
- Bayesian Program Synthesis - 初步探索
- rman备份例子
1.全备份例子 #!/bin/sh RMAN_OUTPUT_LOG=/home/oracle/rman_output.logRMAN_ERROR_LOG=/home/oracle/rman_error ...
- 配置maven默认jdk版本
1.在setting.xml中配置.对所有通过该配置文件构建的maven项目有效. <profile> <id>jdk-1.8</id> <activatio ...
- topcoder srm 305 div1
problem1 link 直接按照题意模拟即可. import java.util.*; import java.math.*; import static java.lang.Math.*; pu ...
- Spring <import>标签配置
使用情景:在Maven项目中,我们在Spring 配置文件中需要用到<import resource="">标签来引入其他配置文件,这里我要记下一些注意事项 情景1 & ...
- Python3基础 list append 向尾部添加一个元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 【做题】agc003E - Sequential operations on Sequence——经典结论
题意:有一个序列,初始是从\(1\)到\(n\)的\(n\)个数.有\(q\)次操作,每次操作给出\(q_i\),把当前的序列重复无数遍,然后截取最前面的\(q_i\)个元素作为新序列.要求输出完成所 ...
- html 之 padding,margin
margin:对象挤压外界 padding:对象挤压自身 例如: td使用margin 对table而言没有任何效果,但使用padding是对table内部的挤压,若是table空间不够,则会扩大ta ...