1154:LETTERS

Posted huashanqingzhu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1154:LETTERS相关的知识,希望对你有一定的参考价值。

题目链接http://bailian.openjudge.cn/practice/1154/
总时间限制: 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

算法:深搜

代码一:

技术图片
 1 #include<iostream>
 2 using namespace std;
 3 int bb[26]={0},s,r,sum=1,s1=1;
 4 char aa[25][25];
 5 int dir[4][2]={-1,0,1,0,0,-1,0,1};
 6 void dfs(int a,int b)
 7 {
 8     int a1,b1;
 9     if(s1>sum) sum=s1;           //更新最大数值
10     for(int i=0;i<4;i++)
11     {
12         a1=a+dir[i][0];             //用bb数组记录访问过的字母
13         b1=b+dir[i][1];
14         if(a1>=0&&a1<s&&b1>=0&&b1<r&&!bb[aa[a1][b1]-A])
15         {
16             s1++;
17             bb[aa[a1][b1]-A]=1;   //如果在这条单线上没有记录改字母被访问过,则总数++;
18             dfs(a1,b1);             //第一个字母总要被访问,所以不用回溯;
19             bb[aa[a1][b1]-A]=0;   //回溯反标记
20             s1--;                   //临时记录恢复
21         }
22     }
23 }
24 int main()
25 {
26     cin>>s>>r;
27     for(int i=0;i<s;i++)
28         for(int j=0;j<r;j++)
29         cin>>aa[i][j];
30     bb[aa[0][0]-A]=1;
31     dfs(0,0);
32     cout<<sum<<endl;
33     return 0;
34 }
View Code

代码二:

技术图片
 1 #include <stdio.h>
 2 #include<iostream>
 3 using namespace std;
 4 int qq[25][25];
 5 int fx[4]={1,0,-1,0},fy[4]={0,-1,0,1},pd[30],sum,ans;//右下左上 
 6 void fun(int x,int y)
 7 {    
 8     if(ans<sum)ans=sum;
 9     if(qq[x][y]==0) return;
10     for(int i=0;i<4;i++)
11     {
12         if(qq[x+fx[i]][y+fy[i]]!=0&&pd[qq[x+fx[i]][y+fy[i]]]==0)
13         {
14             sum++;
15             pd[qq[x+fx[i]][y+fy[i]]]=1;
16             fun(x+fx[i],y+fy[i]);
17             pd[qq[x+fx[i]][y+fy[i]]]=0;
18             sum--;
19         }
20     }
21 }
22 int main(int argc, char *argv[])
23 {
24     int r,s;
25     scanf("%d%d",&r,&s);
26     for(int i=1;i<=r;i++)
27         for(int j=1;j<=s;j++)
28            {
29             char t;
30             cin>>t; 
31             qq[i][j]=t-A+1;
32         }
33     pd[qq[1][1]]=1;
34     sum=ans=1;
35     fun(1,1);
36     printf("%d",ans);
37     return 0;
38 }
View Code

 

以上是关于1154:LETTERS的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器