LETTERS
2013-09-26 11:19
302 查看
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
Sample Output
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 题意:给定一个二维矩阵,从最左最上开始搜索,重复的字母不能再搜索,求最大的序列长度 题解:深度优先搜索 #include<stdio.h> #include<string.h> int n,m; int dir[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; int cnt,maxi; int visit[30]; int flag[25][25]; char mm[25][25]; void dfs(int x,int y) { int i; for(i=0;i<4;i++) { int xx=x+dir[i][0]; int yy=y+dir[i][1]; if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!visit[flag[xx][yy]]) { cnt++; visit[flag[xx][yy]]=1; dfs(xx,yy); visit[flag[xx][yy]]=0; if(cnt>maxi) maxi=cnt; cnt--; } } } int main() { int i,j; while(scanf("%d %d",&n,&m)!=EOF) { getchar(); for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { scanf("%c",&mm[i][j]); flag[i][j]=mm[i][j]-'A'+1; } getchar(); } memset(visit,0,sizeof(visit)); visit[flag[1][1]]=1; cnt=1; maxi=1; dfs(1,1); visit[flag[1][1]]=0; printf("%d\n",maxi); } return 0; }
相关文章推荐
- linux 用户(User)和用户组(Group)配置文件详解
- STM32双机SPI中断通信机制
- HTML5教程之音频Audio标签应用
- cloudfoundry资料
- eclipse安装其他颜色主题包
- .net调用存储过程详解
- jQuery Select操作大集合
- awk
- android修改开机自动默认设置
- C++静态成员/函数小结
- 信息检索-拼写检查器
- cocos2d-x解决中文乱码问题的几种办法
- Linux获取线程tid线程名
- 9月第三周国内域名主机网站Top15
- 尚学堂马士兵Oracle学习笔记之二:select子句和常用SQL函数
- 长沙网络赛G,H,J
- Linux下集群技术应用概述
- 【程序练习】——括号匹配
- PowerDesigner 学习及使用
- 物联网技术架构