Red and Black (BFS)poj 1979

                                  Red and Black

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 26181   Accepted: 14213

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

题意为:求@可以到达.(点)的最多个数, 别忘了@本身也算一个哦~!#代表不可以穿越!

大体就是这么个意思,不多说,直接上代码.......

#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#define N 30
using namespace std;
int ans = 0, m, n;//ans用于计数的!
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};//一点小技巧
int vis[N][N], map[N][N];//vis数组是用于标记是否访问过~
void bfs(int x, int y)
{
    int i,ty,tx,ttx,tty;
    queue<pair<int, int> > Q;//不会用queue的可以去百度看看  很好用滴
    Q.push(make_pair(x, y));
    vis[x][y] = 1;
    while(!Q.empty())
    {
        tx = Q.front().first, ty = Q.front().second;
        Q.pop();//别忘了一直要删除队头
        for(i = 0; i < 4; i++)
        {
            ttx = tx + dx[i], tty = ty + dy[i];
            if(ttx >= 0 && ttx < m && tty >= 0 && tty < n)
                if(!vis[ttx][tty] && !map[ttx][tty])
                {
                    vis[ttx][tty] = 1;
                    ans++;
                    Q.push(make_pair(ttx, tty));
                }
        }
    }
}
int main()
{
    int i, j, x, y;
    while(~scanf("%d%d", &n, &m))
    {
        ans = 1;
        memset(vis, 0, sizeof(vis));//比for循环清零好使
        getchar();//有些人输入老是出问题,看看是不是这的原因
        if(n == 0 && m == 0)
            break;
        char s[N];
        for(i = 0; i < m; i++)
        {
            gets(s);
            for(j = 0; j < n; j++)
            {
                if(s[j] == ‘.‘)
                    map[i][j] = 0;
                else if(s[j] == ‘#‘)
                    map[i][j] = 1;
                else
                    x = i, y = j;//@的坐标
            }
        }
        bfs(x, y);
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-07 07:02:40

Red and Black (BFS)poj 1979的相关文章

HDU 1312 Red and Black (BFS)

Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status 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.

HDU 1312 Red and Black(bfs)

Red and Black Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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, h

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

hdu 1312 Red and Black(BFS水题)

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

POJ 1562-Oil Deposits(BFS)

Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12621   Accepted: 6888 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular r

HDU 1240——Asteroids!(三维BFS)POJ 2225——Asteroids

普通的三维广搜,需要注意的是输入:列,行,层 #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #define M 11 using namespace std; int dir[6][3]={{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};//6个方向 int

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

HDU 5024 Wang Xifeng&#39;s Little Plot (bfs)

Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. This novel was created in Qing Dynasty, by Cao Xueqin.

poj2243&amp;&amp;hdu1372 Knight Moves(BFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http://poj.org/problem?id=2243 HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where y