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. Codeforces Round #143 (Div. 2) E. Cactus 无向图缩环+LCA

    E. Cactus   A connected undirected graph is called a vertex cactus, if each vertex of this graph bel ...

  2. windows下R语言在终端的运行

    在windows下可以有多种方式来运行R,R导论的这些章节给出一些详细的指导. 通常在环境变量离包含R的安装目录类似于R\R-3.1.2\bin\x64的情况下,就可以在CMD下运行R程序了 注意我这 ...

  3. [自动运维]ant脚本打包,上传文件到指定服务器,并部署

    1.根节点使用,表示根目录为当前目录,默认启动的target为build,项目名称为othersysm, <project basedir="." default=" ...

  4. [bzoj2118]墨墨的等式【dijk+堆】

    10/30的update:如果是冲着dijk的板子来的,建议看多校联考contest中第二场day2的T2,那边的写法比较优秀... --------------------------------- ...

  5. PHP如何实现文件上传

    PHP如何实现文件上传 1.表单部分  允许用户上传文件,在HTML表单的声明中要加上一个上传的属性:  enctype = 'multipart/form-data'  表单的method必须是PO ...

  6. 疯狂java学习笔记之面向对象(一) - 定义类、方法、构造器

    Java面向对象 1.定义类 2.创建对象.调用方法 类和对象: 某一类对象的概念定义. 比如:人类 - 抽象出来的概念(不特指某个人) 对象 - 在类的概念下产生的一个实例,它就是一个对象了. ja ...

  7. Docker搭建便捷的开发者环境

    你可能遇到这样的场景:开发软件时,需要像数据库(mysql,mongodb).消息系统(rabbitmq).缓存服务(redis)等其它依赖服务.当然我们可以找台机器,一步步安装依赖,然后把所有依赖的 ...

  8. SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int

    --SQL批量更新数据库中所有用户数据表中字段类型为tinyint为int --关键说明:--1.从系统表syscolumns中的查询所有xtype='48'的记录得到类型为[tinyint]的字段- ...

  9. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering

    Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...

  10. BZOJ2981 : [Poi2002]括号

    对于最终加入了括号的序列,对其求中缀表达式,建树. 可以发现$n-1$个运算符DFS序递增,且若一个-上方往左走了奇数次,则它就是+,否则就是-. 所以考虑DP,设$f[i][j]$表示考虑了前$i$ ...