【题解】Image Perimeters-C++
发布时间:2020-12-16 07:18:41 所属栏目:百科 来源:网络整理
导读:题目 Description 给出一张由"x"和".“组成的矩阵。每个"x"可以向上下左右及两个斜对角进行连通,请问由某个点开始的"x”,它所连通的图形的周长为多少。 Input 整个测试有多组数据,整个测试以四个零代表结束。 对于每个数据,第一行给出整个图形的大小(长
题目 Sample Output 思路 代码 1 #include<bits/stdc++.h> 2 using namespace std; 3 int cnt,dir[8][2]={{1,0},{-1,{0,-1},1},{1,1}}; 4 char a[51][51]; 5 struct node 6 { 7 int x; 8 int y; 9 node(){}; 10 node(int xx,int yy) 11 { 12 x=xx,y=yy; 13 } 14 }; 15 queue<node> q; 16 queue<node> qu; 17 bool vis[51][51]; 18 void bfs(int x,int y) 19 { 20 qu.push(node(x,y)); 21 vis[x][y]=1; 22 while(!q.empty()) 23 { 24 node now=q.front(); 25 q.pop(); 26 for(int i=0;i<8;i++) 27 { 28 int tx=now.x+dir[i][0],ty=now.y+dir[i][1]; 29 if(!vis[tx][ty]&&a[tx][ty]==‘X‘) 30 { 31 q.push(node(tx,ty)); 32 qu.push(node(tx,ty)); 33 vis[tx][ty]=1; 34 } 35 } 36 } 37 } 38 int main() 39 { 40 int n,m,x,y; 41 while(cin>>n>>m>>x>>y&&n&&m) 42 { 43 memset(vis,0,sizeof(vis)); 44 memset(a,‘ |