poj1979 Red and Black【搜索】

点击打开题目

Red and Black

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 23194   Accepted: 12513

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

题目翻译:给出一个m*n的图,然后‘.’代表可以走的路线,‘@’代表起点,‘#’代表墙,不能走,

求:算上起点,一共有多少格子可以走?

解题思路:dfs搜索!

#include<cstdio>
int m,n,x1,y1,cot;
char G[22][22];
int mov[][2]={0, 1, 0, -1, -1, 0, 1, 0};
void dfs(int x,int y){
    G[x][y]='#';
    int i,X,Y;
    for(i=0;i<4;i++){
        X=x+mov[i][0];
        Y=y+mov[i][1];
        if(G[X][Y]!='#'&&X>=0&&X<n&&Y>=0&&Y<m){
            cot++;
            dfs(X,Y);
        }
    }
}
int main(){
    int i,j;
    while(scanf("%d%d",&m,&n),m|n){
        for(i=0;i<n;++i)
            scanf("%s",G[i]);
        for(i=0;i<n;++i)
            for(j=0;j<m;++j)
                if(G[i][j]=='@'){
                    x1=i;y1=j;
                }
        cot=1;
        dfs(x1,y1);
        printf("%d\n",cot);
    }
    return 0;
}
时间: 2024-08-03 07:16:53

poj1979 Red and Black【搜索】的相关文章

POJ1979 Red and Black

问题链接:POJ1979 Red and Black. 题意简述:输入正整数w和h,w为列数,h为行数.输入h×w矩阵 (1 <= h <= 20; 1 <= w <= w),其中'.'代表可到达,'#'代表不可到达,'@'代表开始点.问从'@'开始可以到达最多多少个点. 问题分析:本题可以使用深度优先搜索求解,用广度优先搜索也可以求解,差别不大.需要注意的是'@'也算一个可以到达的点. 程序说明如下: 1.方向数组 使用方向数组后,各个方向的试探的程序就会变得简洁了,用循环处理即

HDU 1312 Red and Black (搜索)

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

poj-1979 red and black(搜索)

Time limit1000 ms Memory limit30000 kB 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 r

POJ1979 Red and Black (简单DFS)

POJ1979 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 mo

poj1979 Red and Black(DFS)

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 25797   Accepted: 13967 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

[dfs] [FloodFill] POJ1979 Red And Black

分析题目: 用dfs实现FloodFill. @和.实际上都是表示该格可走,#则表示该格不能走,想到用 bool map[i][j] 存储是否可走. 用visit[i][j]表示是否已经访问过. 从一个黑格只能到达上,下,左,右四块相邻的格,该格应该未访问过且该格不能是红格. inmap(i, j)判断该格是否在map中. 解题代码: 1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 5 boo

11.17 dfs poj1979 Red and Black

---恢复内容开始--- https://www.cnblogs.com/OctoptusLian/p/7429645.html 解救小哈--dfs入门 //果然博客园质量高丫 #include<iostream>#include<cstdio>#include<queue>using namespace std;int dx[] = {0,-1,0,1};int dy[] = {-1,0,1,0};int ans,n,m;char a[25][25];void dfs

深度优先搜索DFS (poj2386,poj1979, poj3009,poj1321,aoj0033,aoj0118)

深度优先搜索(DFS) 往往利用递归函数实现(隐式地使用栈). 深度优先从最开始的状态出发,遍历所有可以到达的状态.由此可以对所有的状态进行操作,或列举出所有的状态. 1.poj2386 Lake Couting 题意:八连通被认为连接在一起,求总共有多少个水洼? Sample Input: 10 12 W........WW. .WWW.....WWW ....WW...WW. .........WW. .........W.. ..W......W.. .W.W.....WW. W.W.W.

图的遍历之深度优先搜索(DFS)

深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的路每一个拐点有一盏灯是亮着的,你的任务是将所有灯熄灭,按照DFS的做法如下: 1. 熄灭你当前所在的拐点的灯 2. 任选一条路向前(深处)走,每经过一个拐点将灯熄灭直到与之相邻的拐点的灯全部熄灭后,原路返回到某个拐点的相邻拐点灯是亮着的,走到灯亮的拐点,重复执行步骤1 3. 当所有灯熄灭时,结束 将