SDUT 2152 Balloons(BFS 第一届山东省赛)

#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>using namespace std;struct node{int x;int y;};int jx1[] = {1,0,0,-1};int jy1[] = {0,1,-1,0};int jx2[] = {1,0,-1,0,-1,1,1,-1};int jy2[] = {0,1,0,-1,-1,1,-1,1};char map[110][110];int v[110][110];int n;void BFS1(int x,int y){struct node t,f;queue<node>q1;t.x = x;t.y = y;q1.push(t);v[t.x][t.y] = 1;while(!q1.empty()){t = q1.front();q1.pop();for(int i=0; i<4; i++){f.x = t.x + jx1[i];f.y = t.y + jy1[i];if(f.x>=0 && f.x<n && f.y>=0 && f.y<n && v[f.x][f.y] == 0 && map[f.x][f.y] == '1'){q1.push(f);v[f.x][f.y] = 1;}}}}void BFS2(int x,int y){struct node t,f;queue<node>q2;t.x = x;t.y = y;q2.push(t);v[t.x][t.y] = 1;while(!q2.empty()){t = q2.front();q2.pop();for(int i=0; i<8; i++){f.x = t.x + jx2[i];f.y = t.y + jy2[i];if(f.x>=0 && f.x<n && f.y>=0 && f.y<n && v[f.x][f.y] == 0 && map[f.x][f.y] == '1'){q2.push(f);v[f.x][f.y] = 1;}}}}int main(){int kk = 0;while(scanf("%d",&n)!=EOF){if(n == 0){break;}memset(map,0,sizeof(map));for(int i=0; i<n; i++){scanf("%s",map[i]);}memset(v,0,sizeof(v));int ans = 0;int cnt = 0;for(int i=0; i<n; i++){for(int j=0; j<n; j++){if(v[i][j] == 0){if(map[i][j] == '1'){BFS1(i,j);ans++;}}}}memset(v,0,sizeof(v));for(int i=0; i<n; i++){for(int j=0; j<n; j++){if(v[i][j] == 0){if(map[i][j] == '1'){BFS2(i,j);cnt++;}}}}printf("Case %d: %d %d\n",++kk,ans,cnt);printf("\n");}return 0;}

,只要相信,期待就会成真

SDUT 2152 Balloons(BFS 第一届山东省赛)

相关文章:

你感兴趣的文章:

标签云: