题面传送门

解决思路

本题是找最长路的图上问题,所以先考虑如何建图。

首先把每一个字母转化为数字,然后对于每一个点枚举四个方向,如果有下一个字母,就向那个点建一条边,可以用 \(vector\) 存图比较方便。用数字标号,只需要判断 \(t_{x2,y2}=(t_{x1,y1}+1)\mod 4\) 是否成立即可,但直接用字母判断也是比较方便的。

然后考虑 \(\text{dfs}\) 搜索可以走的最长路。有以下几个注意点:

  • 开始搜索的点一定是 D

  • 已经更新过答案的点就不用搜了,直接 \(\text{return}\),也算一个剪枝过程。

  • 可以选无数个点的情况就是出现环了。所以需要 \(vis\) 数组标记本次搜索经过的点,如果走到已经走过的点,直接输出 Poor Inna! 结束程序。同时回溯时记得要把 \(vis\) 清空。

  • 搜索时答案记录为走过的点数比较方便。因为从 D 开始,所以记 \(ans=\max\{dis_{i,j}\}\)。最后若 \(ans<4\) 即输出 Poor Dima!,否则 \(ans \div 4\) 就是 DIMA 的数量。

这样就可以愉快地通过本题了。

AC Code

#include<bits/stdc++.h>
using namespace std;
int n,m,t[1005][1005],dis[1005][1005],ans;
bool vis[1005][1005];
char c;
int X[4]={0,0,1,-1},Y[4]={1,-1,0,0};
struct node{
int x,y;
};
vector<node> a[1005][1005];
void read(){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>c;
if(c=='D') t[i][j]=0;
if(c=='I') t[i][j]=1;
if(c=='M') t[i][j]=2;
if(c=='A') t[i][j]=3;
}
}
}
bool cango(int x1,int y1,int x2,int y2){ //能不能走
if(t[x2][y2]==(t[x1][y1]+1)%4) return 1;
return 0;
}
void build(){ //建图部分
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
for(int z=0;z<4;z++){
int xx=i+X[z],yy=j+Y[z];
if(xx>n||xx<1||yy>m||yy<1) continue;
if(cango(i,j,xx,yy)) a[i][j].push_back({xx,yy});
}
}
}
}
void dfs(int sx,int sy){ //dfs
if(dis[sx][sy]) return;
vis[sx][sy]=1;
dis[sx][sy]=1;
for(int i=0;i<a[sx][sy].size();i++){
int jx=a[sx][sy][i].x,jy=a[sx][sy][i].y;
if(vis[jx][jy]){ //出现环
printf("Poor Inna!");
exit(0);
}
dfs(jx,jy);
dis[sx][sy]=max(dis[sx][sy],dis[jx][jy]+1);
ans=max(ans,dis[sx][sy]);
}
vis[sx][sy]=0; //vis清零
}
int main(){
scanf("%d%d",&n,&m);
read();
build();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++) if(!t[i][j]) dfs(i,j); //对每一个D搜索
}
if(ans<4) printf("Poor Dima!");
else printf("%d",ans/4);
return 0;
}

【题解】CF374C Inna and Dima的更多相关文章

  1. cf374C Inna and Dima dfs判环+求最长链

    题目大意是有一个DIMA四种字母组成的矩阵,要在矩阵中找最长的DIMADIMADIMA……串,连接方式为四方向连接,问最长能找到多少DIMA.字母可以重复访问,如果DIMA串成环,即可以取出无限长的D ...

  2. Codeforces 374 C Inna and Dima (DFS)

    Inna and Dima 题意:从图上的任意一个D点按着DIMADIMA的顺序走,问一共可以经过多少个DIMA,如果经过0个DIMA就输出“Pool DIma!“,如果可以有无数多个DIMA就输出” ...

  3. Codeforces 374C - Inna and Dima

    374C - Inna and Dima 思路:dfs+记忆化搜索 代码: #include<bits/stdc++.h> using namespace std; #define ll ...

  4. cf C. Inna and Dima

    http://codeforces.com/contest/374/problem/C 记忆化搜索,题意:求按照要求可以记过名字多少次,如果次数为无穷大,输出Poor Inna!,如果不经过一次输出P ...

  5. 题解合集 (update on 11.5)

    收录已发布的题解 按发布时间排序. 部分可能与我的其他文章有重复捏 qwq . AtCoder for Chinese: Link ZHOJ: Link 洛谷 \(1\sim 5\) : [题解]CF ...

  6. Dima and Salad(完全背包)

    Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. Codeforces Round #214 (Div. 2) C. Dima and Salad 背包

    C. Dima and Salad   Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to ...

  8. Codeforces Round #214 (Div. 2) C. Dima and Salad (背包变形)

    C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. CF Dima and Salad 01背包

    C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...

随机推荐

  1. C#基础_C#判断文件是否被打开

    1.用文件流判断 using System; using System.Collections.Generic; using System.IO; using System.Linq; using S ...

  2. NetCore性能优化

    NetCore性能优化2.非跟踪查询在只读方案中使用结果时,非跟踪查询十分有用,可以更快速地执行.增加AsNoTracking()表示非跟踪,如:var users = context.User.As ...

  3. c语言_二叉树的建立以及3种递归

    二叉树c语言的实现 二叉树的建立 二叉树的数据结构 typedef struct node{    int data;    struct node* left;    struct node* ri ...

  4. KingbaseES例程之快速删除表数据

    概述 快速删除表中的数据 delete语句删除数据 表中的数据被删除了,但是这个数据在硬盘上的真实存储空间不会被释放. 这种删除缺点是:删除效率比较低. 这种删除优点是:支持删除部分数据,支持回滚. ...

  5. 【学习笔记】Vin-Mono论文阅读笔记(一)

    VINS-Mono 概述 VINS-Mono VINS-Mono是由一个单目相机和一个低成本IMU组成的鲁棒通用的单目视觉惯性系统.通过融合预积分的IMU测量值和特征观测值来获得高精度的视觉惯性里程计 ...

  6. Elasticsearch:top_hits aggregation

    top_hits指标聚合器跟踪要聚合的最相关文档. 该聚合器旨在用作子聚合器,以便可以按存储分区汇总最匹配的文档. top_hits聚合器可以有效地用于通过存储桶聚合器按某些字段对结果集进行分组. 一 ...

  7. 清除已安装的rook-ceph集群

    官方文档地址:https://rook.io/docs/rook/v1.8/ceph-teardown.html 如果要拆除群集并启动新群集,请注意需要清理的以下资源: rook-ceph names ...

  8. Kibana:Canvas入门

  9. 01_Typora学习

    Typora学习 使用Typora 编辑器 一. 标题 一个#后加空格表示一级标题(快捷键Ctrl+1) 两个#后加空格表示二级标题(快捷键Ctrl+2) 以此类推,目前最多到六级标题(快捷键Ctrl ...

  10. 【面试题】Vue2动态添加路由 router.addRoute()

    Vue2动态添加路由 点击打开视频讲解更加详细 场景: 一般结合VueX和localstorage一起使用 router.addRoutes vue-router4后 已废弃:使用 router.ad ...