• dfs(最长路径)


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

    LETTERS
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 9519   Accepted: 4252

    Description

    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.

    Input

    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.

    Output

    The first and only line of the output should contain the maximal number of position in the board the figure can visit.

    Sample Input

    3 6
    HFDFFB
    AJHGDH
    DGAGEH

    Sample Output

    6

    Source

    题意:从左上角出发,每个字母不走两次的条件下,求走最多次数。
    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdio.h>
    #include <queue>
    #include <stack>;
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 1000000007
    using namespace std;
    typedef long long ll ;
    char a[29][29];
    int n , m ;
    int bx , by ;
    int ans ;
    int vis[29];
    int dir[4][2] = {{1 ,0 } , {-1 , 0} , {0 , 1} , {0 , -1}};
    
    void dfs(int x , int y , int cnt)
    {
        for(int i = 0 ; i < 4 ; i++)
        {
    
            int xx = x + dir[i][0];
            int yy = y + dir[i][1];
            if(vis[a[xx][yy] - 'A'] || xx <= 0 || xx > n || yy <= 0 || yy > m)
            {
                continue;
            }
            vis[a[xx][yy] - 'A'] = 1 ;
            dfs(xx , yy , cnt+1);
    
        }
        ans = max(ans , cnt);//for循环结束代表一条路径的结束。
        vis[a[x][y] - 'A'] = 0 ;//不同路径互不干扰
    }
    
    
    
    int main()
    {
        while(~scanf("%d%d" , &n , &m))
        {
            for(int i = 1 ; i <= n ; i++)
            {
                for(int j = 1 ; j <= m ; j++)
                {
                    cin >> a[i][j];
                }
            }
            memset(vis , 0 , sizeof(vis));
            vis[a[1][1] - 'A'] = 1 ;//将原地标记并加一
            ans = 0 ;
            dfs(1 , 1 , 1);
            cout << ans << endl ;
        }
    
        return 0;
    }

    下面这个是参考学长的码。

    //#include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <stdio.h>
    #include <queue>
    #include <stack>;
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    #define ME(x , y) memset(x , y , sizeof(x))
    #define SF(n) scanf("%d" , &n)
    #define rep(i , n) for(int i = 0 ; i < n ; i ++)
    #define INF  0x3f3f3f3f
    #define mod 1000000007
    using namespace std;
    typedef long long ll ;
    char a[29][29];
    int n , m ;
    int ans ;
    int vis[29];
    int dir[4][2] = {{1 ,0 } , {-1 , 0} , {0 , 1} , {0 , -1}};
    
    
    void dfs(int x , int y , int cnt)
    {
        if(vis[a[x][y] - 'A'] || x <= 0 || y <= 0 || x > n || y > m)
        {
            cnt--;
            return ;
        }
        else vis[a[x][y] - 'A'] = 1 ;
        dfs(x+1 , y , cnt+1);
        dfs(x-1 , y , cnt+1);
        dfs(x , y+1 , cnt+1);
        dfs(x , y-1 , cnt+1);
        ans = max(cnt , ans);
        vis[a[x][y] - 'A'] = 0 ;//每一条走到最后的路径都互不影响,所以要取消标记。
    }
    
    
    
    int main()
    {
        while(~scanf("%d%d" , &n , &m))
        {
            for(int i = 1 ; i <= n ; i++)
            {
                for(int j = 1 ; j <= m ; j++)
                {
                    cin >> a[i][j];
                }
            }
            ans = -INF ;
            memset(vis , 0 , sizeof(vis));
            dfs(1 , 1 , 1);
            cout << ans << endl ;
        }
    
        return 0;
    }
  • 相关阅读:
    PwnAuth——一个可以揭露OAuth滥用的利器
    Facebook 被指收集用户数据:通过照片和文本
    avast:中兴手机预装恶意软件 嵌入固件底层
    数字货币钱包安全白皮书
    CVE-2017-8464远程命令执行漏洞(震网漏洞)复现
    南非再曝数据库泄露事件:致百万人信息大白
    华盛顿邮报:FBI 屡次夸大了“手机加密威胁”的数字
    工业互联网联盟发布新物联网安全成熟度模型
    深入剖析最新IE0day漏洞
    联储证券被曝存在多项信息安全漏洞
  • 原文地址:https://www.cnblogs.com/nonames/p/11396324.html
Copyright © 2020-2023  润新知