Hdu1312/poj1979/zoj2165 red and black (red and black) solution report

Source: Internet
Author: User

Link: hdu1312/poj1979/zoj2165 red and black (red and black)

Red and black Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 9902 accepted submission (s): 6158


Problem descriptionthere 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.
 
Inputthe 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 ctions, respectively. W and H are not more than 20.

There are h more lines in the data set, each of which between des 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)
 
Outputfor each data set, your program shocould 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
4559613
 
Sourceasia 2004, Ehime (Japan), Japan domestic

Question:

There is a rectangular room filled with square tiles. The tiles are either red or black. A man is standing on one of the black tiles and can move to the top, bottom, left, and right directions, but cannot move to the Red Tile and asks him about the number of black tiles he can reach.

Analysis:

DFS search.

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int dir[4][2] = {{-1, 0},{0, 1},{1, 0},{0, -1}};int cnt, W, H;char mp[21][21];bool vis[21][21];void dfs(int x, int y){    vis[x][y] = true;    for(int i = 0; i < 4; i++)    {        int tx = x + dir[i][0];        int ty = y + dir[i][1];        if(tx >= 1 && tx <= H && ty >= 1 && ty <= W && !vis[tx][ty] && mp[tx][ty] == '.')        {            cnt++;            dfs(tx, ty);        }    }}int main(){    char c;    int x, y;    while(scanf("%d%d", &W, &H), W, H)    {        scanf("%c", &c);        for(int i = 1; i <= H; i++)        {            for(int j = 1; j <= W; j++)            {                scanf("%c", &mp[i][j]);                if(mp[i][j] == '@')                {                    x = i;                    y = j;                }            }            scanf("%c", &c);        }        cnt = 1;        memset(vis, false, sizeof(vis));        dfs(x, y);        printf("%d\n", cnt);    }    return 0;}


Hdu1312/poj1979/zoj2165 red and black (red and black) solution report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.