poj 1154
1 LETTERS 2 Time Limit: 1000MS Memory Limit: 10000K 3 Total Submissions: 9298 Accepted: 4174 4 Description 5 6 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. 7 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. 8 The goal of the game is to play as many moves as possible. 9 Write a program that will calculate the maximal number of positions in the board the figure can visit in a single game. 10 Input 11 12 The first line of the input contains two integers R and C, separated by a single blank character, 1 <= R, S <= 20. 13 The following R lines contain S characters each. Each line represents one row in the board. 14 Output 15 16 The first and only line of the output should contain the maximal number of position in the board the figure can visit. 17 Sample Input 18 19 3 6 20 HFDFFB 21 AJHGDH 22 DGAGEH 23 Sample Output 24 25 6
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <string> 5 #include <string> 6 #include <cstring> 7 #include <map> 8 #include <utility> 9 using namespace std; 10 const int N = 1e2+20 ; 11 int n,m; 12 int maxx,cnt; 13 bool vis[N]; 14 char s[N][N]; 15 int dir[4][2]={ 16 {0,1},{0,-1},{-1,0},{1,0} 17 }; 18 bool check(int r,int l){ 19 if(r>=0&&r<n&&l>=0&&l<m&&vis[s[r][l]-'A']==0){ 20 return true; 21 } 22 return false; 23 } 24 void dfs(int r,int l){ 25 26 27 maxx=max(maxx,cnt); 28 vis[s[r][l]-'A']=1; 29 for(int i=0;i<4;i++){ 30 int rr=r+dir[i][0]; 31 int ll=l+dir[i][1]; 32 if(check(rr,ll)){ 33 cnt++; 34 dfs(rr,ll); 35 vis[s[rr][ll]-'A']=0; 36 cnt--; 37 } 38 } 39 } 40 int main() 41 { 42 while (~scanf("%d%d",&n,&m)){ 43 memset(vis,0,sizeof(vis)); 44 for(int i=0;i<n;i++) scanf("%s",s[i]); 45 maxx=1;cnt=1; 46 dfs(0,0); 47 printf("%d\n",maxx); 48 } 49 return 0; 50 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现