LETTERS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8976   Accepted: 4017

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
思路:
基础dfs,
实现代码:
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<list>
using namespace std;
#define ll long long
const int Mod = 1e9+;
const int inf = 1e9;
const int Max = 1e5+;
vector<int>vt[Max];
int dx[] = {-, , , };
int dy[] = { , , -, };
//void exgcd(ll a,ll b,ll& d,ll& x,ll& y){if(!b){d=a;x=1;y=0;}else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}}
//ll inv(ll a,ll n){ll d, x, y;exgcd(a,n,d,x,y);return (x+n)%n;} ��Ԫ
//int gcd(int a,int b) { return (b>0)?gcd(b,a%b):a; } ��С��Լ
//int lcm(int a, int b) { return a*b/gcd(a, b); } ������
int ans = ,n,m,vis[];
char mp[][]; void dfs(int x,int y,int cnt){
for(int i=;i<;i++){
int nx = dx[i] + x;
int ny = dy[i] + y;
if(nx>=&&nx<n&&ny>=&&ny<m&&vis[mp[nx][ny]-'A']==){
vis[mp[nx][ny]-'A'] = ;
ans = max(ans,cnt+);
dfs(nx,ny,cnt+);
vis[mp[nx][ny]-'A'] = ;
}
}
} int main()
{
cin>>n>>m;
memset(vis,,sizeof(vis));
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>mp[i][j];
}
}
vis[mp[][]-'A'] = ;
dfs(,,);
cout<<ans<<endl;
}

poj1154 【DFS】的更多相关文章

  1. 【第40套模拟题】【noip2011_mayan】解题报告【map】【数论】【dfs】

    目录:1.潜伏者 [map] 2.Hankson的趣味题[数论]3.mayan游戏[dfs] 题目: 1. 潜伏者(spy.pas/c/cpp)[问题描述]R 国和S 国正陷入战火之中,双方都互派间谍 ...

  2. Kattis - glitchbot 【DFS】

    Kattis - glitchbot [DFS] 题意 有一个机器人 刚开始在(0, 0),然后给出一个目标点,并且会给出一系列指令,但是其中会有一个指令是错误的.我们需要找出那个指令,并且改成正确的 ...

  3. HDU 6113 度度熊的01世界 【DFS】(2017"百度之星"程序设计大赛 - 初赛(A))

    度度熊的01世界 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. 【dfs】P1331 海战

    题目描述 在峰会期间,武装部队得处于高度戒备.警察将监视每一条大街,军队将保卫建筑物,领空将布满了F-2003飞机.此外,巡洋船只和舰队将被派去保护海岸线.不幸的是因为种种原因,国防海军部仅有很少的几 ...

  5. 【dfs】p1731 生日蛋糕

    1441:[例题2]生日蛋搞 [题目描述] 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.设从下往上数第i(1≤i≤M)层蛋糕是半径为Ri, 高 ...

  6. 【dfs】LETTERS

    1212:LETTERS [题目描述] 给出一个roe×colroe×col的大写字母矩阵,一开始的位置为左上角,你可以向上下左右四个方向移动,并且不能移向曾经经过的字母.问最多可以经过几个字母. [ ...

  7. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  8. 【dfs】BZOJ1703-[Usaco2007 Mar]Ranking the Cows 奶牛排名

    [题目大意] 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序,约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他 ...

  9. 【DFS】BZOJ3522-[Poi2014]Hotel

    [题目大意] 给出一棵树,求三个节点使得它们两两之间的距离相等,问共有多少种可能性? [思路] 显然,这三个节点是关于一个中心点对称地辐射出去的. 枚举中心点,往它的各个子树跑Dfs.tmp[i]表示 ...

随机推荐

  1. Android中AsyncTask的使用

    原文 https://blog.csdn.net/liuhe688/article/details/6532519 在Android中实现异步任务机制有两种方式,Handler和AsyncTask. ...

  2. 搭建SSH服务

    1.安装 ssh-server 通过命令进行安装:sudo apt-get install openssh-server 在安装时遇到问题,根据提示,执行命令:sudo apt-get update, ...

  3. Kubernetes-v1.12.0基于kubeadm部署

    1.主机规划 #master节点(etcd/apiserver/scheduler/controller manager)master.example.cometh0: 192.168.0.135et ...

  4. Foreach循环输出索引值

    循环输邮索引值,使用for是没有任何问题: class Bh { public string[] str { get; set; } public void TestFor() { ; i < ...

  5. (转)Linux的用户和用户组管理

    原文 Linux是个多用户多任务的分时操作系统,所有一个要使用系统资源的用户都必须先向系统管理员申请一个账号,然后以这个账号的身份进入系统.用户的账号一方面能帮助系统管理员对使用系统的用户进行跟踪,并 ...

  6. SpringMVC之单/多文件上传

    1.准备jar包(图标所指必备包,其他按情况导入) 2.项目结构 3.SingleController.java(控制器代码单文件和多文件) package com.wt.uplaod; import ...

  7. 5分钟入门自动化测试——你应该学会的Postman用法(2)

    前言 之前的一篇文章<你应该学会的Postman用法>,主要介绍了postman的一些高级的用法,便于日常开发和调试使用,本文的基础是对postman的基本使用以及一些高级用法有一定的了解 ...

  8. VS2013软件的安装和单元测试

    VS2013是什么? 微软在Builder 2013开发者大会上发布了Visual Studio 2013预览版,并且发布其程序组件库.NET 4.5.1的预览版.该软件已于北京时间2013年11月1 ...

  9. 【ML】ICLR2016_Delving Deeper into Convolutional Networks

    ICLR2016_DELVING DEEPER INTO CONVOLUTIONAL NETWORKS Note here: Ballas recently proposed a novel fram ...

  10. 个人博客-week7

    团队任务收获及个人感想 团队任务已经进行了一个多月的时间,我很荣幸能和软剑攻城队的小伙伴们度过这一个月的开发时光.在这一个月的时间里,我亲身经历了一个软件从想法到实现,从创意到实体的过程.同时我也在和 ...