cf C. Inna and Dima
http://codeforces.com/contest/374/problem/C
记忆化搜索,题意:求按照要求可以记过名字多少次,如果次数为无穷大,输出Poor Inna!,如果不经过一次输出Poor Dima!,否则输出输出次数。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 1001
#define LL __int64
using namespace std;
const LL inf=; int n,m;
char g[maxn][maxn];
LL dp[maxn][maxn];
int dir[][]={{,},{-,},{,},{,-}};
LL max1(LL a,LL b)
{
return a>b?a:b;
}
LL min1(LL a,LL b)
{
return a>b?b:a;
} LL dfs(int x,int y)
{
if(dp[x][y]!=-) return dp[x][y];
dp[x][y]=inf;
int c=;
for(int i=; i<; i++)
{
int xx=x+dir[i][];
int yy=y+dir[i][];
if(xx>=&&xx<n&&yy>=&&yy<m)
{
if((g[x][y]=='D'&&g[xx][yy]=='I')||(g[x][y]=='I'&&g[xx][yy]=='M')||(g[x][y]=='M'&&g[xx][yy]=='A')||(g[x][y]=='A'&&g[xx][yy]=='D'))
{
c=max1(c,dfs(xx,yy));
}
}
}
return dp[x][y]=min1(inf,c+);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,-,sizeof(dp));
for(int i=; i<n; i++)
{
scanf("%s",g[i]);
}
LL ans=;
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
if(g[i][j]=='D')
{
ans=max1(ans,dfs(i,j));
}
}
}
if(ans==inf)
{
printf("Poor Inna!\n");
}
else
{
LL y=ans/;
if(y==)
{
printf("Poor Dima!\n");
}
else
printf("%I64d\n",y);
}
}
return ;
}
cf C. Inna and Dima的更多相关文章
- Codeforces 374 C Inna and Dima (DFS)
Inna and Dima 题意:从图上的任意一个D点按着DIMADIMA的顺序走,问一共可以经过多少个DIMA,如果经过0个DIMA就输出“Pool DIma!“,如果可以有无数多个DIMA就输出” ...
- Codeforces 374C - Inna and Dima
374C - Inna and Dima 思路:dfs+记忆化搜索 代码: #include<bits/stdc++.h> using namespace std; #define ll ...
- cf374C Inna and Dima dfs判环+求最长链
题目大意是有一个DIMA四种字母组成的矩阵,要在矩阵中找最长的DIMADIMADIMA……串,连接方式为四方向连接,问最长能找到多少DIMA.字母可以重复访问,如果DIMA串成环,即可以取出无限长的D ...
- 【题解】CF374C Inna and Dima
题面传送门 解决思路 本题是找最长路的图上问题,所以先考虑如何建图. 首先把每一个字母转化为数字,然后对于每一个点枚举四个方向,如果有下一个字母,就向那个点建一条边,可以用 \(vector\) 存图 ...
- cf A. Inna and Pink Pony(思维题)
题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...
- cf C. Inna and Candy Boxes
题意:给你一个长度为n的只含有1和0的字符串,w个询问,每次询问输入l,r:在[l,r]中在l+k-1.l+2*k-1.......r的位置都必须为1,如果不为1的,变成1,记为一次操作,其它的地方的 ...
- cf B Inna and Candy Boxes
题意:输入n,然后输入n个数ai,再输入n个数bi,如果在1-ai中能找到两个数x,y,x和y可以相等,如果x+y=bi,答案加上x*y,否则减去1,让结果尽可能大,输出结果. #include &l ...
- cf B. Inna and Nine
http://codeforces.com/contest/374/problem/B #include <cstdio> #include <cstring> #includ ...
- CF Dima and Salad 01背包
C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...
随机推荐
- MyBatis架构图
MyBatis架构 MyBatis依赖的jar不多,而且代码行数也没多少,其中使用了大量的设计模式,值得好好学习.下图是MyBatis的一张架构图,来自Java框架篇—Mybatis 入门. Myba ...
- 简单md5加密
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...
- Rocketmq整体分析
之前本人在实际的生产环境中,使用过activemq和rabbitmq消息队列,在使用过程中出现一些难以解决的问题,本文通过产品选型.网络架构和核心特性分析了rocketmq的优势和特性. 产品选型 我 ...
- Linux基本配置和管理 3 ---- Linux命令行文本处理工具
1 文件浏览(简单回顾) 1 cat 查看文件的内容 2 more 以翻页的形式查看,但是只能向下翻页 3 less 以翻页的形式查看,但是能够支持向上和向下翻页 4 head 默认是查看前10行,但 ...
- [Angular 2] Adding a data model
Instead of add todo as a string, we create a data model: export class TodoModel{ constructor( public ...
- Java的容器小结
1. 各个类与接口的关系:
- C#解leetcode 18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- sql查阅每一月的数据
因为项目中需要做数据报表的功能,需要统计每个月的销售额.我找到下面的sql语句.后来经过自己的测试,发现第二句才是可以用的, //String sql="SELECT year(buydat ...
- 二维码生成Demo
在C#中直接引用ThoughtWorks.QRCode.dll 类, 下载 dll 类 http://file.111cn.net/download/2013/06/29/20120516165420 ...
- (转)模板引擎类dedetemplate.class.php使用说明
1.概述 织梦的模板标签类似于XML格式,所有的模板都含有定界符,默认情况下是{dede:*}和{/dede:*},“*”代表模板标记名称. 一般情况下{dede:*}和{/dede:*}是成对出现的 ...