码迷,mamicode.com
首页 > 其他好文 > 详细

111

时间:2019-04-06 12:36:37      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:ios   can   dir   ring   air   swa   ==   char   out   

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAX = 100;
int N, M, cnt;
char maze[MAX][MAX];
bool vis[MAX][MAX];
int sx, sy;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int bfs () {
    //cout<<"1"<<endl;
    queue<pair<int, int> > q;
    int cnt = 0;
    q.push(make_pair(sx, sy));
    vis[sx][sy] = true;
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop() ;
        for (int i = 0; i < 4; i++) {
            int nx = x + dir[i][0];
            int ny = y + dir[i][1];
            if(nx >= 0 && nx < N && ny >= 0 && ny < M && maze[nx][ny] == . && vis[nx][ny] == false) {
                vis[nx][ny] = true;
                q.push(make_pair(nx, ny)); 
            }
        }
        cnt++;
    }
    return cnt;
}

int main () {
    while (cin >> N >> M && N != 0) {
    //while (~scanf("%d%d", &N, &M) && N != 0) {
        //getchar();
        swap(N,M);
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                //scanf("%c", &maze[i][j]);
                cin >> maze[i][j];
                if (maze[i][j] == @) {
                    sx = i;
                    sy = j;
                }
            }
            //getchar();
        }
        memset (vis, false, sizeof(vis));
        int ans = bfs();
        printf("%d\n", ans);
    }
    
    
}

 

111

标签:ios   can   dir   ring   air   swa   ==   char   out   

原文地址:https://www.cnblogs.com/astonc/p/10661308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!