Inna and Dima
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.
The first line of the input contains two integersn andm(1 ≤ n, m ≤ 103).
Then follow n lines that describe Inna and Dima's table. Each line containsm 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".
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.
1 2
DI
Poor Dima!
2 2
MA
ID
Poor Inna!
5 5
DIMAD
DIMAI
DIMAM
DDMAA
AAMID
4
解题思路:每次按照DIMA的顺序去找下一个位置,遇到A的时候将答案+1,要特别注意对于环的判断。
AC代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include<set>
#include<map>
#include<queue>
#define maxn 1005
#define inf 0x3f3f3f3f
#define ONES(x) __builtin_popcount(x)
using namespace std;
typedef long long ll ;
const double eps =1e-8;
const int mod = 1000000007;
typedef pair<int, int> P;
const double PI = acos(-1.0);
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int n,m;
int ans = inf;
char cmap[maxn][maxn];
char ch[4] = {'D','I','M','A'};
int dp[maxn][maxn];
bool vis[maxn][maxn];
queue <P> start;
bool flag2 = false;//判断是否成环
int dfs(int x, int y, int flag)//x,y当前位置,flag表示当前要寻找的是DIMA中第几个元素
{
if(flag2)
return -1;
int res = 0,i;
int a[4] = {0};//记录一下每次四个方向搜索得到的答案
if(dp[x][y]!=-1)//进行记忆化操作
return dp[x][y];
if(x>n || x<=0 || y>m || y<=0)//越界判定
return 0;
if(vis[x][y])//判断当前位置是否访问过,是判环的重要一步
{
flag2 = true;
return -1;
}
vis[x][y] = true;//将当前节点设置成已经访问
for(i=0;i<4;i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(cmap[xx][yy]==ch[(flag+1)%4])//判断新位置是否可以继续
{
a[i] = dfs(xx,yy,(flag+1)%4);//下一步深搜答案
vis[xx][yy] = false;//消除本次访问的影响
if(a[i]==-1)//发现成环
return -1;
}
}
res = *max_element(a,a+4);
if(ch[(flag)%4]=='A')//更新答案
res++;
return dp[x][y] = res;
}
int main()
{
//freopen("test.txt","r",stdin);
scanf("%d%d",&n,&m);
int i,j;
for(int i = 1; i<=n ; i++)
{
scanf("%s",cmap[i]+1);
}
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
if(cmap[i][j]=='D')//将所有的起点存到一个队列之中
{
P p1(i,j);
start.push(p1);
}
}
}
if(start.empty())
{
printf("Poor Dima!\n");
return 0;
}
ans = -1;
memset(dp,-1,sizeof(dp));
while(!start.empty())
{
P p1 = start.front();
start.pop();
int x = p1.first;
int y = p1.second;
int flag = 0;
//vis[x][y] = true;
ans = max(ans,dfs(x,y,flag));
if(flag2)
{cout << "Poor Inna!" << endl;return 0;}
vis[x][y] = false;
}
if(ans==0)
cout << "Poor Dima!" << endl;
else
cout << ans << endl;
return 0;
}