码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 搜索练习 Red and Black

时间:2016-04-17 20:33:16      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

Red and Black

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 23 Accepted Submission(s) : 13
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.<br><br>Write a program to count the number of black tiles which he can reach by repeating the moves described above. <br>
 

 

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.<br><br>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.<br><br>‘.‘ - a black tile <br>‘#‘ - a red tile <br>‘@‘ - a man on a black tile(appears exactly once in a data set) <br>
 

 

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). <br>
 

 

Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
 

 

Sample Output
45
59
6
13
 
简单题意:
  #为障碍,"."为陆地,@表示人,求这个人最大能走到少范围
 
思路分析:
  简单搜索问题,,,最值得注意的是,先输入的是列, 而不是行
# include <iostream>
# include <queue>
# include <cstring>
# include <fstream>
using namespace std; // 好坑呀, 先输入列 在输入行

struct Info
{
    int x;
    int y;
}start;
int n, m;
char map[101][101];
int is[101][101];
int dir[4][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int dfs();
bool border(Info & info)
{
    if(info.x >= 1 && info.x <= m && info.y >= 1 && info.y <= n)
    return true;
    return false;
}
int main()
{
    //fstream cin("aaa.txt");
    while(cin >> n >> m)
    {
        if(n == 0 && m == 0)
        break;
        memset(is, 0, sizeof(is));
        for(int i = 1 ; i <= m; i++)
            for(int j = 1; j <= n; j++)
            {
                cin >> map[i][j];
                if(map[i][j] == @)
                {
                    start.x = i;
                    start.y = j;

                }
            }
        is[start.x][start.y] = 1;
        map[start.x][start.y] = .;
        cout << dfs() << endl;
    }
    return 0;
}
int dfs()
{
    int jishu = 1;
    queue <Info> Q;
    Q.push(start);
    Info now, next;
    while(!Q.empty())
    {
        now = Q.front();
        Q.pop();

        for(int i = 0; i < 4; i++)
        {
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];
            if(!border(next))
                continue;
            if(map[next.x][next.y] == #)
            continue;
            if(is[next.x][next.y])
            continue;

            jishu++;
            is[next.x][next.y] = 1;
            Q.push(next);
        }
    }
    return jishu;
}

 

 
  

HDU 搜索练习 Red and Black

标签:

原文地址:http://www.cnblogs.com/lyf-acm/p/5401935.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!