• cf C. Inna and Dima


    http://codeforces.com/contest/374/problem/C

    记忆化搜索,题意:求按照要求可以记过名字多少次,如果次数为无穷大,输出Poor Inna!,如果不经过一次输出Poor Dima!,否则输出输出次数。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #define maxn 1001
     5 #define LL __int64
     6 using namespace std;
     7 const LL inf=999999999;
     8 
     9 int n,m;
    10 char g[maxn][maxn];
    11 LL dp[maxn][maxn];
    12 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
    13 LL max1(LL a,LL b)
    14 {
    15     return a>b?a:b;
    16 }
    17 LL min1(LL a,LL b)
    18 {
    19     return a>b?b:a;
    20 }
    21 
    22 LL dfs(int x,int y)
    23 {
    24     if(dp[x][y]!=-1) return dp[x][y];
    25     dp[x][y]=inf;
    26     int c=0;
    27     for(int i=0; i<4; i++)
    28     {
    29         int xx=x+dir[i][0];
    30         int yy=y+dir[i][1];
    31         if(xx>=0&&xx<n&&yy>=0&&yy<m)
    32         {
    33             if((g[x][y]=='D'&&g[xx][yy]=='I')||(g[x][y]=='I'&&g[xx][yy]=='M')||(g[x][y]=='M'&&g[xx][yy]=='A')||(g[x][y]=='A'&&g[xx][yy]=='D'))
    34             {
    35                 c=max1(c,dfs(xx,yy));
    36             }
    37         }
    38     }
    39     return dp[x][y]=min1(inf,c+1);
    40 }
    41 int main()
    42 {
    43     while(scanf("%d%d",&n,&m)!=EOF)
    44     {
    45         memset(dp,-1,sizeof(dp));
    46         for(int i=0; i<n; i++)
    47         {
    48             scanf("%s",g[i]);
    49         }
    50         LL ans=0;
    51         for(int i=0; i<n; i++)
    52         {
    53             for(int j=0; j<m; j++)
    54             {
    55                 if(g[i][j]=='D')
    56                 {
    57                     ans=max1(ans,dfs(i,j));
    58                 }
    59             }
    60         }
    61         if(ans==inf)
    62         {
    63             printf("Poor Inna!
    ");
    64         }
    65         else
    66         {
    67             LL y=ans/4;
    68             if(y==0)
    69             {
    70                 printf("Poor Dima!
    ");
    71             }
    72             else
    73                 printf("%I64d
    ",y);
    74         }
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    直接插入排序
    直接选择排序
    冒泡排序
    归并排序
    进程调度
    进程与线程
    c语言struct和c++struct的区别
    二叉搜索树、AVL平衡二叉搜索树、红黑树、多路查找树

    6-11 先序输出叶结点
  • 原文地址:https://www.cnblogs.com/fanminghui/p/4083641.html
Copyright © 2020-2023  润新知