题目链接http://bailian.openjudge.cn/practice/1154/
总时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入
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.
输出
The first and only line of the output should contain the maximal number of position in the board the figure can visit.
样例输入
3 6
HFDFFB
AJHGDH
DGAGEH
样例输出
6
来源
Croatia OI 2002 Regional Competition - Juniors

算法:深搜

代码一:

 #include<iostream>
using namespace std;
int bb[]={},s,r,sum=,s1=;
char aa[][];
int dir[][]={-,,,,,-,,};
void dfs(int a,int b)
{
int a1,b1;
if(s1>sum) sum=s1; //更新最大数值
for(int i=;i<;i++)
{
a1=a+dir[i][]; //用bb数组记录访问过的字母
b1=b+dir[i][];
if(a1>=&&a1<s&&b1>=&&b1<r&&!bb[aa[a1][b1]-'A'])
{
s1++;
bb[aa[a1][b1]-'A']=; //如果在这条单线上没有记录改字母被访问过,则总数++;
dfs(a1,b1); //第一个字母总要被访问,所以不用回溯;
bb[aa[a1][b1]-'A']=; //回溯反标记
s1--; //临时记录恢复
}
}
}
int main()
{
cin>>s>>r;
for(int i=;i<s;i++)
for(int j=;j<r;j++)
cin>>aa[i][j];
bb[aa[][]-'A']=;
dfs(,);
cout<<sum<<endl;
return ;
}

代码二:

 #include <stdio.h>
#include<iostream>
using namespace std;
int qq[][];
int fx[]={,,-,},fy[]={,-,,},pd[],sum,ans;//右下左上
void fun(int x,int y)
{
if(ans<sum)ans=sum;
if(qq[x][y]==) return;
for(int i=;i<;i++)
{
if(qq[x+fx[i]][y+fy[i]]!=&&pd[qq[x+fx[i]][y+fy[i]]]==)
{
sum++;
pd[qq[x+fx[i]][y+fy[i]]]=;
fun(x+fx[i],y+fy[i]);
pd[qq[x+fx[i]][y+fy[i]]]=;
sum--;
}
}
}
int main(int argc, char *argv[])
{
int r,s;
scanf("%d%d",&r,&s);
for(int i=;i<=r;i++)
for(int j=;j<=s;j++)
{
char t;
cin>>t;
qq[i][j]=t-'A'+;
}
pd[qq[][]]=;
sum=ans=;
fun(,);
printf("%d",ans);
return ;
}

1154:LETTERS的更多相关文章

  1. poj 1154 letters (dfs回溯)

    http://poj.org/problem?id=1154 #include<iostream> using namespace std; ]={},s,r,sum=,s1=; ][]; ...

  2. dfs(最长路径)

    http://poj.org/problem?id=1154 LETTERS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: ...

  3. 九度OJ 1154:Jungle Roads(丛林路径) (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:832 解决:555 题目描述: The Head Elder of the tropical island of Lagrishan has ...

  4. 九度 题目1154:Jungle Roads

    题目描写叙述: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid mon ...

  5. [LeetCode] Remove Duplicate Letters 移除重复字母

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  6. 316. Remove Duplicate Letters

    Given a string which contains only lowercase letters, remove duplicate letters so that every letter ...

  7. Remove Duplicate Letters I & II

    Remove Duplicate Letters I Given a string which contains only lowercase letters, remove duplicate le ...

  8. [CareerCup] 18.13 Largest Rectangle of Letters

    18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangl ...

  9. LeetCode Remove Duplicate Letters

    原题链接在这里:https://leetcode.com/problems/remove-duplicate-letters/ 题目: Given a string which contains on ...

随机推荐

  1. JavaEE 之 DBCP

    1.DBCP a.定义:DBCP(DataBase Connection Pool)数据库连接池,是java数据库连接池的一种,由Apache开发,通过数据库连接池,可以让程序自动管理数据库连接的释放 ...

  2. PostgreSQL 的命令行工具 psql 的常用命令

    1. 连接服务器: psql -h 192.168.1.88 -U username -d databasename 2. 切换数据库: \c dbname 3. 列出所有数据库: \l 4. 列出所 ...

  3. Python常用模块--configparser

    作用: 官方:实现基本配置语言的类,该语言提供类似于Microsoft Windows INI文件中的结构.您可以使用它来编写可由最终用户轻松定制的Python程序. 通俗的说:该模块用于系统配置文件 ...

  4. Java-从Double类型精度丢失认识BigDecimal

    Java-从Double类型精度丢失认识BigDecimal 参考资料 https://www.jianshu.com/p/07e3eeb90f18 https://zh.wikipedia.org/ ...

  5. SpringBoot拦截器

    在实际开发中,总存在着这样的场景,比如拦截请求的ip地址,或者在所有的请求都返回相同的数据,如果每一个方法都写出相同数据固然可以实现,但是随着项目的变大,重复的代码会越来越多,所以在这种情况我们可以用 ...

  6. class关键字

    class的数据类型为function,可以看做构造函数的另一种写法.事实上,类的所有方法都定义在类的prototype属性上面.一.声明class class Animal { constructo ...

  7. 11.6 正睿停课训练 Day17

    目录 2018.11.6 正睿停课训练 Day17 A chinese(思路 计数) B physics(单调队列/剪枝 DP) C chemistry(期望 DP) 考试代码 A B C 2018. ...

  8. Centos6 安装RabbitMq3.7.7

    安装包准备官网地址:    Erlang安装包下载:https://www.erlang-solutions.com/resources/download.html    RabbitMq安装包下载: ...

  9. Java -- 内部类(一)

    什么是内部类 将一个类的定义放在另一个类的定义内部,这就是内部类.在Java中内部类主要分为成员内部类.局部内部类.匿名内部类.静态内部类.举个栗子: public class A { public ...

  10. 面试知识点——Java

    目录 Java容器 hashmap实现原理 java多线程 jvm内存模型 java 垃圾回收机制 对象存活状态检查 垃圾收集算法 垃圾收集器 内存分配与回收策略 java nio Java容器 ha ...