poj1154 连通分量 简单深搜
2016-04-14 16:06
225 查看
LETTERS
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
题意很简单:就是在一个矩形的字符阵中寻找一个没有重复的的字符的字符序列,方向只有四个方向,求这个序列字符个数
解题思路:从每一个位置开始搜索一下,在每一个位置递归进去,运用回溯就可以满足了
然后实现代码,其实很简单,可以和我的另外一篇blog对比一下http://blog.csdn.net/summer__show_/article/details/51146603
都是连通分量求最大值
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8031 | Accepted: 3625 |
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
题意很简单:就是在一个矩形的字符阵中寻找一个没有重复的的字符的字符序列,方向只有四个方向,求这个序列字符个数
解题思路:从每一个位置开始搜索一下,在每一个位置递归进去,运用回溯就可以满足了
然后实现代码,其实很简单,可以和我的另外一篇blog对比一下http://blog.csdn.net/summer__show_/article/details/51146603
都是连通分量求最大值
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int R,C; char visit[25][25]; int temp[100]; int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; int ans,coun; int dfs(int x,int y) { for(int i=0;i<4;i++){ int tempx=x+dx[i]; int tempy=y+dy[i]; if(tempx>=0&&tempy>=0&&tempx<R&&tempy<C&&temp[visit[tempx][tempy]]==0){ temp[visit[tempx][tempy]]=1; coun++; dfs(tempx,tempy); ans=max(ans,coun); temp[visit[tempx][tempy]]=0;//回溯 coun--; } } } int main() { while(scanf("%d%d",&R,&C)!=EOF) { getchar(); for(int i=0;i<R;i++) scanf("%s",visit[i]); memset(temp,0,sizeof(temp)); ans=coun=1; temp[visit[0][0]]=1; dfs(0,0); printf("%d\n",ans); } return 0; }
相关文章推荐
- 秒杀多线程第七篇 经典线程同步 互斥量Mutex
- LeetCode -- Word Break
- 数据库创建用户SQL
- lvs的工作模型及调度算法
- 异步与同步&&阻塞与非阻塞
- jQuery学习进阶篇
- 利用Filter解决web应用乱码
- Java设置Swing窗体居中显示
- 关于CSS定位的理解
- 小故障
- ReentrantLock
- 【Android UI】自定义圆形SeekBar和自定义Dialog的结合使用
- centos 搭建git服务器和客户端
- clone
- clone
- 编译pycaffe问题
- 序列化.to_sym
- FusionCompute-VRM安装
- TextView的setCompoundDrawables不显示问题解决
- Mac下搭建Android开发环境