《挑战》2.1 POJ POJ 1979 Red and Black (简单的DFS)

B - Red and Black

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

Submit Status Practice POJ 1979

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)         The end of the input is indicated by a line consisting of two zeros.

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

题目大意:

  就是说,对于一个h*w的网格,每次从‘@’开始,求最多能走多少个‘.’,每次只能够向四个方向进行扩展。

解题思路:

  直接dfs,然后用book数组随时记录目前这个点有没有走过,如果这个点是‘.‘并且没有走过的话,那么我们就res++,由于是多组数据,每次输出结束后,记着memset,

复杂度:

  O(4*w*h)

代码:

 1 # include<cstdio>
 2 # include<iostream>
 3 # include<cstring>
 4
 5 using namespace std;
 6
 7 # define MAX 23
 8
 9 char grid[MAX][MAX];
10 int book[MAX][MAX];
11 int nxt[4][2] = {{1,0},{0,-1},{-1,0},{0,1} };
12 int res;
13 int w,h;
14
15 int can_move ( int x,int y )
16 {
17     if ( x>=0&&x<h&&y>=0&&y<w )
18     {
19         if ( grid[x][y]==‘.‘&&book[x][y]==0 )
20             return 1;
21     }
22     return 0;
23 }
24
25 void dfs ( int x,int y )
26 {
27     book[x][y] = 1;
28     for ( int i = 0;i < 4;i++ )
29     {
30         int n_x = x+nxt[i][0], n_y = y+nxt[i][1];
31         if ( can_move ( n_x,n_y ) )
32         {
33             res++;
34             book[n_x][n_y] = 1;
35             dfs(n_x,n_y);
36         }
37     }
38 }
39
40 int main(void)
41 {
42     while ( scanf("%d%d",&w,&h)!=EOF )
43     {
44         res = 1;
45         if ( w==0&&h==0 )
46             break;
47         for ( int i = 0;i < h;i++ )
48             scanf("%s",grid[i]);
49         for ( int i = 0;i < h;i++ )
50         {
51             for ( int j = 0;j < w;j++ )
52             {
53                 if ( grid[i][j]==‘@‘ )
54                 {
55                     book[i][j] = 1;
56                     dfs(i,j);
57                 }
58             }
59         }
60
61         printf("%d\n",res);
62         memset(book,0,sizeof(book));
63     }
64
65
66
67     return 0;
68 }
时间: 2024-10-14 16:44:20

《挑战》2.1 POJ POJ 1979 Red and Black (简单的DFS)的相关文章

POJ 1979 Red and Black (简单dfs)

题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define INF 2147483647 int w,h; char a[22][22]; int dir[4][2] = {-1,0,1,0,0,-1,0,1}; int ans = 0; void dfs(int x,int y){ if(x < 0 || x >= h || y < 0 || y &g

POJ 1979 Red and Black (红与黑)

POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K 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

POJ 1979 Red and Black 深度优先搜索上手题

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21738   Accepted: 11656 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 Red and Black【深度优先搜索】

题目链接:http://poj.org/problem?id=1979 题目大意:一个矩形的房间地板被分为w*h个小块,每一个小块不是红的就是黑的,你首先站在一个黑色小块上,你只能朝你的四个方向(上下左右)移动,且不能到达红色的小块上,问你最多能到达多少个小块. 很简单的dfs深度优先搜索 没搜索过一个格子,将该格子设置为红色,之后的搜索就不会再搜索到该格子,就不会造成重复,因为该题有很多数据,记得每次处理数据是初始化各数组及其他数据. 代码如下: #include <iostream> #i

poj 2492 a bug&#39;s life 简单种类并查集

题意大致为找同性恋的虫子.... 这个比食物链要简单些.思路完全一致,利用取余操作实现关系之间的递推. 个人感觉利用向量,模和投影可能可以实现具有更加复杂关系的并查集. 1 #include<cstdio> 2 using namespace std; 3 const int MAXN=50010; 4 int fa[MAXN]; 5 int rel[MAXN]; // 0代表同类,1代表吃fa[i],2代表被吃 6 void _set(int n) 7 { 8 for(int i=1;i&l

poj 3112 Digital Biochemist Circuit(简单题)

题目链接:http://poj.org/problem?id=3112 Digital Biochemist Circuit Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 876   Accepted: 375 Description A digital biochemist circuit (DBC) is a device composed of a set of processing nodes. Each pro

POJ Frogs&#39; Neighborhood havel-hakimi定理 (简单题)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 8263   Accepted: 3504   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..

POJ 3282 Ferry Loading IV(简单模拟)

Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry cross

POJ 2909 Goldbach&#39;s Conjecture(简单题)

[题意简述]:输入一个数,输出有几对素数对可以使他们的和正好等于这个数 [分析]:暴力打表,再暴力循环求解 //268K 125Ms #include<iostream> using namespace std; #define N 35000 // 2^15 bool isprime[N]; int prime[N],nprime;//prime[N]用来存储素数,nprime是此时一共有多少素数 void doprime(int n) { int i,j; nprime = 1; mems

POJ POJ 2778 DNA Sequence AC自动机 + 矩阵快速幂

首先建立Trie和失败指针,然后你会发现对于每个节点 i 匹配AGCT时只有以下几种情况: i 节点有关于当前字符的儿子节点 j 且安全,则i 到 j找到一条长度为 1的路. i 节点有关于当前字符的儿子节点 j 且 不安全,则i 到 j没有路. i 节点没有关于当前字符的儿子节点 但是能通过失败指针找到一个安全的节点j,那么 i 到 j 找到一条长度为1的路. 关于节点安全的定义: 当前节点不是末节点且当前节点由失败指针指回跟节点的路径上不存在不安全节点,那么这个节点就是安全节点. 然后问题就