博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LETTERS(搜索题)
阅读量:7230 次
发布时间:2019-06-29

本文共 2291 字,大约阅读时间需要 7 分钟。

LETTERS
Time Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u
Submit

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 6HFDFFBAJHGDHDGAGEH

Sample Output

6
1 #include 
2 #include
3 #include
4 using namespace std; 5 6 int r, c, ans; 7 const int move[4][2] = {
{
0, 1}, {
0, -1}, {-1, 0}, {
1, 0}}; 8 bool VisLetter[26], VisPos[21][21]; 9 char board[21][21];10 11 bool Judge(int i, int j)12 {13 return (!VisLetter[board[i][j] - 'A'] && !VisPos[i][j] && i >= 0 && i < r && j >= 0 && j < c);14 }15 16 void DFS(int i, int j, int cnt)17 {18 VisLetter[board[i][j] - 'A'] = VisPos[i][j] = 1;19 for(int k = 0; k < 4; k++)20 {21 int ii = i + move[k][0];22 int jj = j + move[k][1];23 if(Judge(ii, jj))24 {25 DFS(ii, jj, cnt + 1);26 VisLetter[board[ii][jj] - 'A'] = VisPos[ii][jj] = 0;27 }28 }29 if(ans < cnt) ans = cnt;30 }31 32 int main()33 {34 while(scanf("%d %d", &r, &c) != EOF)35 {36 ans = 1;37 for(int i = 0; i < r; i++)38 scanf("%s", board[i]);39 memset(VisLetter, 0, sizeof(VisLetter));40 memset(VisPos, 0, sizeof(VisPos));41 DFS(0, 0, 1);42 printf("%d\n", ans);43 }44 }

 

转载地址:http://jqsfm.baihongyu.com/

你可能感兴趣的文章
HDFS基本原理及数据存取实战
查看>>
j2ee页面静态化方案encache web cache框架详解1
查看>>
php高级注入
查看>>
[硬件]三维点云数据获取
查看>>
nagios安装配置
查看>>
bzoj 2763 [JLOI2011]飞行路线 Dijikstra 分层
查看>>
HEOI2018 游记
查看>>
UITableViewCell 取消选中的蓝色背景
查看>>
MFC DestroyWindow、OnDestroy、OnClose 程序关闭相关
查看>>
hibernate理解
查看>>
第二篇第五章防火防烟分区于分隔
查看>>
POJ 2387 Til the Cows Come Home
查看>>
POJ 1733 Parity game
查看>>
apply函数用法
查看>>
[转载] Knowledge Management and Engineering——07 PROMOTE Methodology
查看>>
deepin 2014 安装后 ,grub出错
查看>>
DevExpress.XtraGrid 导出文本的bug
查看>>
CentOS 7 系统初始化设置
查看>>
【树莓派智能门锁】使用脚本控制GPIO来开锁【4】
查看>>
转载---- 使用opencv源码自己编制android so库的过程
查看>>