题目链接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. 线程有gil锁

    gil锁作用: 遇到阻塞( 比如 recv() , accept() )就切换

  2. Stall Reservations POJ - 3190(贪心)

    Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked ...

  3. Codeforces 1093D. Beautiful Graph【二分图染色】+【组合数】

    <题目链接> 题目大意: 给你一个无向图(该无向图无自环,且无重边),现在要你给这个无向图的点加权,所加权值可以是1,2,3.给这些点加权之后,要使得任意边的两个端点权值之和为奇数,问总共 ...

  4. Pedestrian Attributes Recognition Paper List

    Pedestrian Attributes Recognition Paper List  2018-12-22 22:08:55 [Note] you may also check the upda ...

  5. Spring BPP中优雅的创建动态代理Bean

    一.前言 本文章所讲并没有基于Aspectj,而是直接通过Cglib以及ProxyFactoryBean去创建代理Bean.通过下面的例子,可以看出Cglib方式创建的代理Bean和ProxyFact ...

  6. 深入理解原型链(Prototype chain) __proto__

    原型链(Prototype chain) 原型对象也是普通的对象,并且也有可能有自己的原型,如果一个原型对象的原型不为null的话,我们就称之为原型链(prototype chain). A prot ...

  7. BZOJ.2679.Balanced Cow Subsets(meet in the middle)

    BZOJ 洛谷 \(Description\) 给定\(n\)个数\(A_i\).求它有多少个子集,满足能被划分为两个和相等的集合. \(n\leq 20,1\leq A_i\leq10^8\). \ ...

  8. MVC微型框架---------学习

    1.单一入口机制 是指在web程序中 所有的请求都指向一个脚本文件 2.工厂模式的概念精髓:工厂类就是对类的封装,类是对方法的封装,方法是对实现过程的封装调用当前类的静态方法,规范的写法是使用 sel ...

  9. BZOJ4613 : [Wf2016]Longest Rivers

    对于每条河流,要让它排名最靠前,那么显然它必须要延伸到根. 设第$i$条河流到根的距离为$d[i]$,对于每个节点,如果存在一条河流比$d[i]$长,那么让它延伸会使答案最小,否则要选择一条最短的河流 ...

  10. BZOJ4223 : Tourists

    将位置划分成$O(m)$段区间,每段最早被阻挡的时间可以用堆维护. 那么每段区间对询问的贡献独立,扫描线处理即可. 时间复杂度$O(m\log m)$. #include<cstdio> ...