zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    dom元素和方法总结
    jQuery插件开发
    单次遍历,带权随机选取问题
    转:面试中常见的一些算法问题
    树状数组资料
    逆序数的求法
    将n进制的数组压缩成字符串(0-9 a-z)同一时候解压
    [积累]C++复习 海大2014硕士生面试题微信系统总结
    记一个手游app数据文件的破解
    poj1189 简单dp
  • 原文地址:https://www.cnblogs.com/huashanqingzhu/p/10630870.html
Copyright © 2011-2022 走看看