Home > Web Front-end > HTML Tutorial > Codeforces Round #220 (Div. 2) Question C: Inna and Dima (Memory Search DP)_html/css_WEB-ITnose

Codeforces Round #220 (Div. 2) Question C: Inna and Dima (Memory Search DP)_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:55:15
Original
1236 people have browsed it

Question address: http://codeforces.com/problemset/problem/374/C

Use dp[i][j] to represent the number in row i and column j. You can go maximum distance. When it is -1, it means that it has not been walked, and the ones that have been walked but not finished are temporarily marked as INF. In this way, if there is a cycle, INF will be returned. Then search with dfs.

The code is as follows:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define LL __int64const int INF=0x3f3f3f3f;char mp[1001][1001], str[]="DIMA";int dp[1001][1001];int jx[]={0,0,1,-1};int jy[]={1,-1,0,0};int n, m;int dfs(int x, int y, int tmp){    if(dp[x][y]!=-1) return dp[x][y];    dp[x][y]=INF;    int i, a, b, t=0;    tmp=(1+tmp)%4;    for(i=0;i<4;i++)    {        a=x+jx[i];        b=y+jy[i];        if(a>=0&&a<n&&b>=0&&b<m&&mp[a][b]==str[tmp])        {            t=max(t,dfs(a,b,tmp));        }    }    dp[x][y]=++t;    return t;}int main(){    int i, j, max1=0;    scanf("%d%d",&n,&m);    for(i=0;i<n;i++)    {        scanf("%s",mp[i]);    }    memset(dp,-1,sizeof(dp));    for(i=0;i<n;i++)    {        for(j=0;j<m;j++)        {            if(mp[i][j]=='D')            {                max1=max(max1,dfs(i,j,0));            }        }    }    if(max1/4==0)    {        puts("Poor Dima!");    }    else if(max1>=INF)    {        puts("Poor Inna!");    }    else        printf("%d\n",max1/4);    return 0;}
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template