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
复制代码

 

posted on   华山青竹  阅读(214)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
历史上的今天:
2018-03-31 最大子矩阵和

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示