• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Codeforce Round #220 Div2 C

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

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.

 1 #include <cstdio>
 2 #include <vector>
 3 #include <cmath>
 4 #include <queue>
 5 #include <cstring>
 6 #include <iostream>
 7 #include <algorithm>
 8 using namespace std;
 9 #define INF 100000000
10 #define mod 1000000007
11 #define ll long long
12 #define maxn 1005
13 #define pi acos(-1.0)
14 int n, m, k, d, x, y, ans, t;
15 char s[maxn][maxn];
16 int dp[maxn][maxn];
17 int dx [] = { 1, 0, -1, 0 };
18 int dy [] = { 0, 1, 0, -1 };
19 int dfs(int x, int y){
20     if (dp[x][y] == -1)return INF;
21     if (dp[x][y] == 0){
22         dp[x][y] = -1;
23         int res = 1;
24         for (int i = 0; i < 4; i++)
25             if (s[x][y] == 'D'&&s[x + dx[i]][y + dy[i]]=='I' || s[x][y] == 'I'&&s[x + dx[i]][y + dy[i]]=='M' || s[x][y] == 'M'&&s[x + dx[i]][y + dy[i]] == 'A' || s[x][y] == 'A'&&s[x + dx[i]][y + dy[i]] == 'D')
26                 res = max(res, dfs(x + dx[i], y + dy[i]) + 1);
27         dp[x][y] = min(res, INF);
28     }
29     return dp[x][y];
30 }
31 int main(){
32     scanf("%d%d", &n, &m);
33     for(int i=0;i<n;i++)scanf("%s", s[i]);
34     ans = 0;
35     for (int i = 0; i < n; i++)
36         for (int j = 0; j < m; j++)
37         if (s[i][j]=='D')ans = max(ans, dfs(i, j));
38     if (ans < 4){ printf("Poor Dima!\n"); return 0; }
39     if (ans >= INF)printf("Poor Inna!\n");
40     else printf("%d\n", ans/4);
41     return 0;
42 }
View Code
posted @ 2013-12-20 12:13  HaibaraAi  阅读(143)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3