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是 ...
随机推荐
- mysql-sql-standard
https://github.com/zhishutech/mysql-sql-standard
- [控件] LineAnimationView
LineAnimationView 效果 说明 水平循环无间隔播放动画效果,用于loading的界面 源码 https://github.com/YouXianMing/UI-Component-Co ...
- Replace-iOS
Replace-iOS https://github.com/MartinRGB/Replace-iOS 看了下demo,运行起来超卡...... Simply Implement Zee Young ...
- [翻译] BTSimpleRippleButton
BTSimpleRippleButton https://github.com/balram3429/btSimpleRippleButton This is a custom button for ...
- [翻译] SCRecorder
SCRecorder https://github.com/rFlex/SCRecorder An easy Vine/Instagram like video and/or audio record ...
- [C++] 用Xcode来写C++程序[7] Class
用Xcode来写C++程序[7] Class 不带构造函数的Rectangle类 // // Rectangle.h // Plus // // Created by YouXianMing on 1 ...
- 审计系统---堡垒机项目之用户交互+session日志写入数据库[完整版]
2018-06-20 时隔一个多月,忘记了之前的所有操作,重拾起来还是听不容易的,想过放弃,但还是想坚持一下,加油. 世界杯今天葡萄牙1:0战胜摩洛哥,C 罗的一个头球拯救了时间,目前有4个射球,居2 ...
- 连接Mysql错误 error 1042 can't get hostname for your address
背景: 1.etc下的hosts文件有: 127.0.0.1 localhost 2.MySQL的my.ini配置文件: [mysqld] 节点下已经加入以下两行代码 skip-n ...
- 1.3 Essential Python Libraries(一些重要的Python库)
1.3 Essential Python Libraries(一些重要的Python库) 如果不了解Python的数据生态,以及本书中即将用到的一些库,这里会做一个简单的介绍: Numpy 这里就不过 ...
- NSProxy应用例子
动态代理模式的应用很多,特别是在不能修改被代理类的前提下,要对执行某些方法时需要打log或者捕捉异常等处理时,是一个非常方便的方法.只需要少量修改客户端(场景类)代码和添加一个代理类就可以实现,这个符 ...