C. Inna and Dima

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.

Examples
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.

DFS搜索!


#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f

int index[4][2]={0,1,0,-1,-1,0,1,0};
char mp[1005][1005];//用于记录走的桌子
int f[1005][1005];//记录不同搜索次序是否走到了同一个点,走到就直接返回该值
int v[1005][1005];//用于记录同一次是否走回去了,形成了回环!形成则返回无穷!
int n,m;
int ans;

int dfs(int x,int y)
{
	int dx,dy;
	int ret=0;
	if(f[x][y]!=-1) return f[x][y];
	if(v[x][y]) return inf;
	v[x][y]=1;
	for(int i=0;i<4;i++)
	{
		dx=x+index[i][0];
		dy=y+index[i][1];
		if(x<0||x>n-1||y<0||y>m-1)
			continue ;
		if(mp[x][y]=='D'&&mp[dx][dy]=='I') ret=max(dfs(dx,dy),ret);
		else if(mp[x][y]=='I'&&mp[dx][dy]=='M') ret=max(dfs(dx,dy),ret);
		else if(mp[x][y]=='M'&&mp[dx][dy]=='A') ret=max(dfs(dx,dy),ret);
		else if(mp[x][y]=='A'&&mp[dx][dy]=='D') ret=max(dfs(dx,dy),ret);
	}
	v[x][y]=0;
	if(mp[x][y]=='A')
	{
		f[x][y]=ret+1;
		return ret+1;
	}
	else
		return ret;
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(f,-1,sizeof(f));
		memset(v,0,sizeof(v));
		ans=0;
		for(int i=0;i<n;i++)
			scanf("%s",mp[i]);
		for(int i=0;i<n;i++)
			for(int j=0;j<m;j++)
		{
			if(mp[i][j]=='D')
			{
				ans=max(dfs(i,j),ans);
			}
		}
		if(ans==0) puts("Poor Dima!");
		else if(ans>=inf) puts("Poor Inna!");
		else printf("%d\n",ans);
	}
   return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值