G - Red and Black

原创 2018年04月17日 21:26:37
G - Red and Black
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<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
int ne[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
char tu[25][25]={'0'};
int step,W,H;
int dfs(int x,int y)
{
    int tx,ty;
     step++;
     tu[x][y]='#';    //已经搜过,标记
    for(int i=0;i<4;i++)
    {
        tx=x+ne[i][0];
        ty=y+ne[i][1];
        if(tu[tx][ty]=='.'&&tx>=0&&ty>=0&&tx<H&&ty<W)
        {
             dfs(tx,ty);
        }
    }
    return step;
}
int main()
{
    while(~scanf("%d%d",&W,&H)&&(W||H))
    {
        getchar();
        int x,y;
        step=0;
        for(int i=0;i<H;i++)
        {
            for(int j=0;j<W;j++)
              {
                   scanf("%c",&tu[i][j]);
                   if(tu[i][j]=='@')
                   {
                       x=i;
                       y=j;
                   }
              }
              getchar();
        }
        cout<<dfs(x,y)<<endl;
    }
    return 0;
}

北大ACM poj1979 Red and Black

#includechar map[21][21]; int c; int n,m; void count(char map[21][21],int i,int j){ map[i][j]='#...
  • gubojun123
  • gubojun123
  • 2012-12-08 11:06:31
  • 1266

HDU 1312 Red and Black(经典搜索,DFS&BFS三种方式)

Red and Black Problem Description There is a rectangular room, covered with square tiles. Each...
  • hurmishine
  • hurmishine
  • 2016-03-18 23:19:49
  • 1596

北大ACM1979 - Red and Black (广度优先搜索)

1.1      算法分析 这是一道很简单的广度优先搜索问题,我们以原始位置开始向四个方向搜索,如果是黑色的地砖,则将地砖标记为已经搜索过,然后搜索该黑色砖的四个临近地砖;如果该地砖是红色的或者...
  • Gykimo
  • Gykimo
  • 2013-01-06 18:19:26
  • 1407

POJ 1979 Red and Black(红与黑)

原文DescriptionThere is a rectangular room, covered with square tiles. Each tile is colored either red...
  • NoMasp
  • NoMasp
  • 2015-12-09 20:13:23
  • 3279

杭电 HDU 1312 Red and Black(超级简单dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • lsgqjh
  • lsgqjh
  • 2015-07-31 10:22:43
  • 754

杭电ACM1312——Red and Black~~广搜

这一题,简单的广搜或者深搜都可以搞定,时间复杂度都差不多。 我用的是广搜。题目的意思是:@是一个人的起始位置,#不可以走,. 可以走,求出可以走的位置的个数。 一开始没有用结构体来存储坐标,直接用的是...
  • qq_25425023
  • qq_25425023
  • 2015-05-21 22:44:13
  • 406

红黑树—Red Black Tree

红黑树是具有以下五条性质的二叉查找树: 1、每个结点要么是红的要么是黑的。 2、根结点是黑的。 3、每个叶结点(叶结点即指树尾端NIL指针或NULL结点)都是黑的。 4、如果一个结点是红的...
  • yuyixinye
  • yuyixinye
  • 2015-03-28 21:47:10
  • 726

红黑树(RED-BLACK TREE)的插入和删除

红黑树的左旋和右旋
  • flyapy
  • flyapy
  • 2014-07-12 16:06:45
  • 389

疯狂Java学习笔记(41)----------Red-Black Trees(红黑树)

目录(?)[+] 一红黑树简介二树的旋转三红黑树的插入操作四红黑树的删除五补充 Java 实现的红黑树 图 6 Java 红黑树的示意 红黑树和平衡二叉树 一:红黑树简介 红黑树,一种二叉...
  • u011225629
  • u011225629
  • 2015-05-20 21:48:18
  • 1119

杭电(hdu)ACM 1312 Red and Black

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • IT142546355
  • IT142546355
  • 2015-08-13 08:31:17
  • 597
收藏助手
不良信息举报
您举报文章:G - Red and Black
举报原因:
原因补充:

(最多只允许输入30个字)