您的位置:首页 > 其它

HDU - 1312 - Red and Black

2015-07-22 12:47 483 查看
题意:给你一个二维数组,@是人的起始位置,‘#’代表红色,不能走,’.’代表黑色可以走,输出能走多的方格的个数,@起始位置也算一个。题意说明只有上下左右四个方向,就深搜这四个方向。

[code]#include<stdio.h>
#include<string.h>
#define MAXN 22

char s[MAXN][MAXN];
int vis[MAXN][MAXN]; 
int w, h, ans;
int move[4][2]={{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

void DFS(int x, int y){
    for(int i = 0; i < 4; i++){
        int tx = x + move[i][0];
        int ty = y + move[i][1];
        if(tx >= 0 && tx < h && ty >= 0 && ty < w && s[tx][ty]=='.' && !vis[tx][ty]){
            vis[tx][ty] = 1;
            ans++; 
            DFS(tx, ty);
        }
    } 
} 

int main()
{
    while(scanf("%d%d", &w, &h) && w+h){
        ans = 0;
        for(int i = 0; i < h; i++)
            scanf("%s", s[i]);
        memset(vis, 0, sizeof(vis));

        for(int i = 0; i < h; i++)
        {
            for(int j = 0; j < w; j++){
                if(s[i][j]=='@'){
                    vis[i][j] = 1;
                    ans++;
                    DFS(i, j);
                }
            } 
        }
        printf("%d\n", ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: