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线程之——synchronized的注意细节

    我在学习synchronized的时候,十分好奇当一个线程进入了一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它方法? 然后就做了个实验(实验代码最后贴出),最后得到了如下 ...

  2. javascript的笔记精简版

    在写javascript的代码时一定要用单引号或者双引号括起来,不带引号的话就以字符串来处理 在javascript里面不能以纯数字或者click命名函数或者变量 要想修改标签的属性,在html里面怎 ...

  3. 通信原理实践(二)——幅度调制

    一.幅度调制,并画出时域和频域波形 1.代码如下: function [ p_n ] = AM_func( N,fs,fm,Am,fc,Ac,Ma ) %UNTITLED 此处显示有关此函数的摘要 % ...

  4. Hibernate 延迟加载

    一.什么是延迟加载? 延迟加载是指当应用程序想要从数据库获取对象时(在没有设置lazy属性值为false),Hibernate只是从数据库获取符合条件的对象的OId从而生成代理对象,并没有加载出对象访 ...

  5. JSHint配置详解

    Also available on Github JSHint配置详解 增强参数(Enforcing Options) 本类参数设为true,JSHint会产生更多告警. bitwise 禁用位运算符 ...

  6. centos7安装redis3.2.5

    安装redis 1官方介绍 Installation Download, extract and compile Redis with: $ wget http://download.redis.io ...

  7. 【转】Spark-Sql版本升级对应的新特性汇总

    Spark-Sql版本升级对应的新特性汇总 SparkSQL的前身是Shark.由于Shark自身的不完善,2014年6月1日Reynold Xin宣布:停止对Shark的开发.SparkSQL抛弃原 ...

  8. js实现页面的自动读秒跳转

    <!-- 代码片段A --> <!-- 倒计时跳转 --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Trans ...

  9. hdu 2669 Romantic

    Romantic Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  10. 使用python实现栈和队列

    1.使用python实现栈: class stack(): def __init__(self): self.stack = [] def empty(self): return self.stack ...