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 ...
随机推荐
- mvc与mvvm
mvc:被动型式的,也就是说,只有view有要求的时候,控制器才有反应 View Controller Model 可以把一个页面看作是由多个view组成 Controller控制 ...
- mysql group by的用法 注意
group by 用法: 官方的解释:select 后面的字段必须出现在 group by 后面, 除非是聚合,sum,或者count 但是如果 是多表联查, SELECT c.`name` A ...
- [Angular 2] ng-class and Encapsulated Component Styles
import {Input, Component, View, NgClass} from "angular2/angular2"; @Component({ selector: ...
- Android 跨应用调用Activity及Service
如何调用另外一个app应用的activity或者service,本文提供一个验证可行的方法. 调用方法: Intent intent=new Intent("youActionName&qu ...
- Android(java)学习笔记240:多媒体之图形颜色的变化
1.相信大家都用过美图秀秀中如下的功能,调整颜色: 2. 下面通过案例说明Android中如何调色: 颜色矩阵 ColorMatrix cm = new ColorMatrix(); paint.se ...
- Android传感器的使用(GravieySensor)
这里以重力传感器为例说明 第一步:创建一个SensorManager对象,用来管理或者获取传感器 SensorManager sm = (SensorManager) this.getSystemSe ...
- Python操作MySQL -即pymysql/SQLAlchemy用法
本节介绍Python对于MySQL的一些操作用法 模块1:pymysql(等同于MySQLdb) 说明:pymysql与MySQLdb模块的使用基本相同,学会pymysql,使用MySQLdb也就不是 ...
- PHP微信公众号 access_token缓存
PHP创建access_token.json文件,将access_token 和 生成时间expires 保存在其中, {"access_token":"xxxx&quo ...
- 使用jq深入研究轮播图特性
网站轮播图 太耳熟的词了 基本上做pc端的 主页绝壁会来一个轮播图的特效 轮播图他一个页面页面的切换,其实的原理是通过css的定位 ,定位到一起,第一张首先显示,其余默认隐藏. 今天我实现的这个轮播 ...
- 移动Web开发技巧
META相关 1. 添加到主屏后的标题(IOS) <meta name="apple-mobile-web-app-title" content="标题" ...