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 可以自动处理下一分钟,姑且理解 ...
随机推荐
- mysql优化之使用iotop+pt-ioprofile定位具体top io文件
今天,将一个环境切换成行情优化后的版本后,发现io等待还是挺高,这还是第一次出现的.其他很多套环境都没有这个问题了,故iotop看了下,基本可以确定为是mysql进程的问题,如下: 但是iotop只能 ...
- 简易DVD查询系统
==========DVD管理系统========== 这是DVD任务管理器的主界面: package day20181224; import java.text.ParseException; im ...
- bzoj 1093 最大半连通子图 - Tarjan - 拓扑排序 - 动态规划
一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径.若G'=(V ...
- Codeforces Round #427 (Div. 2) Problem A Key races (Codeforces 835 A)
Two boys decided to compete in text typing on the site "Key races". During the competition ...
- topcoder srm 415 div1
problem1 link 每次贪心地从crans由大到小地找到一个能搬得动地尽量大地box即可. problem2 link 首先,$hava$可以全部换成钱,然后就是找到一个最小的钱减去自己已有的 ...
- MongDB 数据库使用技巧及注意事项
说明 本文记录MongoDB数据库的使用技巧总结,特性发现. 利用TTL特性实现数据定时删除 限制: 你不能创建TTL索引,如果要索引的字段已经在其他索引中使用. 索引不能包含多个字段. 索引的字段必 ...
- 三部排序|2013年蓝桥杯B组题解析第六题-fishers
三部排序| 一般的排序有许多经典算法,如快速排序.希尔排序等. 但实际应用时,经常会或多或少有一些特殊的要求.我们没必要套用那些经典算法,可以根据实际情况建立更好的解法. 比如,对一个整型数组中的数字 ...
- Win10下Java开发环境配置
首先下载符合操作系统版本的jdk,比如64位的JDK8: 下载链接:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-down ...
- 案例2:用一条SQL查询出数学语文成绩都大于80分的学生姓名?
方法1: 查出科目成绩有小于80分的学生姓名,再约束并去重学生不等于查出来的姓名 select distinct A.name from t_score A where A.name not in(s ...
- entity framework浅谈
1. 什么是EF 微软提供的ORM工具. ORM让开发人员节省数据库访问代码的时间. 将更多的时间放在业务逻辑层面上. 开发人员使用linq语言, 对数据库进行操作. 2. EF的使用场景 EF有三种 ...