Codeforces Round #220 (Div. 2) C. Inna and Dima (记忆化搜索)

C. Inna and Dima
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Inna and Dima bought a table of size n × m in the shop. Each cell of the table contains a single letter: "D", "I", "M", "A".

Inna loves Dima, so she wants to go through his name as many times as possible as she moves through the table. For that, Inna acts as follows:

  1. initially, Inna chooses some cell of the table where letter "D" is written;
  2. then Inna can move to some side-adjacent table cell that contains letter "I"; then from this cell she can go to one of the side-adjacent table cells that contains the written letter "M"; then she can go to a side-adjacent cell that contains letter "A". Then Inna assumes that she has gone through her sweetheart's name;
  3. Inna's next move can be going to one of the side-adjacent table cells that contains letter "D" and then walk on through name DIMA in the similar manner. Inna never skips a letter. So, from the letter "D" she always goes to the letter "I", from the letter "I" she always goes the to letter "M", from the letter "M" she always goes to the letter "A", and from the letter "A" she always goes to the letter "D".

Depending on the choice of the initial table cell, Inna can go through name DIMA either an infinite number of times or some positive finite number of times or she can't go through his name once. Help Inna find out what maximum number of times she can go through name DIMA.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 103).

Then follow n lines that describe Inna and Dima's table. Each line contains m characters. Each character is one of the following four characters: "D", "I", "M", "A".

Note that it is not guaranteed that the table contains at least one letter "D".

Output

If Inna cannot go through name DIMA once, print on a single line "Poor Dima!" without the quotes. If there is the infinite number of names DIMA Inna can go through, print "Poor Inna!" without the quotes. Otherwise print a single integer — the maximum number of times Inna can go through name DIMA.

Sample test(s)
input
1 2
DI
output
Poor Dima!
input
2 2
MA
ID
output
Poor Inna!
input
5 5
DIMAD
DIMAI
DIMAM
DDMAA
AAMID
output
4
Note

Notes to the samples:

In the first test sample, Inna cannot go through name DIMA a single time.

In the second test sample, Inna can go through the infinite number of words DIMA. For that, she should move in the clockwise direction starting from the lower right corner.

In the third test sample the best strategy is to start from the cell in the upper left corner of the table. Starting from this cell, Inna can go through name DIMA four times.


题意:

在所给的矩阵中找 'D'->'I'->'M'->'A' 这样的序列,问最大长度为多少,如果有环的话那么最大长度就是无穷。


思路:

如果没有环的话。那么就是一个简单的记忆化搜索,有环的话也很简单,加个标记回溯就好了,看是否搜到了原先访问的地方。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 100005
#define mod 1000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,ans,flag,cnt;
char mp[maxn][maxn];
char next[200];
int dp[maxn][maxn];
bool vis[maxn][maxn];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};

int dfs(int x,int y,char c)
{
    if(dp[x][y]!=-1) return dp[x][y];
    int i,j,t,tx,ty,best=0;
    for(i=0;i<4;i++)
    {
        tx=x+dx[i];
        ty=y+dy[i];
        if(tx<0||tx>=n||ty<0||ty>=m||next[c]!=mp[tx][ty]) continue ;
        if(vis[tx][ty])
        {
            best=INF;
        }
        else
        {
            vis[tx][ty]=1;
            t=dfs(tx,ty,mp[tx][ty]);
            vis[tx][ty]=0;
            best=max(t,best);
        }
    }
    dp[x][y]=best+1;
    return dp[x][y];
}
int main()
{
    int i,j,t;
    next['D']='I',next['I']='M';
    next['M']='A',next['A']='D';
    while(~scanf("%d%d",&n,&m))
    {
        for(i=0;i<n;i++)
        {
            scanf("%s",mp[i]);
        }
        for(i=1;i<5;i++) ;
        memset(dp,-1,sizeof(dp));
        memset(vis,0,sizeof(vis));
        ans=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(mp[i][j]=='D'&&dp[i][j]==-1)
                {
                    vis[i][j]=1;
                    t=dfs(i,j,'D');
                    ans=max(ans,t/4);
                    vis[i][j]=0;
                }
            }
        }
        if(ans==0) printf("Poor Dima!\n");
        else if(ans>=INF/4) printf("Poor Inna!\n");
        else printf("%d\n",ans);
    }
    return 0;
}
/*
5 5
DIMAD
DIIDI
DIMAM
DDMMA
AAMID
*/





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值