The first feeling is to use the search to do, violence can be solved, here will be converted into a 3*3 of the matrix, and then search the vegetarian can be. After writing it, I did it over and over again, 31ms. Attached code:
#include"iostream"#include"stdio.h"#include"algorithm"#include"Cmath"#include"Queue"#include"Stack"#include"Vector"#include"string"#include"string.h"using namespacestd;Const intmx=1005;inta[3][3]={{0,1,0},{1,1,0},{0,0,0}};intb[3][3]={{0,1,0},{0,1,1},{0,0,0}};intc[3][3]={{0,0,0},{1,1,0},{0,1,0}};intd[3][3]={{0,0,0},{0,1,1},{0,1,0}};inte[3][3]={{0,1,0},{0,1,0},{0,1,0}};intf[3][3]={{0,0,0},{1,1,1},{0,0,0}};intg[3][3]={{0,1,0},{1,1,1},{0,0,0}};inth[3][3]={{0,1,0},{1,1,0},{0,1,0}};inti[3][3]={{0,0,0},{1,1,1},{0,1,0}};intj[3][3]={{0,1,0},{0,1,1},{0,1,0}};intk[3][3]={{0,1,0},{1,1,1},{0,1,0}};intdir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};intMAZE[MX][MX];BOOLVISITED[MX][MX];intm,n,wellspring;BOOLJudgeintXinty) { if(x>=0&&x<3*m&&y>=0&&y<3*n&&maze[x][y]==1&&!Visited[x][y])return true; return false;}voidDfsintXinty) { intDx,dy; for(intI=0;i<4; i++) {DX=x+dir[i][0]; Dy=y+dir[i][1]; if(judge (Dx,dy)) {Visited[dx][dy]=true; DFS (Dx,dy); } }}voidCreat_maze (inta[][3],intIintj) { intp,q; for(p=i*3;p <i*3+3;p + +) { for(q=j*3; q<j*3+3; q++) {Maze[p][q]=a[p-i*3][q-j*3]; } }}voidWellspring_count () {inti,j; for(i=0;i<3*m;i++) { for(j=0;j<3*n;j++) { if(maze[i][j]==1&&!Visited[i][j]) {wellspring++; VISITED[I][J]=true; DFS (I,J); }}} printf ("%d\n", wellspring);}voidinput () {inti,j; while(SCANF ("%d%d", &m,&n), m!=-1|| n!=-1) {memset (visited,false,sizeof(visited)); Wellspring=0; Charpipe; for(i=0; i<m;i++) {GetChar (); for(j=0; j<n;j++) {scanf ("%c",&pipe); Switch(pipe) { Case 'A': Creat_maze (A,I,J); Break; Case 'B': Creat_maze (B,I,J); Break; Case 'C': Creat_maze (C,I,J); Break; Case 'D': Creat_maze (D,I,J); Break; Case 'E': Creat_maze (E,I,J); Break; Case 'F': Creat_maze (F,I,J); Break; Case 'G': Creat_maze (G,I,J); Break; Case 'H': Creat_maze (H,I,J); Break; Case 'I': Creat_maze (I,I,J); Break; Case 'J': Creat_maze (J,I,J); Break; Case 'K': Creat_maze (K,I,J); Break; }}} wellspring_count (); }}intMain () {//freopen ("E:\\in.txt", "R", stdin);input (); return 0;}
View Code
But this is a topic in the collection, so there should be a more efficient way, after all, my code is not used and check set.
So think again, transient look at someone else's and look at the set of ideas, and wrote a code.
HDU Farm Irrigation