Red and Black(dfs水)

题目链接: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): 15186    Accepted Submission(s): 9401

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

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<string>
 5 using namespace std;
 6 #define N 25
 7 char mp[N][N];
 8 int go[4][2] = {{1,0},{-1,0},{0,-1},{0,1}};
 9
10 int n,m;
11 bool ck(int x, int y){
12     if(x<n&&x>=0&&y<m&&y>=0) return true;
13     else return false;
14 }
15 bool vis[N][N];
16 void dfs(int x, int y, int &sum)
17 {
18     vis[x][y] = 1;
19     bool fl = 0;
20     for(int i = 0; i < 4; i++){
21         int xx = x + go[i][0];
22         int yy = y + go[i][1];
23         if(ck(xx,yy)&&!vis[xx][yy]&&mp[xx][yy]==‘.‘){
24             fl = 1;
25             sum = sum+1;
26             dfs(xx,yy,sum);
27         }
28     }
29     if(fl==0) return;
30 }
31 int main()
32 {
33     while(~scanf("%d%d",&m,&n))
34     {
35         int x, y;
36         if(n==0&&m==0) break;
37         getchar();
38         for(int i = 0; i < n; i++){
39             for(int j = 0; j < m; j++){
40                 scanf("%c",&mp[i][j]);
41                 if(mp[i][j]==‘@‘){ x = i; y = j; }
42             }
43             getchar();
44         }
45         memset(vis,0,sizeof(vis));
46         int sum = 0;
47         dfs(x,y,sum);
48         printf("%d\n",sum+1);
49     }
50     return 0;
51 }
时间: 2024-11-06 18:37:32

Red and Black(dfs水)的相关文章

poj 1979 Red and Black(dfs水题)

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

POJ 2248 Addition Chains dfs(水)

题意:给出n 构成出满足下列条件 长度最小的数列a[0]=1,a[m]=n, a[0]<a[1]<..<a[m]每个下标k都存在(i,j<k) 满足:a[k]=a[i]+a[j] n<=100 枚举长度 dfs爆搜+剪枝 水过, #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N=2e5+20; int n,a[N

HDU1312:Red and Black(DFS)

题目描述 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 blac

Red and Black(水)

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

POJ 1979 Red and Black(DFS 连通块中元素数量)

题意  求矩阵中包含'@'的'.'连通块中元素数量  '@'也看做'.' 最基础的dfs了 #include<cstdio> #include<cstring> using namespace std; const int N = 30; char mat[N][N]; int dx[4] = {0, 0, -1, 1}, dy[4] = { -1, 1, 0, 0}; int ans; void dfs(int r, int c) { if(mat[r][c] != '.') r

POJ 题目2245 Lotto(DFS水)

Lotto Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 4153 Description In the German Lotto you have to select 6 numbers from the set {1,2,...,49}. A popular strategy to play Lotto - although it doesn't increase your chan

poj 1979 Red and Black(dfs)

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23191   Accepted: 12510 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

poj 1979 dfs水题

// 练练水题,夯实基础吧 #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib

HDOJ 题目1312 Red and Black(DFS)

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