题面传送门

解决思路

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

首先把每一个字母转化为数字,然后对于每一个点枚举四个方向,如果有下一个字母,就向那个点建一条边,可以用 \(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. 并发与并行,同步和异步,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang并发编程之GoroutineEP13

    如果说Go lang是静态语言中的皇冠,那么,Goroutine就是并发编程方式中的钻石.Goroutine是Go语言设计体系中最核心的精华,它非常轻量,一个 Goroutine 只占几 KB,并且这 ...

  2. Docker网络讲解 及实验redis集群部署

    理解docker0 准备工作:清空所有的容器,清空所有的镜像 docker rm -f $(docker ps -a -q) # 删除所有容器 docker rmi -f $(docker image ...

  3. VS Code 之KoroFileHeader插件

    设置 在vscode左下角点击设置按钮,选择"设置",然后输入"fileheader", 文件头部注释:Fileheader:custom Made 函数注释: ...

  4. Sentinel 源码分析-限流原理

    1. git clone senetinel 源码到本地,切换到release1.8分支 2. 找到FlowQpsDemo.java, 根据sentinel自带的案例来学习sentinel的原理 3. ...

  5. 使用SSH连接Windows Server

    之前发过一篇在Windows Server上启用SSH服务器的文章.最近正好有这个需求,需要使用密钥免密登录服务器,试了一下,发现之前的方法不行了.需要再修正一些文件权限. 需要使用Repair-Au ...

  6. Flink基础概念入门

    Flink 概述 什么是 Flink Apache Apache Flink 是一个开源的流处理框架,应用于分布式.高性能.高可用的数据流应用程序.可以处理有限数据流和无限数据,即能够处理有边界和无边 ...

  7. 【Spring】Spring bean中id和name的差异

    id和name都是spring 容器中中bean 的唯一标识符. id: 一个bean的唯一标识 , 命名格式必须符合XML ID属性的命名规范 name: 可以用特殊字符,并且一个bean可以用多个 ...

  8. GitHub desktop常见问题及解决办法

    1.There are unresolved conflicts in the working directory. 问题出现:A台电脑push代码后,可能新建了分支,然后B电脑打开GitHub de ...

  9. SQL 时间范围和时间粒度

    前言 使用 SQL 进行业务数据计算时,经常会遇到两个概念:时间范围 和 时间粒度 .以 最近一天的每小时的用户访问人数 为例: 最近一天 是时间范围 每小时 是时间粒度 常见的时间范围:最近五分钟. ...

  10. 面试说:聊聊JavaScript中的数据类型

    前言 请讲下 JavaScript 中的数据类型? 前端面试中,估计大家都被这么问过. 答:Javascript 中的数据类型包括原始类型和引用类型.其中原始类型包括 null.undefined.b ...