Codeforces 374 C. Travelling Salesman and Special Numbers (dfs、记忆化搜索)
题目链接: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、记忆化搜索)的更多相关文章
- Codeforces 914 C. Travelling Salesman and Special Numbers (数位DP)
题目链接:Travelling Salesman and Special Numbers 题意: 给出一个二进制数n,每次操作可以将这个数变为其二进制数位上所有1的和(3->2 ; 7-> ...
- 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 ...
- Codeforces Round #459 (Div. 2):D. MADMAX(记忆化搜索+博弈论)
D. MADMAX time limit per test1 second memory limit per test256 megabytes Problem Description As we a ...
- 牛客假日团队赛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 ...
- Codeforces Global Round 23 D.Paths on the Tree(记忆化搜索)
https://codeforces.ml/contest/1746/problem/D 题目大意:一棵n节点有根树,根节点为1,分别有两个数组 s[i] 顶点 i 的魅力值 c[i] 覆盖顶点 i ...
- Codeforces 914C Travelling Salesman and Special Numbers:数位dp
题目链接:http://codeforces.com/problemset/problem/914/C 题意: 对数字x进行一次操作,可以将数字x变为x在二进制下1的个数. 显然,一个正整数在进行了若 ...
- Travelling Salesman and Special Numbers CodeForces - 914C (数位dp)
大意: 对于一个数$x$, 每次操作可将$x$变为$x$二进制中1的个数 定义经过k次操作变为1的数为好数, 求$[1,n]$中有多少个好数 注意到n二进制位最大1000位, 经过一次操作后一定变为1 ...
- Codeforces 914C Travelling Salesman and Special Numbers (数位DP)
题意:题目中定义了一种运算,把数字x变成数字x的二进制位数.问小于n的恰好k次运算可以变成1的数的个数(题目中的n是二进制数,n最大到2^1000) 思路:容易发现,无论多么大的数,只要进行了一次运算 ...
- 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C】 Travelling Salesman and Special Numbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 会发现. 进行一次操作过后. 得到的数字肯定是<=1000的 然后1000以下可以暴力做的. 则我们枚举第1步后得到的数字x是 ...
随机推荐
- AVAudioPlayer简易封装
AVAudioPlayer简易封装 [说明] AVAudioPlayer简易封装,仅仅支持播放,暂停,停止,暂停时候带有渐隐效果,自己用,没有参考价值. [源码] https://github.com ...
- IE漏洞的调试心得
在调试漏洞的过程中,个人感觉最棘手的就是ie浏览器的漏洞和flash player的漏洞了.这里打算记录一下学习过程中的心得(主要是基于uaf类),以方便新人学习. 首先,ie漏洞与众不同的是,程序的 ...
- September 17th 2017 Week 38th Sunday
Distance could make you forget about them, but the memories would always be there. 距离会让你遗忘,但是回忆却始终在那 ...
- Mac Item2 设置别名 永久生效
使用 Item2 终端, 设置 别名的时候, 按照 网上的说法, 是 去 修改 用户目录下的 .bashrc 或者 .bash_profile 这两个文件都可以, 把 alias 写在 这两 ...
- Stacks And Queues
栈和队列 大型填坑现场,第一部分的还没写,以上. 栈和队列是很基础的数据结构,前者后进先出,后者先进先出,如下图: 下面开始将客户端和具体实现分开,这样有两个好处:一是客户端不知道实现的细节,但同时也 ...
- 【笔记】select, poll, epool
Select 系统调用: select 轮询监听多个文件描述符的数组,其原理如下(转自:这里): 从用户空间拷贝fd_set到内核空间:注册回调函数__pollwait:遍历所有fd,对全部指定设备做 ...
- 自定义控件(视图)2期笔记14:自定义视图之View事件分发 dispatchTouchEvent,onTouch,onTouchEvent,onClick逻辑顺序过程
1. 这里我们先从案例角度说明dispatchTouchEvent,onTouch,onTouchEvent,onClick逻辑顺序过程: (1)首先我们重写一个MyButton 继承自 Button ...
- 记一次webservice的超时时间设置
一次项目组中需要控制超时时间,前期习惯用CXF实现,熟悉的才是最好的.所以这次依然想用CXF实现. 实现的方式代码如下: static{ String fvpWebserviceUrl = Prope ...
- 一劳永逸部署项目:通过tomcat加载环境变量
一劳永逸部署项目:通过tomcat加载环境变量 转载自:https://blog.csdn.net/u010414666/article/details/46499953 一.说明 项目中经常会用到x ...
- using指令都用了这么多年了,其实还真没懂!
在C语言中,我们经常使用#include<stdio.h>指令来导入标准输入输出库,这确实很好理解,相当于把代码复制到当前的程序中. 但在C#语言中,当我们写Console程序时,经常在第 ...