hdu dfs水题

hdu 1181 、 1241 、 1312 、 4707

很久没有刷题了,寒假在家没网络,真是不爽啊。这两天决定做一做DFS的专题,所以上网搜了几道水题(大神勿喷,本人弱菜一只)。


1181:

变形课

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 11647    Accepted Submission(s): 4322


Problem Description
呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体. 
Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听一大堆好好学习的道理.
 

Input
测试数据有多组。每组有多行,每行一个单词,仅包括小写字母,是Harry所会的所有咒语.数字0表示一组输入结束.
 

Output
如果Harry可以完成他的作业,就输出"Yes.",否则就输出"No."(不要忽略了句号)
 

Sample Input
   
   
   
   
so soon river goes them got moon begin big 0
 

Sample Output
   
   
   
   
Yes.
Hint
Hint
Harry 可以念这个咒语:"big-got-them".
 

#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<string> in;
string s;
int vis[100000];
int flag,cnt;
char begain,_end;
void dfs(int cur)
{
    if(flag||cur>=cnt)
        return ;
    for(int i=0;i<cnt;i++)
    {
        if(in[i][0]==begain&&vis[i]==0)
        {
            char temp=begain;
            begain=in[i][in[i].length()-1];
            vis[i]=1;
            dfs(cur+1);
            begain=temp;
            vis[i]=0;
        }
        if(in[i][0]==begain&&in[i][in[i].length()-1]==_end&&vis[i]==0)
        {
            flag=1;
            return ;
        }
    }
}
int main()
{
    while(cin>>s)//             我之前是这样写的,但是会超时,不知道为什么......while(1)
    {                           //                                              {  
    begain='b',_end='m';        //                                                  cin>>s;
    flag=0,cnt=0;               //                                                    ...
    in.clear();                 //                                                    ...
    while(s!="0")               //                                                    ...
    {                           //
        in.push_back(s);        //
        vis[cnt++]=0;          //
        cin>>s;               //
    }                         //
    dfs(0);                      //
    cout<<(flag?"Yes.\n":"No.\n"); //
                                  //
    }                             //                                             }
    return 0;
}


1241:

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9570    Accepted Submission(s): 5612


Problem Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid. 
 

Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
 

Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
 

Sample Input
   
   
   
   
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
 

Sample Output
   
   
   
   
0 1 2 2
 

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
char a[110][110];
int vis[110][110];
int m,n;
void dfs(int i,int j)
{
    if(i>=m||i<0||j>=n||j<0||a[i][j]=='*'||vis[i][j])
        return ;
    vis[i][j]=1;
    dfs(i-1,j-1);dfs(i-1,j);dfs(i-1,j+1);
    dfs(i,j-1);             dfs(i,j+1);
    dfs(i+1,j-1);dfs(i+1,j);dfs(i+1,j+1);
}
int main()
{
    while(scanf("%d%d",&m,&n)&&m)
    {
        for(int i=0;i<m;i++)
        {
            scanf("%s",a[i]);
        }
        int cnt=0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(a[i][j]=='@'&&!vis[i][j])
                {
                    cnt++;
                    dfs(i,j);
                }
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

1312:

Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7822    Accepted Submission(s): 4908


Problem Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above. 
 

Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

'.' - a black tile 
'#' - a red tile 
'@' - a man on a black tile(appears exactly once in a data set) 
 

Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself). 
 

Sample Input
   
   
   
   
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
 

Sample Output
   
   
   
   
45 59 6 13
 
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
int w,h;
char a[25][25];
int cnt;
int vis[25][25];
void dfs(int i,int j)
{
    if(i<0||i>=w||j<0||j>=h||a[i][j]=='#'||vis[i][j])
        return ;
    cnt++;
    vis[i][j]=1;
                dfs(i-1,j);
    dfs(i,j-1);            dfs(i,j+1);
                dfs(i+1,j);
}
int main()
{
    while(scanf("%d%d",&h,&w)&&(w||h))
    {
        memset(vis,0,sizeof(vis));
        for(int i=0;i<w;i++)
            scanf("%s",a[i]);
        cnt=0;
        for(int i=0;i<w;i++)
            for(int j=0;j<h;j++)
                if(a[i][j]=='@')
                    dfs(i,j);
        printf("%d\n",cnt);
    }
    return 0;
}

4707:

Pet

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 920    Accepted Submission(s): 462


Problem Description
One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searched in the room but didn’t find the hamster. He tried to use some cheese to trap the hamster. He put the cheese trap in his room and waited for three days. Nothing but cockroaches was caught. He got the map of the school and foundthat there is no cyclic path and every location in the school can be reached from his room. The trap’s manual mention that the pet will always come back if it still in somewhere nearer than distance D. Your task is to help Lin Ji to find out how many possible locations the hamster may found given the map of the school. Assume that the hamster is still hiding in somewhere in the school and distance between each adjacent locations is always one distance unit.
 

Input
The input contains multiple test cases. Thefirst line is a positive integer T (0<T<=10), the number of test cases. For each test cases, the first line has two positive integer N (0<N<=100000) and D(0<D<N), separated by a single space. N is the number of locations in the school and D is the affective distance of the trap. The following N-1lines descripts the map, each has two integer x and y(0<=x,y<N), separated by a single space, meaning that x and y is adjacent in the map. Lin Ji’s room is always at location 0.
 

Output
For each test case, outputin a single line the number of possible locations in the school the hamster may be found.
 

Sample Input
    
    
    
    
1 10 2 0 1 0 2 0 3 1 4 1 5 2 6 3 7 4 8 6 9
 

Sample Output
    
    
    
    
2
 

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 100000+10
int n,d;
vector<int>m[maxn];
int vis[maxn];
int cnt;
void dfs(int num,int e)
{
    if(e>d)return ;
    vis[num]=1;
    cnt++;
    int len=m[num].size();
    for(int i=0;i<len;i++)
    {
        if(!vis[m[num][i]])
            dfs(m[num][i],e+1);
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&d);
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
            m[i].clear();
        for(int i=0;i<n-1;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            m[a].push_back(b);
            m[b].push_back(a);
        }
        cnt=0;
        dfs(0,0);
        printf("%d\n",n-cnt);
    }
    return 0;
}




你可能感兴趣的:(hdu dfs水题)