HDU 1312 矩阵中找出初始点的可达点数 简单bfs

来源:互联网 发布:java中的关键字 编辑:程序博客网 时间:2024/05/21 09:27
#include <iostream>#include <queue>using namespace std;const int N = 21;int dir[4][2] = {{0,1}, {0, -1}, {1, 0}, {-1, 0}};char a[N][N];int cnt = 0;int n, m;struct node {int x, y;};void bfs(int x, int y){struct node cur, next;cur.x = x;cur.y = y;queue<node> q;q.push(cur);while(!q.empty()){cur = q.front();q.pop();for(int i = 0; i < 4; i++){next.x = cur.x + dir[i][0];next.y = cur.y + dir[i][1];if(next.x >=0 && next.x < n && next.y >= 0 && next.y < m && a[next.x][next.y] == '.'){cnt++;q.push(next);a[next.x][next.y] = '#';}}}}int main(){int x, y;while(cin >> m >> n, m || n ){for(int i = 0; i < n ; i++)scanf("%s", a+i);for(int i = 0; i < n; i++){for(int j = 0; j < m; j++)if(a[i][j] == '@'){x = i;y = j;break;}}cnt = 1;bfs(x, y);cout << cnt << endl;}return 0;}

原创粉丝点击