zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    更新整理本人所属博客文章的示例代码和工具组件(Java 和 C++)
    【端午呈献】通用高性能 Windows Socket 组件 HPSocket v2.2.1 悄然发布
    REST手记(一):对URI隧道技术以及CRUD操作的理解
    Jquery+JSON消费REST WCF4.0 服务(带源码)
    对REST架构 风格下WCF的一点补充
    REST与SOA两种架构下WCF的异同比较(含源码)
    REST笔记(三):一种标准的超媒体格式:Atom
    REST WCF 使用Stream进行Server与Client交互
    REST笔记(六)通过缓存架构可伸缩性与容错性的Service
    深入理解WCF系统体系(之二:WCF客户端如何构建?(上))
  • 原文地址:https://www.cnblogs.com/nonames/p/11396324.html
Copyright © 2011-2022 走看看