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

    今天看了一本书,书里有道题,题目很常见,排序,明了点说: 需求:输入:最多有n个正整数,每个数都小于n, n为107 ,没有重复的整数 输出:按升序排列 思路:假设有一组集合 {1,3,5,6,11, ...

  2. 关于css的全面学习笔记

    1.text-align 属性规定元素中的文本的水平对齐方式.该属性通过指定行框与哪个点对齐,从而设置块级元素内文本的水平对齐方式.通过允许用户代理调整行内容中字母和字之间的间隔,可以支持值 just ...

  3. 学习angularjs时遇到 XX is not a function

        第一次练习就直接没效果:   "后不能直接以 function XXXcontroller (){ code......}这样的方式直接注册监听器了. 以后必须angular.mod ...

  4. Python学习笔记02

      元组:圆括号的,不能进行赋值操作,即不可更改. 列表:方括号的,可以修改. 访问:均使用下标访问   # 元组是一个静态列表,初始化之后就不可以修改,可以试任意类型 tuple1 = ('a st ...

  5. 错误修改/etc/fstab,导致系统无法开机

    enter password or type control-D to continue 系统提示你输入root密码,而输入以后系统的所有文件是只读的,你无法修改 看下你的/etc/fstab这个目录 ...

  6. Silverlight

    http://kb.cnblogs.com/zt/silverlight/ http://www.cnblogs.com/gnielee/archive/2010/01/15/silverlight- ...

  7. 【转】Xcelsius2008 水晶易表问题 部分汇总

    要使用 Xcelsius 2008,需要安装 Adobe Flash 吗? 若要正常运行 Xcelsius 2008,必须安装 Adobe Flash Player 版本 9.如果在安装过程中没有安装 ...

  8. BZOJ2981 : [Poi2002]括号

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

  9. FastDFS原理

    转自:http://blog.chinaunix.net/uid-20196318-id-4058561.html 开源的轻量级分布式文件系统,由跟踪服务器(tracker server).存储服务器 ...

  10. ExtJs文件上传(Ext.ux.form.FileUploadField)

    Ext.ux.form.FileUploadField = Ext.extend(Ext.form.TextField, { /**  * @cfg {String} buttonText The b ...