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 ...
随机推荐
- [好好学习]在VMware中安装Oracle Enterprise Linux (v5.7) - (1/5)
在想到Oracle Enterprise Linux,第一时间就是在Oracle官网上找到资源,但是,出现以下图片
- Bootstrap框架如何设置导入链接
bootstrap文件划分: 导入规则: 方式一:可以先下载 ----https://v3.bootcss.com/ (1) 注意事项: (2) (3) 注意1:bootstrap中的js文件依赖于j ...
- 【记录】mybatis mapper.xml 基础
<choose> <when test=""> //... </when> <otherwise> //... </other ...
- 2018-5-5-UWP-和-WPF-对比
title author date CreateTime categories UWP 和 WPF 对比 lindexi 2018-05-05 17:23:33 +0800 2018-04-16 20 ...
- 07.SUSE Linux 系统本地yum源配置
SUSE Linux 系统 1.新建本地源存储目录root@suse:mkdir /mnt/SUSE_LOCAL_SOURCE 2.创建zypper本地源root@suse:zypper ar fil ...
- Sass:@at-root
@at-root 从字面上解释就是跳出根元素.当你选择器嵌套多层之后,想让某个选择器跳出,此时就可以使用 @at-root.来看一个简单的示例: .a { color: red; .b { color ...
- springboot集成hibernate
package com.jxd.Boot.hibernate.dao.impl; import java.util.List; import javax.persistence.EntityManag ...
- 在PHPstorm上安装thinkPHP
>环境:ubuntu php7.2 phpstorm https://blog.csdn.net/roukmanx/article/details/85646174 https://www.ka ...
- 带你一文搞定 IO 流相关核心问题
问:简单谈谈 Java IO 流各实现类的特性? 答:java.io 包下面的流基本都是装饰器模式的实现,提供了各种类型流操作的便携性,常见的流分类如下. 以二进制字节方式读写的流: InputStr ...
- JAVA学习笔记--ClassLoader
仅先摘要书中内容以记之,后续也许需要更深入的去探索.先推荐篇博文http://blog.csdn.net/xyang81/article/details/7292380 6.9 初始化和类装载 在许多 ...