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. 用PowerShell脚本实现对SharePoint页面Title的修改

    存在这样一种情况,对应的page已经部署到product的SharePoint环境中,那么在部署下一个版本的时候就不允许把已经创建好的page删除再创建,因此page中修改过的属性就不能再次部署到Sh ...

  2. filter应用案例二:权限控制

    filter可以用来进行权限控制,比如admin文件夹下的文件只允许管理员进入,那么,可以给admin文件夹加上一个过滤器: 简单代码示例: import java.io.IOException; i ...

  3. ffmpeg处理RTMP流媒体的命令 发送流媒体的命令(UDP,RTP,RTMP)

    将文件当做直播送至live ffmpeg -re -i localFile.mp4 -c copy -f flv rtmp://server/live/streamName   re限制输出速率,按照 ...

  4. 【虚拟机】苹果虚拟机mac10.11.6+Xcode8.1

    [虚拟机]苹果虚拟机mac10.11.6+Xcode8.1本虚拟机加装Xcode8.1,方便大家更好学习Swift3.0语言以及iOS开发.安装注意事项:第一步:确认硬件:1.确认主板以及cpu支持虚 ...

  5. Transform组件C#游戏开发快速入门

    Transform组件C#游戏开发快速入门大学霸 组件(Component)可以看作是一类属性的总称.而属性是指游戏对象上一切可设置.调节的选项,如图2-8所示.本文选自C#游戏开发快速入门大学霸   ...

  6. zepto的bug1

    给页面<a>标签绑定了tap事件,在移动设备上点击按钮貌似一切正常,可以正常响应. 但是,把页面上下滑动几次之后,或者在滑动时手指滑动出移动屏幕之外,之后再点击按钮,就会发现第一次点击的时 ...

  7. 安卓微POS-PDA手持终端,支持离线在线联网销售开单;移动开单 盘点 功能

    采购单.采购退货单  销售单.销售退货单.收款.优惠.赠品等操作实现盘点作业(多台设备同时作业,相同商品,数量累计) 现场打印票据 实现采购订单.采购单.采购退货单.销售订单.销售单.销售退货单验货没 ...

  8. BZOJ 2631 Tree ——Link-Cut Tree

    [题目分析] 又一道LCT的题目,LCT只能维护链上的信息. [代码] #include <cstdio> #include <cstring> #include <cs ...

  9. js运动框架tween

    <!DOCTYPE html> <html> <head> <title>myAnimate</title> <style> * ...

  10. js常用函数陆续总结

    1.each() 方法规定为每个匹配元素规定运行的函数. $.each(data,function(index,item){ sb.append(item.answerNum); } $(" ...