HDU  1312  Red and Black

           Red and Black

                            Time Limit:                          2000/1000 MS        

                     Memory Limit:                65536/32768 K (Java/Others)

 

Problem

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

本题据学长说是板子题(简单到不能再简单的那种),好吧。。。。我不会    )X--X(    ,,按着板子套了好久,总算弄懂了。。

翻译就不粘了,以后还是要看懂英文的,毕竟很多题都是英文的,,,,,所以就说一下大意吧!!!!

题的大意:@是你的起始位置, 点是你可以走的,#号是墙不能走,问你你能到走的位置的数目(其中包括你的起始位置@)

解题思路:DFS深搜

AC代码:

#include <stdio.h>
#include <algorithm>
#include <string.h>//memset对数组进行赋初值memset(数组名,所赋的值,数组的长度sizeof(数组名)) strcmp
#include <string>//string
#include <vector>//vec
#include <queue>//que
#include <stack>//sta
#include <set>//set<int > se;
#include <map>//map<string, int> mmp;
using namespace std;
typedef long long ll;

int w, h, dot;
char str[50][50];
int vis[50][50];

//定义一个下一次走向的方向数组
int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};

void DFS(int x, int y)
{
    vis[x][y] = 1;//标记已经走过的点
    dot++;
    for(int i=0; i<4; i++)
    {
        int dx = x + dir[i][0];
        int dy = y + dir[i][1];
        if(dx >= 0 && dy >= 0 && dx < h && dy < w && vis[dx][dy] != 1 && str[dx][dy] == '.')//满足递归的条件
        {
            DFS(dx, dy);//递归回溯
        }
    }
}
int main()
{
    int stax, stay;
    while(scanf("%d %d", &w, &h) != EOF && (w || h))
    {
        for(int i=0; i<h; i++)  scanf("%s", str[i]);//最好不要用 &str[i][j]来存储数据,
        for(int i=0; i<h; i++)                    //因为当数据量很大时,通常会超时,
            for(int j=0; j<w; j++)                //而且这样存储你还需要去除回车
                if(str[i][j] == '@')    stax = i, stay = j;
        memset(vis, 0, sizeof(vis));//对VIS数组赋初值
        dot = 0;
        DFS(stax, stay);
        printf("%d\n",dot);
    }
    return 0;
}

以下这个代码是用scanf("%c",&str[i][j]);来接收的:

int main()
{
    int stax, stay;
    while(scanf("%d %d", &w, &h) != EOF && (w || h))
    {
        getchar();//第一次吸收掉输入w,h之后的那个回车键;
//        for(int i=0; i<h; i++)  scanf("%s", str[i]);
        for(int i=0; i<h; i++)
        {
            for(int j=0; j<w; j++)
            {
                scanf("%c", &str[i][j]);
            }
            getchar();//第二次吸收掉每一行字符串后的换行;
        }
//        for(int i=0; i<h; i++)  puts(str[i]);
        for(int i=0; i<h; i++)
            for(int j=0; j<w; j++)
                if(str[i][j] == '@')    stax = i, stay = j;
        memset(vis, 0, sizeof(vis));
        dot = 0;
        DFS(stax, stay);
        printf("%d\n",dot);
    }
    return 0;
}

本题所学到的:

  1. 如果用%c来接收需要用getchar()来吸收换行符;
  2. memset(vis, a, sizeof(vis))  (其中vis表示数组名,a表示要赋的值,sizeof(vis)表示要赋值的长度)
  3. 告诉你个秘密这个memset的头文件是#include <string.h>  
  4. 它也可以给非int型的数组赋值

猜你喜欢

转载自blog.csdn.net/A_B_C_D_E______/article/details/81332907