1154:LETTERS
生活随笔
收集整理的這篇文章主要介紹了
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
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 following R lines contain S characters each. Each line represents one row in the board.
算法:深搜
代碼一:
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?
轉載于:https://www.cnblogs.com/huashanqingzhu/p/10630870.html
總結
以上是生活随笔為你收集整理的1154:LETTERS的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信支付条码支付上线啦
- 下一篇: composer Failed to d