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): 25283    Accepted Submission(s): 15239


 

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.

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

 题意:给一张图(注意n和m位置反了)从@点出发,“.”可以走,“#”不能走,问最多可以走多少个点

广搜深搜都可以写,这道题才是入门题,毕竟搜索是要先遍历的,而又得题说是入门题但是直接就要求最长走多少什么的有点过分。让很多新手曲解搜索的意义

广搜代码:  

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int a,b;
char mapp[50][50];
int vis[50][50];
struct node
{
    int x,y;
};
node st,ed;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
int judge(int x,int y)
{
    if(x>=0&&x<a&&y>=0&&y<b&&mapp[x][y]=='.'&&vis[x][y]==0)
        return 1;
    return 0;
}
int ans;
int bfs()
{
    queue<node>q;
    while(!q.empty()) q.pop();
    memset(vis,0,sizeof(vis));
    vis[st.x][st.y]=1;
    q.push(st);
    while(!q.empty())
    {
//    printf("**\n");
        st=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            ed.x=st.x+dx[i];
            ed.y=st.y+dy[i];
            if(judge(ed.x,ed.y))
            {
                vis[ed.x][ed.y]=1;
                ans++;
                q.push(ed);
            }
        }
    }
    return ans;
}
int main()
{
    while(~scanf("%d%d",&b,&a))
    {
        if(a==0&&b==0)
            break;
        for(int i=0;i<a;i++)
            scanf("%s",mapp[i]);
        for(int i=0;i<a;i++)
            for(int j=0;j<b;j++)
            {
                if(mapp[i][j]=='@')
                {
                    st.x=i;
                    st.y=j;
                }
            }
        ans=0;
//        printf("%d %d\n",st.x,st.y);
        printf("%d\n",bfs()+1);
    }
}

深搜代码(改编自广搜,所以结构体其实不必要开)

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
int a,b;
char mapp[50][50];
int vis[50][50];
struct node
{
    int x,y;
};
node st,ed;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
int judge(int x,int y)
{
    if(x>=0&&x<a&&y>=0&&y<b&&mapp[x][y]=='.'&&vis[x][y]==0)
        return 1;
    return 0;
}
int ans;
void dfs(int x,int y)
{
    vis[x][y]=1;
    ans++;
    for(int i=0;i<4;i++)
    {
        int xx=x+dx[i];
        int yy=y+dy[i];
        if(judge(xx,yy))
        {
            dfs(xx,yy);
        }
    }
    return ;
}
int main()
{
    while(~scanf("%d%d",&b,&a))
    {
        if(a==0&&b==0)
            break;
        for(int i=0;i<a;i++)
            scanf("%s",mapp[i]);
        for(int i=0;i<a;i++)
            for(int j=0;j<b;j++)
            {
                if(mapp[i][j]=='@')
                {
                    st.x=i;
                    st.y=j;
                }
            }
        ans=0;
        memset(vis,0,sizeof(vis));
        dfs(st.x,st.y);
        printf("%d\n",ans);
    }
}

猜你喜欢

转载自blog.csdn.net/zezzezzez/article/details/81112549