您的位置:首页 > 其它

codeforces 374C C. Inna and Dima(DFS)

2015-07-16 11:23 543 查看
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:

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
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;

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


题意:

在图中找出最多有多少个连着的DIMA。没有,输出 "Poor
Dima!" ;形成环即为无穷个,输出"Poor
Inna!";否则输出最大个数。

代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define inf 9999999

int vis[1001][1001],dis[1001][1001];
char map[1001][1001];
char next[200];
int n,m;
int dix[]={1,0,-1,0};
int diy[]={0,1,0,-1};

int dfs(int x,int y,char c)
{
int tmp,step=0;
if(dis[x][y]!=-1)
return dis[x][y];
for(int i=0;i<4;i++)
{
int xx=x+dix[i];
int yy=y+diy[i];
if(xx<0||xx>=n||yy<0||yy>=m||next[c]!=map[xx][yy])
continue;
if(vis[xx][yy])
{
step=inf;
}
else
{
vis[xx][yy]=1;
tmp=dfs(xx,yy,map[xx][yy]);
step=max(tmp,step);
vis[xx][yy]=0;
}
}
dis[x][y]=step+1;
return dis[x][y];
}

int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
scanf("%s",&map[i]);
next['D']='I';next['I']='M';
next['M']='A';next['A']='D';
memset(dis,-1,sizeof(dis));
memset(vis,0,sizeof(vis));
int t,ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(map[i][j]=='D' && dis[i][j]==-1)
{
vis[i][j]=1;
t=dfs(i,j,'D');
ans=max(ans,t/4);
vis[i][j]=0;
}
}
}
if(ans==0) cout<<"Poor Dima!"<<endl;
else if(ans>=inf/4)cout<<"Poor Inna!"<<endl;
else cout<<ans<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: