zoukankan      html  css  js  c++  java
  • poj1154 【DFS】

    LETTERS
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 8976   Accepted: 4017

    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
    思路:
    基础dfs,
    实现代码:
    //#include<bits/stdc++.h>
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<stack>
    #include<set>
    #include<list>
    using namespace std;
    #define ll long long
    const int Mod = 1e9+7;
    const int inf = 1e9;
    const int Max = 1e5+10;
    vector<int>vt[Max];
    int dx[] = {-1, 1,  0, 0};
    int dy[] = { 0, 0, -1, 1};
    //void exgcd(ll a,ll b,ll& d,ll& x,ll& y){if(!b){d=a;x=1;y=0;}else{exgcd(b,a%b,d,y,x);y-=x*(a/b);}}
    //ll inv(ll a,ll n){ll d, x, y;exgcd(a,n,d,x,y);return (x+n)%n;}  ��Ԫ
    //int gcd(int a,int b)  {  return (b>0)?gcd(b,a%b):a;  }  ��С��Լ
    //int lcm(int a, int b)  {  return a*b/gcd(a, b);   }    ������
    int ans = 1,n,m,vis[25];
    char mp[25][25];
    
    void dfs(int x,int y,int cnt){
        for(int i=0;i<4;i++){
            int nx = dx[i] + x;
            int ny = dy[i] + y;
            if(nx>=0&&nx<n&&ny>=0&&ny<m&&vis[mp[nx][ny]-'A']==0){
                vis[mp[nx][ny]-'A'] = 1;
                ans = max(ans,cnt+1);
                dfs(nx,ny,cnt+1);
                vis[mp[nx][ny]-'A'] = 0;
            }
        }
    }
    
    int main()
    {
        cin>>n>>m;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                cin>>mp[i][j];
            }
        }
        vis[mp[0][0]-'A'] = 1;
        dfs(0,0,1);
        cout<<ans<<endl;
    }
  • 相关阅读:
    c#mysql批量更新的两种方法
    批处理上传错误
    c#关于Mysql MySqlBulkLoader 批量上传
    findViewById(R.id.btn_first) 给写成 R.layout.
    linearlayout 中ImageView 居中等问题
    c#npoi 报错Cannot get a numeric value from a text cell 的解决
    c#Dapper 批量插入Mysql
    c#NPOI读取excel 比interop和Microsoft.Jet.OLEDB.4.0 之类 的好的多
    Android程序入口以及项目文件夹的含义和使用总结—入门
    百度地图sdk sha1秘钥获取有种想吐的赶脚
  • 原文地址:https://www.cnblogs.com/kls123/p/7403775.html
Copyright © 2011-2022 走看看