Topic links
Main topic:
The number of @ connected blocks in the figure, as long as two @ adjacent even if they are connected (oblique direction is also counted adjacent)
#include <stdio.h>#include<string.h>Charmap[ the][ the];intvis[ the][ the];intN, m, ans;intdir[8][2] = { {0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1} };voidDfsintAintb) { intI, J; VIS[A][B]=1; if(A > m | | b > N | | A <1|| b <1)return; for(intK =0; K <8; k++) { intx = a + dir[k][0]; inty = B + dir[k][1]; if(X <1|| y<1|| X>m | | Y>n)Continue; if(Map[x][y] = ='@'&&vis[x][y] = =0) {dfs (x, y); } }}intMain () {intI, J; while(SCANF ("%d%d", &m, &n)! = EOF, n | |m) {GetChar (); for(i =1; I <= m; i++) {scanf ("%s", map[i]+1); } memset (Vis,0,sizeof(VIS)); Ans=0; for(i =1; I <=m; i++) { for(j =1; J <=n; J + +) { if(Map[i][j] = ='@'&&VIS[I][J] = =0) {ans++; DFS (I, j); }}} printf ("%d\n", ans); } return 0;}
2018-03-31
UVa 572-oil deposits "DFS" Unicom block issues