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 可以自动处理下一分钟,姑且理解 ...
随机推荐
- powermock+mockito+testng 单元测试pom文件
0:Supported versions PowerMock version 1.7.0 and upper has experimental support of Mockito 2. A lot ...
- python2.7安装mysql-python
环境:python2.7.14+mysql-python-1.2.3.win-amd64-py2.7.exe 安装版本许参照解释器版本,有64和32位之分,如果安装错误版本,则运行时会报错:Impor ...
- gnats配置文件
尽管NATS可以无配置的运行,但也可以使用配置文件配置NATS服务器 1. 配置项包括 客户端监听器端口 Client listening port HTTP监听器端口 HTTP monitoring ...
- maven单元测试报java.lang.IllegalStateException: Failed to load ApplicationContext
报这个异常java.lang.IllegalStateException: Failed to load ApplicationContext的时候,通常是因为applicationContent.x ...
- 一个简单的购物金额结算(JAVA)
我编写的代码: import java.util.Scanner; public class ZuoYe01 { public static void main(String[] args) { // ...
- 03:git常见报错解决方法
1.1 git常见报错解决方法 1.warning: LF will be replaced by CRLF in .idea/workspace.xml. 参考博客:https://www.cnbl ...
- 动态规划之91 decode ways
题目链接:https://leetcode-cn.com/problems/decode-ways/description/ 参考:https://www.jianshu.com/p/5a604070 ...
- C++进程间通信之剪贴板
转载:http://blog.csdn.net/hk627989388/article/details/53693636 转载:http://blog.csdn.net/heihei36/articl ...
- 【转】RHEL(RedHat Enterprise Linux)5/6 ISO镜像下载
本文贴出了RHEL(RedHat Enterprise Linux)发行版本中常用的服务器版本的ISO镜像文件,供大家下载学习使用,贴出的版本有RedHat Enterprise Linux(RHEL ...
- Delphi XE5 for Android (八)
delphi xe5 编译的程序在启动时会有短暂的黑屏出现,这个现象产生是因为启动首个activity时会加载一些初始化数据,整个时间大约在2~3秒,如何处理? 网上有些资料,这里主要参考和整理了CS ...