【题目】LETTERS
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.
输入
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.
输出
The first and only line of the output should contain the maximal number of position in the board the figure can visit.
样例输入
3 6
HFDFFB
AJHGDH
DGAGEH
样例输出
6
翻译(百度):
单人游戏是在一个矩形的棋盘上进行的,分为R行和C列。有一个大写字母(A-Z)写在棋盘上的每个位置。
在游戏开始前,棋盘左上角有一个数字(第一行,第一列)。在每一个动作中,玩家可以移动这个数字到相邻位置之一(上、下、左或右)。唯一的限制是一个图形不能访问同一个字母标记的位置两次。
这个游戏的目标是尽可能多地移动。
编写一个程序,计算一个游戏中人物可以访问的棋盘中的最大位置数。
输入
输入的第一行包含两个整数r和c,用一个空白字符分隔,1个< = r,s=20。
下面的R行包含S字符。每行代表棋盘中的一行。
输出
输出的第一行和唯一行应该包含该行可以访问的板中的最大位置数。
【思路】
深搜。
【代码】
#include<iostream> #include<cstring> using namespace std; int sum=0,ma=0,h,l; char zhen[30][30]; char biu[30]; int so(int x,int y) { if(ma<sum)ma=sum; if(biu[zhen[x][y]-'A']!=1) { sum++; biu[zhen[x][y]-'A']=1; if(x-1>=1)so(x-1,y); if(x+1<=h)so(x+1,y); if(y-1>=1)so(x,y-1); if(y+1<=l)so(x,y+1); biu[zhen[x][y]-'A']=0; sum--; } } int main() { int i,j; cin>>h>>l; for(i=1;i<=h;i++) for(j=1;j<=l;j++) cin>>zhen[i][j]; memset(biu,0,sizeof(biu)); so(1,1); cout<<ma<<endl; }