C. Inna and Dima time limit per test 1 second memory limit per test megabytes input standard input output Put
Inna and Dima bought a table of size NXM in the shop. Each cell of the table contains a single letter: "D", "I", "M", "a".
Inna loves Dima, so she wants-go through his name as many times as possible as she moves through the table. For, Inna acts as follows:
Initially, Inna chooses some cell of the table where letter ' D ' is written; 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, the contains the written letter "M"; Then she can go to a side-adjacent cell, that contains letter "a". Then Inna assumes that she had gone through her sweetheart ' s name; Inna ' s next move can be going to one of the side-adjacent table cells, contains letter "D" and then walk on through NA Me DIMA in the similar manner. Inna never skips a letter. So, from the letter "D" she is always goes to the letter "I" from the "I" she is always goes the "M", from the Letter "M" she is 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 som E 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 the 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 are one of the following four characters: "D", "I", "M", "A".
Note that it isn't 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 of 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 was to start from the cell in the upper left corner of the table. Starting from the This cell, Inna can go through name DIMA the four times.
Test instructions
In the given matrix to find the ' D ', ' I ', ' M ', ' a ' such a sequence, ask the maximum length of how much, if there is a ring then the maximum length is infinite.
Ideas:
If there is no ring. Then is a simple memory search, there is a ring is also very simple, add a marker back to the good, see if the original visit to the place.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set&
Gt #include <queue> #pragma comment (linker, "/stack:102400000,102400000") #define MAXN 1005 #define MAXN 100005 #defin
E 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 */