zoukankan      html  css  js  c++  java
  • poj 1154 letters (dfs回溯)

    http://poj.org/problem?id=1154

    #include<iostream>
    using namespace std;
    int bb[26]={0},s,r,sum=1,s1=1;
    char aa[25][25];
    int dir[4][2]={-1,0,1,0,0,-1,0,1};
    void dfs(int a,int b)
    {
        int a1,b1;
    
        if(s1>sum)        
            sum=s1;           //更新最大数值
            for(int i=0;i<4;i++)
            { a1=a+dir[i][0];               //用bb数组记录访问过的字母
              b1=b+dir[i][1];
                if(a1>=0&&a1<s&&b1>=0&&b1<r&&!bb[aa[a1][b1]-'A'])
                    {
                        s1++;
                        bb[aa[a1][b1]-'A']=1;              //如果在这条单线上没有记录改字母被访问过,则总数++;
                        dfs(a1,b1);                       //第一个字母总要被访问,所以不用回溯;
    
                        bb[aa[a1][b1]-'A']=0;           //回溯反标记
                        s1--;                            //临时记录恢复
                    }
    
            }
    }
    int main()
    {
        cin>>s>>r;
        for(int i=0;i<s;i++)
            for(int j=0;j<r;j++)
            cin>>aa[i][j];
        bb[aa[0][0]-'A']=1;
        dfs(0,0);
    
        cout<<sum<<endl;
    
    
    
        return 0;
    }
  • 相关阅读:
    foreach和each
    one
    存储
    动态添加
    百度描点
    php环境配置
    图文并茂
    css实现鼠标移上去变大,旋转,转别人的额
    vagrant box打包前的准备
    VirtualBox压缩打包
  • 原文地址:https://www.cnblogs.com/jin-nuo/p/5514731.html
Copyright © 2011-2022 走看看