题目链接:Travelling Salesman and Special Numbers

题意:

  给了一个n×m的图,图里面有'N','I','M','A'四种字符。问图中能构成NIMA这种序列最大个数(连续的,比如说NIMANIMA = 2)为多少,如果有环的话那么最大长度就是无穷。

题解:

  哇,做了这题深深得感觉自己的dfs真的好弱。这题就是从N开始深搜,在深搜的过程中记录值。返回这个值加1.

 //#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1e3+;
const int INF = 1e9+;
typedef pair<int,int> P;
char vec[MAX_N][MAX_N];
int mp[MAX_N][MAX_N];
int res[MAX_N][MAX_N];
int vis[MAX_N][MAX_N];
int dir[][] = {-,,,,,,,-};
int ans,flag,N,M,T;
queue<P> que;
vector<P> st;
int dfs(int x,int y,int pos)
{
if(vis[x][y])
{
flag = false; return INF;
}
if(res[x][y]) return res[x][y];
vis[x][y] = ;
int num =;
for(int i=;i<;i++)
{
int nx = x + dir[i][];
int ny = y + dir[i][];
if(nx>= && nx<N && ny>= && ny<M && mp[nx][ny] == (pos+)% )
{
num = max(num,dfs(nx,ny,(pos+)%));
}
}
res[x][y] = num+;
vis[x][y] = ;
return num+;
}
int main()
{
cin>>N>>M;
memset(vis,,sizeof(vis));
memset(res,,sizeof(res));
st.clear();
for(int i=; i<N; i++)
{
scanf("%s",vec[i]);
for(int j=; j<M; j++)
{
if(vec[i][j] == 'D') mp[i][j] = ,st.push_back(P(i,j));
else if(vec[i][j] == 'I') mp[i][j] = ;
else if(vec[i][j] == 'M') mp[i][j] = ;
else if(vec[i][j] == 'A') mp[i][j] = ;
}
}
int out = ;
flag = true;
for(int i=;i<st.size();i++)
{
if(res[st[i].first][st[i].second] != || vis[st[i].first][st[i].second]) continue;
int t = dfs(st[i].first,st[i].second,);
if(!flag) break;
out = max(out,t/);
}
if(flag)
{
if(out != ) cout<<out<<endl;
else cout<<"Poor Dima!"<<endl;
}
else cout<<"Poor Inna!"<<endl;
return ;
}
/*
5 5
DIADD
DMADD
DDDID
AMMMD
MIDAD answer: 3
*/

Codeforces 374 C. Travelling Salesman and Special Numbers (dfs、记忆化搜索)的更多相关文章

  1. Codeforces 914 C. Travelling Salesman and Special Numbers (数位DP)

    题目链接:Travelling Salesman and Special Numbers 题意: 给出一个二进制数n,每次操作可以将这个数变为其二进制数位上所有1的和(3->2 ; 7-> ...

  2. Codeforces 914 C Travelling Salesman and Special Numbers

    Discription The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pas ...

  3. Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)

    D. MADMAX time limit per test1 second memory limit per test256 megabytes Problem Description As we a ...

  4. 牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)

    链接:https://ac.nowcoder.com/acm/contest/984/F 来源:牛客网 随机数 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  5. Codeforces Global Round 23 D.Paths on the Tree(记忆化搜索)

    https://codeforces.ml/contest/1746/problem/D 题目大意:一棵n节点有根树,根节点为1,分别有两个数组 s[i] 顶点 i 的魅力值 c[i] 覆盖顶点 i ...

  6. Codeforces 914C Travelling Salesman and Special Numbers:数位dp

    题目链接:http://codeforces.com/problemset/problem/914/C 题意: 对数字x进行一次操作,可以将数字x变为x在二进制下1的个数. 显然,一个正整数在进行了若 ...

  7. Travelling Salesman and Special Numbers CodeForces - 914C (数位dp)

    大意: 对于一个数$x$, 每次操作可将$x$变为$x$二进制中1的个数 定义经过k次操作变为1的数为好数, 求$[1,n]$中有多少个好数 注意到n二进制位最大1000位, 经过一次操作后一定变为1 ...

  8. Codeforces 914C Travelling Salesman and Special Numbers (数位DP)

    题意:题目中定义了一种运算,把数字x变成数字x的二进制位数.问小于n的恰好k次运算可以变成1的数的个数(题目中的n是二进制数,n最大到2^1000) 思路:容易发现,无论多么大的数,只要进行了一次运算 ...

  9. 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C】 Travelling Salesman and Special Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现. 进行一次操作过后. 得到的数字肯定是<=1000的 然后1000以下可以暴力做的. 则我们枚举第1步后得到的数字x是 ...

随机推荐

  1. 使用AKLocationManager定位

    使用AKLocationManager定位 https://github.com/ideaismobile/AKLocationManager 以下是使用情况: 是不是很简单呢,我们可以将它的步骤进一 ...

  2. Python3.5 执行发邮件Exchangelib(=)

    fyl Python发邮件的代码如下: 只需要填写好加粗字体,即可正常使用. from exchangelib import DELEGATE, Account, Credentials, Messa ...

  3. python_web应用雏型

    python_web应用雏型 Web应用程序顾名思义,就是一种可以通过Web访问的应用程序, Web应用的最大特点是用户只需要有网络和浏览器,不需要再安装其他软件就可顺利通过web访问到程序. WEB ...

  4. September 29th 2017 Week 39th Friday

    Human life is ephemera, which makes it precious. 生命短暂,所以珍贵. Don't waste time on praying to the God. ...

  5. [Codeforces 321D][2018HN省队集训D4T2] Ciel and Flipboard

    [Codeforces 321D][2018HN省队集训D4T2] Ciel and Flipboard 题意 给定一个 \(n\times n\) 的矩阵 \(A\), (\(n\) 为奇数) , ...

  6. print in或者not in, 判断在不在里面

    print("不疼" in "麻花疼")        # 结果False print("不疼"in "真不疼") # ...

  7. [微信小程序直播平台开发]___(二)Nginx+rtmp在Windows中的搭建

    1.一个可以忽略的前言 Nginx (engine x) 是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Ramble ...

  8. CI 2.2 + smarty 3.1.18 完美整合配置成功

    CI 2.2 + smarty 3.1.18 配置成功 一.准备文档下载  (CI 框架和smarty) 二.将Smarty-3.1.18 源码包里面的libs文件夹copy到ci的项目目录appli ...

  9. IKVM.NET入门(2)

    ikvm.net是什么 http://www.ikvm.net/ ikvm.net是能够运行在mono和.net framework的java虚拟机.它包括了 在.net中实现的一个java虚拟机 j ...

  10. eclipse异常关闭,无法启动tomcat解决办法

    如果eclipse异常关闭,会出现以下 此时需要关闭javaw.exe即可,重新启动tomcat了. 关闭javaw.exe需要打开任务关闭器,选择详细信息,然后结束javaw.exe即可