hdu 1312(DFS)

Red and Black

Tme Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 18126    Accepted Submission(s): 11045

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

.#.........

.#.#######.

.#.#.....#.

.#.#.###.#.

.#.#[email protected]#.#.

.#.#####.#.

.#.......#.

.#########.

...........

11 6

..#..#..#..

..#..#..#..

..#..#..###

..#..#..#@.

..#..#..#..

..#..#..#..

7 7

..#.#..

..#.#..

###.###

[email protected]

###.###

..#.#..

..#.#..

0 0

Sample Output

45

59

6

13

题意就是  找从@点开始出发,问所能到达的‘.‘点的个数(最开始理解错了意思,以为是一条路径的最大值,后面

才发现是可以到达的‘.‘点 ),直接DFS,

#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<queue>
#include<cstring>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N = 1e6+10;
const ll mod = 1e9+7;
int t=0,flag=0;
char s[26][26];
int visited[26][26];
int m,n;
int _x[4]={0,1,-1,0};
int _y[4]={1,0,0,-1};
int ans;
void DFS(int x,int y){
    for(int t=0;t<4;t++){
        int i=x+_x[t];
        int j=y+_y[t];
        if(i>=0&&j>=0&&visited[i][j]==0&&s[i][j]==‘.‘&&i<=n-1&&j<=m-1)
        {
            ans++;
            visited[i][j]=1;
            DFS(i,j);
        }
    }
}
int main(){
    while(scanf("%d%d",&m,&n)!=EOF){
        if(m==0&&n==0)break;
        memset(visited,0,sizeof(visited));
        int ii,jj;
        int i,j;
        for(ii=0;ii<n;ii++){
            for(jj=0;jj<m;jj++){
                cin>>s[ii][jj];
                if(s[ii][jj]==‘@‘){i=ii;j=jj;}
            }
        }
        ans=1;
        visited[i][j]=1;
        DFS(i,j);
        cout<<ans<<endl;
    }
}
时间: 2024-10-12 19:03:05

hdu 1312(DFS)的相关文章

hdu 1312 DFS算法简单题

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 此题与油田那题很像是练习深搜的好题,题意是从"@"开始,遍历整个图,找到连接的 "."有多少个 但要考虑变化,简单处理下就行了,主要是斜角的不要了,而且判断结束方式也不一样 具体看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

HDU 1312 ----- Red and Black 入门搜索 DFS解法

HDU 1312 ----- Red and Black  入门搜索  http://acm.hdu.edu.cn/showproblem.php?pid=1312 /*HDU 1312 ----- Red and Black 入门搜索 */ #include <cstdio> int n, m; //n行m列 int cnt, startx, starty; char mapp[25][25]; /*找一个连通的区域里可以走的块*/ void dfs(int x, int y){ if (x

HDU 1312:Red and Black(DFS搜索)

HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u 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

HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20820    Accepted Submission(s): 12673 Problem Description There i

HDU 1312 Red and Black (搜索)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9795    Accepted Submission(s): 6103 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

hdu 4109 dfs+剪枝优化

求最久时间即在无环有向图里求最远路径 dfs+剪枝优化 从0节点(自己增加的)出发,0到1~n个节点之间的距离为1,mt[i]表示从0点到第i个节点目前所得的最长路径 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<vector> using namespace std; const

HDU 1015 dfs回溯

题目真长.....看了好长时间才看懂.. 就是给你一个32位数字和一个最多15个字符的字符串,从字符串中选出5个字符,若满足题中给的那个式子,输出字典序最大的那5个字符,若不满足,输出no solution. 为了解决字典序问题,在输入字符串后,把字符串按从大到小排一下序,搜索一下若满足条件输出即可. 贴代码. 1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

HDU 5143 DFS

分别给出1,2,3,4   a, b, c,d个 问能否组成数个长度不小于3的等差数列. 首先数量存在大于3的可以直接拿掉,那么可以先判是否都是0或大于3的 然后直接DFS就行了,但是还是要注意先判合法能否进入下层递归来减少内存消耗. /** @Date : 2017-09-27 15:08:23 * @FileName: HDU 5143 DFS.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link :