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 可以自动处理下一分钟,姑且理解 ...
随机推荐
- P3302 [SDOI2013]森林(主席树+启发式合并)
P3302 [SDOI2013]森林 主席树+启发式合并 (我以前的主席树板子是错的.......坑了我老久TAT) 第k小问题显然是主席树. 我们对每个点维护一棵包含其子树所有节点的主席树 询问(x ...
- spring boot log4j2与三方依赖库log4j冲突无法初始化问题解决方法
因为从Spring Boot 1.4开始,spring boot就不支持log4j了,必须是log4j2或者logback,具体两者如何配置以及NDC的支持可以参考spring boot精华版. 这里 ...
- linux设置代理
在~/.bashrc或者/etc/profile下,添加下面 http_proxy=http://192.168.105.171:80 https_proxy=$http_proxy export h ...
- Java字节码浅析(—)
英文原文链接,译文链接,原文作者:James Bloom,译者:有孚 明白Java代码是如何编译成字节码并在JVM上运行的非常重要,这有助于理解程序运行的时候究竟发生了些什么.理解这点不仅能搞清语言特 ...
- bzoj 1419 Red is good - 动态规划 - 概率与期望
Description 桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元.可以随时停止翻牌,在最优策略下平均能得到多少钱. Input 一 ...
- topcoder srm 330 div1
problem1 link 直接模拟. import java.util.*; import java.math.*; import static java.lang.Math.*; public c ...
- Docker 入门指南——Dockerfile 指令
COPY 复制文件 格式: COPY [--chown=<user>:<group>] <源路径>... <目标路径> 源路径可以是多个,甚至可以使通配 ...
- 编译openwrt时总是报错“staging_dir/target-aarch64_generic_glibc/stam$/.tools_compile_yynyyyyynyyyyynyynnyyyynyyyyyyyyyyyyyyynyynynnyyynny' failed”
1. 详细错误如下: tools/Makefile:146: recipe for target '/home/jello/openwrt/staging_dir/target-aarch64_gen ...
- linux内核中侧async_tx是什么?
答: 异步内存传输api(asynchronous memory transfer/transform API),这是一种api,用来为应用提供操作DMA的API. 下图是async_tx在架构中所处 ...
- Unity3D代码动态修改材质球的颜色
代码动态修改材质球的颜色: gameObject.GetComponent<Renderer>().material.color=Color.red;//当材质球的Shader为标准时,可 ...