LETTERS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8119   Accepted: 3661

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

题意:

给出一个大写字母矩阵,一开始位于左上角,可以上下左右移动但不能移动到曾经经过的字母,问最多可以经过几个字母。

思路: 基础DFS,以前做这道题时,总是不理解怎么统计经过字母的个数和怎么让vis返回,现在重新做终于可以自己理解并解决了。

代码:

#include<iostream>

#include<string>

#include<cstdio>

#include<cmath>

#include<cstring>

using namespace std;

int r,s,sum,cnt,dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};

bool vis[30]; char map[30][30];

void dfs(int x,int y)

{

if(cnt>sum) sum=cnt;

vis[map[x][y]-'A']=1;

for(int i=0;i<4;i++)

{

int x1=x+dir[i][0];

int y1=y+dir[i][1];

if(x1>=1&&x1<=r&&y1>=1&&y1<=s&&!vis[map[x1][y1]-'A'])

{

cnt++;

dfs(x1,y1);

vis[map[x1][y1]-'A']=0;

cnt--;

}

}

}

int main()

{

while(cin>>r>>s)

{

for(int i=1;i<=r;i++)

{

for(int j=1;j<=s;j++)

cin>>map[i][j];

}

memset(vis,0,sizeof(vis));

sum=1;cnt=1;

dfs(1,1);

cout<<sum<<endl;

}

return 0;

}

 

 

POJ1154的更多相关文章

  1. poj1154 【DFS】

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

  2. poj练习题的方法

    poj1010--邮票问题 DFSpoj1011--Sticks dfs + 剪枝poj1020--拼蛋糕poj1054--The Troublesome Frogpoj1062--昂贵的聘礼poj1 ...

  3. 搜索入门练习题9 LETTERS 题解

    题目出处:<信息学奥赛一本通>第五章上机练习1 或者 POJ1154 题目描述 给出一个 \(R\times S\) 的大写字母矩阵,一开始你所处的位置在左上角,你可以向上下左右四个方向移 ...

随机推荐

  1. Java中继承thread类与实现Runnable接口的区别

    Java中线程的创建有两种方式: 1.  通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中 2.  通过实现Runnable接口,实例化Thread类 在实际应用中, ...

  2. ASP.NET MVC5 网站开发实践(一) - 项目框架(转)

    前几天算是开题了,关于怎么做自己想了很多,但毕竟没做过项目既不知道这些想法有无必要,也不知道能不能实现,不过邓爷爷说过“摸着石头过河”吧.这段时间看了一些博主的文章收获很大,特别是@kencery,依 ...

  3. 利用pushState开发无刷页面切换

    转载:http://www.cnblogs.com/flash3d/archive/2013/10/23/3384823.html 实现目标 页面的跳转(前进后退,点击等)不重新请求页面 页面URL与 ...

  4. Eclipse 的 Debug 介绍与技巧【转载】

    没有任何程序员能够一气呵成的写出没有任何 Bug 的代码,所以很多程序员有相当一部分时间是花费在 Debug 上的,程序调试是每个程序员必须面对的工作.如何使用 Eclipse 进行有效的.尤其是高效 ...

  5. JS 中面向对象的5种写法

    //第1种写法 function Circle(r) { this.r = r; } Circle.PI = 3.14159; Circle.prototype.area = function() { ...

  6. <meta>元素

    HTML <meta> 元素 meta标签描述了一些基本的元数据. <meta> 标签提供了元数据.元数据也不显示在页面上,但会被浏览器解析. META元素通常用于指定网页的描 ...

  7. HDU4511 小明系列故事——女友的考验(AC自动机 + DP)

    题目大概说有平面有n个点,从1点出发走到n点,每一步只能走到序号比当前更大的点且走的序列不能包含给定的m个序列中的任何一个,问1走到n的最短路. 用m个序列建个AC自动机,后缀包含整个序列的结点标记一 ...

  8. 由addOneMember引发的思考

    addOneMember是一个方法,这个方法在两处地方重复了. 所以在修改页面的时候,发现修改了一处,如果是新手,肯定不会注意到另外一处有问题,他如果没有看清楚这个类到底整体怎样,那么他会犯的错误是就 ...

  9. 每天一个linux命令---telnet

    执行telnet指令开启终端机阶段作业,并登入远端主机. telnet的命令的格式: telnet  ip port 例1:  建立连接不成功 [richmail@portal bin]$ telne ...

  10. 关于在C#中构造函数中调用虚函数的问题

    在C#中如果存在类的继承关系,应避免在构造函数中调用虚函数.这是由于C#的运行机制造成的,原因如下: 新建一个类实例时,C#会先初始化该类(对类变量赋值,并将函数记在函数表中),然后再初始化父类.构造 ...