http://poj.org/problem?id=1154

LETTERS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9519   Accepted: 4252

Description

A single-player game is played on a rectangular board divided in R rows and C columns. There is a single uppercase letter (A-Z) written in every position in the board.
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 first line of the input contains two integers R and C, separated by a single blank character, 1 <= R, S <= 20.

The following R lines contain S characters each. Each line represents one row in the board.

Output

The first and only line of the output should contain the maximal number of position in the board the figure can visit.

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(最长路径)的更多相关文章

  1. 牛客练习赛27-----C.水图(DFS求最长路径)

    传送门 来源:牛客网题目描述:小w不会离散数学,所以她van的图论游戏是送分的小w有一张n个点n-1条边的无向联通图,每个点编号为1~n,每条边都有一个长度小w现在在点x上她想知道从点x出发经过每个点 ...

  2. dfs+记忆化搜索,求任意两点之间的最长路径

    C.Coolest Ski Route 题意:n个点,m条边组成的有向图,求任意两点之间的最长路径 dfs记忆化搜索 #include<iostream> #include<stri ...

  3. 【二叉树-最长路径系列(任意路径)】直径、最长同值路径、 最大路径和(DFS、树形DP)

    总述 这类题目都是求一个最长路径,这个路径可以不经过根节点. 使用dfs(即递归地遍历树)的方法.维护一个全局最长路径max作为最终结果,而递归方法dfs返回的是含根节点的最长路径.(若不使用全局变量 ...

  4. Codefroces Gym 100781A(树上最长路径)

    http://codeforces.com/gym/100781/attachments 题意:有N个点,M条边,问对两两之间的树添加一条边之后,让整棵大树最远的点对之间的距离最近,问这个最近距离是多 ...

  5. 【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)

    Walking Race   Description flymouse's sister wc is very capable at sports and her favorite event is ...

  6. 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 ...

  7. ubuntu 终端设置(颜色与长路径)

    Linux给人最大的享受就是可以根据个人喜好去定制令自己舒服的系统配置,像终端颜色的设置就是一个典型的例子. 图1 系统默认状态下的终端显示     在没有经过自定义配置的终端下工作久了,难免容易疲劳 ...

  8. hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. AOE网上的关键路径(最长路径 + 打印路径)

    题目描述 一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图.     AOE(Activity On Edge)网:顾名思义,用边表示活动的网,当然它也是DAG ...

随机推荐

  1. goland使用:导入一个github开源项目tidb

    概要:在windos下的IDEA 的go语言的编辑器 goland的使用,导入github上面的开源项目. 问题: 下载好goland之后,open project打开一个下载好的githubhub项 ...

  2. apache重写规则简单理解

    1.前提:开启apache重写,并把httpd.conf里的相关的AllowOverride denied改为AllowOverride all 2.重写规则可写在项目根目录的.htaccess文件或 ...

  3. html+css+javascript学习记录1

    <p> 最近在学一部分前端,知识点很多,却没怎么系统地应用过,因而理解可能不够深吧.所以我想做点片段似的东西,不懂的再在网上搜一搜,这样可能会更有意思点,所以做了这个记录,希望自己坚持下去 ...

  4. struts2+jsp 遍历 <s:iterator><s:property>

    直接把list用request传到jsp页面 <s:iterator var="u" value="#request.users"> <tr& ...

  5. TreeMap定制排序和自然排序

    TreeMap定制排序和自然排序自然排序是实现Comparable接口的方法.代码如下: @Override public int compareTo(Object o) { if (o instan ...

  6. 【HDOJ6684】Rikka with Game(博弈)

    题意:有一个由小写字母组成的字符串,A和B两个人轮流操作,A想让序列的字典序最大,B想最小 两人都能任意选择一位上的字母将其右移一位(a->b,b->c……z->a)或者直接结束游戏 ...

  7. Photon学习(一)——Photon Networking Free网络组件学习

    一般前端untiy程序员都很想自己学会后端网络编程,这样一个人就可以把前后端都做了,做网络游戏可比单机游戏好玩多了,笔者我对喜欢的就是mmo多人对战游戏,一起组队打副本,一起体验多人对战的乐趣.从业以 ...

  8. p5341 [TJOI2019]甲苯先生和大中锋的字符串

    分析 TJOI白给题 建出sam,对于每个点如果它的子树siz和等于k 那么对于这个满足的点它有贡献的长度一定是一个连续区间 直接差分即可 代码 #include<bits/stdc++.h&g ...

  9. machine learning 之 Recommender Systems

    整理自Andrew Ng的machine learning 课程 week 9. 目录: Problem Formulation(问题的形式) Content Based Recommendation ...

  10. 【CDN+】 Spark入门---Handoop 中的MapReduce计算模型

    前言 项目中运用了Spark进行Kafka集群下面的数据消费,本文作为一个Spark入门文章/笔记,介绍下Spark基本概念以及MapReduce模型 Spark的基本概念: 官网: http://s ...