题目如下。这题目..反正我看不懂。
题意:反正就是有一个图,同一字母只能经过一次,求最多的字母个数..
总时间限制:1000ms
内存限制:
65536kB
描述
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
来源
Croatia OI 2002 Regional Competition - Juniors
我的想法就是再开一个数组标记字母有没有被访问过.然后用dfs来做这道题.
递归上下左右4个方向.
代码如下.
#include
int r,c,m; char a[25][25]; bool f[30]; void fuc(int x,int y,int s) { // if(x<0||x==r||y<0||y==c) // return; // printf("%c\n",a[x][y]); if(x<0||x==r||y<0||y==c||f[a[x][y]-64]) { return; } f[a[x][y]-64]=1; fuc(x,y+1,s+1); fuc(x,y-1,s+1); fuc(x+1,y,s+1); fuc(x-1,y,s+1); f[a[x][y]-64]=0; if(s>m) m=s; } int main() { int i; scanf("%d%d",&r,&c); for(i=0;i<r;i++) scanf("%s",a[i]); //f[a[0][0]-64]=1; fuc(0,0,1); printf("%d",m); } int r,c,m; char a[25][25]; bool f[30]; void fuc(int x,int y,int s) { // if(x<0||x==r||y<0||y==c) // return; // printf("%c\n",a[x][y]); if(x<0||x==r||y<0||y==c||f[a[x][y]-64]) { return; } f[a[x][y]-64]=1; fuc(x,y+1,s+1); fuc(x,y-1,s+1); fuc(x+1,y,s+1); fuc(x-1,y,s+1); f[a[x][y]-64]=0; if(s>m) m=s; } int main() { int i; scanf("%d%d",&r,&c); for(i=0;i<r;i++) scanf("%s",a[i]); //f[a[0][0]-64]=1; fuc(0,0,1); printf("%d",m); }
想起来我第4个递归调用时..因为复制粘贴的失误..害我WA了很久..
然后参数什么的应该还是很好理解的吧?
再然后就是一些语句换了位置..比如函数最后那句if语句之前是写在return前面的..
然后就是dfs和bfs各有各的用途吧..
然后bfs的方式我还没做..但也还是好想的..
还有就是标记过后要弄回来..
大概就这样.吧?