菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
137
0

poj 1154

原创
05/13 14:22
阅读数 39520
 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 }

 

发表评论

0/200
137 点赞
0 评论
收藏