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;
}
时间: 2024-11-10 08:21:07

poj 1154 letters (dfs回溯)的相关文章

poj 1154 LETTERS dfs入门题

//poj 1154 //sep9 #include <iostream> using namespace std; const int maxR=32; char a[maxR][maxR]; int r,s; int ans=1; int vis[200]; void dfs(int i,int j,int len) { ans=max(ans,len+1); if(i+1<r&&vis[a[i+1][j]]==0){ vis[a[i+1][j]]=1; dfs(i+

poj 2676Sudoku(DFS+回溯)

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15698   Accepted: 7678   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure.

POJ 2907 Collecting Beepers (DFS+回溯)

Description Karel is a robot who lives in a rectangular coordinate system where each place is designated by a set of integer coordinates (x and y). Your job is to design a program that will help Karel pick up a number of beepers that are placed in he

POJ2488-A Knight&#39;s Journey(DFS+回溯)

题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36695   Accepted: 12462 Description Background The knight is getting bored of seeing the same black and white squares again and again

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

poj1321——dfs回溯

POJ 1321  DFS回溯+递归枚举 棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24813   Accepted: 12261 Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. Input 输入含有多组测试数据. 每组数据的第一行

【DFS+回溯】A Knight&#39;s Journey

总时间限制: 1000ms 内存限制: 65536kB 描述 BackgroundThe knight is getting bored of seeing the same black and white squares again and again and has decided to make a journeyaround the world. Whenever a knight moves, it is two squares in one direction and one squ

poj1270Following Orders(拓扑排序+dfs回溯)

题目链接: 啊哈哈,点我点我 题意是: 第一列给出所有的字母数,第二列给出一些先后顺序.然后按字典序最小的方式输出所有的可能性... 思路: 总体来说是拓扑排序,但是又很多细节要考虑,首先要按字典序最小的方式输出,所以自然输入后要对这些字母进行排列,然后就是输入了,用scanf不能读空格,所以怎么建图呢??设置一个变量判断读入的先后顺序,那么建图完毕后,就拓扑排序了,那么多种方式自然就是dfs回溯了..那么这个问题就得到了解决.. 题目: Following Orders Time Limit:

poj1011 Sticks DFS+回溯

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=1011 Description George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but