Original title Link https://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=598
The problem is to find X in a bunch of *, the adjacent x is one, so we can two times Dfs search, DFS2 * How many heaps, dfs2 search a bunch of * How many different x, and then in ascending order each heap inside the different x output = =
#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespacestd;Charma[ -][ -];intn,m;intT;intdis[4][2]={{1,0},{-1,0},{0,1},{0,-1}};intnum[ -];voidDFS1 (intXinty) { intXx,yy; Ma[x][y]='*'; for(intI=0;i<4; i++) {xx=x+dis[i][0]; YY=y+dis[i][1]; if(xx<0|| xx>=n| | yy<0|| yy>=m| | ma[xx][yy]=='*') Continue; if(ma[xx][yy]=='X') DFS1 (XX,YY); }}voidDFS2 (intXinty) { intXx,yy; Ma[x][y]='.'; for(intI=0;i<4; i++) {xx=x+dis[i][0]; YY=y+dis[i][1]; if(xx<0|| xx>=n| | yy<0|| yy>=m| | ma[xx][yy]=='.') Continue; if(ma[xx][yy]=='X') {DFS1 (XX,YY); Num[t]++; } if(ma[xx][yy]=='*') DFS2 (XX,YY); }}intMain () {intCnt=0; while(SCANF ("%d%d", &m,&n)!=eof&&m&&N) {t=0; memset (num,0,sizeof(num)); Memset (MA,'.',sizeof(MA)); for(intI=0; i<n;i++) scanf ("%s", Ma[i]); for(intI=0; i<n;i++) { for(intj=0; j<m;j++) { if(ma[i][j]=='*') {DFS2 (i,j); T++; }}} sort (Num,num+t); printf ("Throw%d\n",++CNT); for(intI=0; i<t-1; i++) printf ("%d", Num[i]); printf ("%d\n", num[t-1]); printf ("\ n"); } return 0;}
Uva657-the Die is cast