dfs(最长路径)
http://poj.org/problem?id=1154
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9519 | Accepted: 4252 |
Description
Before the begging of the game there is a figure in the upper-left
corner of the board (first row, first column). In every move, a player
can move the figure to the one of the adjacent positions (up, down,left
or right). Only constraint is that a figure cannot visit a position
marked with the same letter twice.
The goal of the game is to play as many moves as possible.
Write a program that will calculate the maximal number of positions in the board the figure can visit in a single game.
Input
The following R lines contain S characters each. Each line represents one row in the board.
Output
Sample Input
3 6
HFDFFB
AJHGDH
DGAGEH
Sample Output
6
Source
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long ll ;
char a[][];
int n , m ;
int bx , by ;
int ans ;
int vis[];
int dir[][] = {{ , } , {- , } , { , } , { , -}}; void dfs(int x , int y , int cnt)
{
for(int i = ; i < ; i++)
{ int xx = x + dir[i][];
int yy = y + dir[i][];
if(vis[a[xx][yy] - 'A'] || xx <= || xx > n || yy <= || yy > m)
{
continue;
}
vis[a[xx][yy] - 'A'] = ;
dfs(xx , yy , cnt+); }
ans = max(ans , cnt);//for循环结束代表一条路径的结束。
vis[a[x][y] - 'A'] = ;//不同路径互不干扰
} int main()
{
while(~scanf("%d%d" , &n , &m))
{
for(int i = ; i <= n ; i++)
{
for(int j = ; j <= m ; j++)
{
cin >> a[i][j];
}
}
memset(vis , , sizeof(vis));
vis[a[][] - 'A'] = ;//将原地标记并加一
ans = ;
dfs( , , );
cout << ans << endl ;
} return ;
}
下面这个是参考学长的码。
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long ll ;
char a[][];
int n , m ;
int ans ;
int vis[];
int dir[][] = {{ , } , {- , } , { , } , { , -}}; void dfs(int x , int y , int cnt)
{
if(vis[a[x][y] - 'A'] || x <= || y <= || x > n || y > m)
{
cnt--;
return ;
}
else vis[a[x][y] - 'A'] = ;
dfs(x+ , y , cnt+);
dfs(x- , y , cnt+);
dfs(x , y+ , cnt+);
dfs(x , y- , cnt+);
ans = max(cnt , ans);
vis[a[x][y] - 'A'] = ;//每一条走到最后的路径都互不影响,所以要取消标记。
} int main()
{
while(~scanf("%d%d" , &n , &m))
{
for(int i = ; i <= n ; i++)
{
for(int j = ; j <= m ; j++)
{
cin >> a[i][j];
}
}
ans = -INF ;
memset(vis , , sizeof(vis));
dfs( , , );
cout << ans << endl ;
} return ;
}
dfs(最长路径)的更多相关文章
- 牛客练习赛27-----C.水图(DFS求最长路径)
传送门 来源:牛客网题目描述:小w不会离散数学,所以她van的图论游戏是送分的小w有一张n个点n-1条边的无向联通图,每个点编号为1~n,每条边都有一个长度小w现在在点x上她想知道从点x出发经过每个点 ...
- dfs+记忆化搜索,求任意两点之间的最长路径
C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...
- 【二叉树-最长路径系列(任意路径)】直径、最长同值路径、 最大路径和(DFS、树形DP)
总述 这类题目都是求一个最长路径,这个路径可以不经过根节点. 使用dfs(即递归地遍历树)的方法.维护一个全局最长路径max作为最终结果,而递归方法dfs返回的是含根节点的最长路径.(若不使用全局变量 ...
- Codefroces Gym 100781A(树上最长路径)
http://codeforces.com/gym/100781/attachments 题意:有N个点,M条边,问对两两之间的树添加一条边之后,让整棵大树最远的点对之间的距离最近,问这个最近距离是多 ...
- 【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)
Walking Race Description flymouse's sister wc is very capable at sports and her favorite event is ...
- Going from u to v or from v to u? POJ - 2762(强连通 有向最长路径)
In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, an ...
- ubuntu 终端设置(颜色与长路径)
Linux给人最大的享受就是可以根据个人喜好去定制令自己舒服的系统配置,像终端颜色的设置就是一个典型的例子. 图1 系统默认状态下的终端显示 在没有经过自定义配置的终端下工作久了,难免容易疲劳 ...
- hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】
Computer Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- AOE网上的关键路径(最长路径 + 打印路径)
题目描述 一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图. AOE(Activity On Edge)网:顾名思义,用边表示活动的网,当然它也是DAG ...
随机推荐
- sqoop简单使用
一,通过sqoop将MySQL里面的数据加载到HDFS 先查看有哪些数据库 查看表person sqoop list-databases --connect jdbc:mysql://ly-p2p4: ...
- java 中Shallow Heap与Retained Heap的区别
Shallow Size Shallow Size是对象本身占据的内存的大小,不包含其引用的对象.对于常规对象(非数组)的Shallow Size由其成员变量的数量和类型来定,而数组的ShallowS ...
- 基于 VirtualApp 结合 whale hook框架实现hook第三方应用
要点 1. whale hook framework 使用示例: 2. 参考项目:VirtualHook: 3. 按照 VirtualHook 修改 VirtualApp: 4. 编写 hook pl ...
- vue Base64图片压缩上传OSS
this.compress(result, 800, 0.5).then(val => { //得到压缩图片 let data = val; that.file = that.dataURLto ...
- OpenVINO 安装及使用
安装 https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_linux.html 使用 文档 ...
- 06Web服务
1.web开发入门 1.1 引入 软件结构分类: CS结构:客户端和服务器端 特点: 1)必须安装特点的客户端程序 2)服务器端升级,客户端同步升级 BS结构:浏览器和服务器端 特点: 1)不需要安装 ...
- 2019hdu多校3 hdu4893(线段树单点 区间更新
补这题主要是因为第三个操作要维护区间,而不是点,否则会T. https://vjudge.net/problem/HDU-4893 题意:输入n.q.表示有n个数,初始化默认这n个数都为零,有q次操作 ...
- .OnCommand mfc
.OnCommand是响应WM_COMMAND消息的,一般是响应控件和菜单的命令消息时使用. 如果 WM_COMMAND 来自控件的话 lParam 就是发送这个 WM_COMMAND 消息的控件的句 ...
- TPS、QPS和系统吞吐量的区别和理解
参考:https://blog.csdn.net/u010889616/article/details/83245695 一.QPS/TPSQPS:Queries Per Second意思是“每秒查询 ...
- 29 基于PCL的点云平面分割拟合算法技术路线(针对有噪声的点云数据)
0 引言 最近项目中用到了基于PCL开发的基于平面的点云和CAD模型的配准算法,点云平面提取采用的算法如下. 1 基于PCL的点云平面分割拟合算法 2 参数及其意义介绍 (1)点云下采样 1. 参数: ...