Please point me in the question.
Exercises
This problem really good egg hurts ah, first Test instructions bad understanding, engaged in a half-day complex to die, there are so many requirements, but also require so many things, do not want to do half of the ... Feel no technical content, but also to do up dead. But obsessive-compulsive disorder has to be in order to do the problem, and finally a little bit to tune it out, say what flood fill, in fact, that is, there is no algorithm on the skill, is to see the feeling of recruit demolition recruit ...
Test instructions understand the problem again, test instructions , thank you!
The first step: according to his rules to draw out the room, traverse over each node around the wall to fill up;
The second step: deep search;
Objective 1: Record the number of deep searches, the number of rooms;
Objective 2: To record the depth of the deep search, the largest room area;
Objective 3: To mark the nodes belonging to the same tree, which is the process of dyeing, to pave the way for the next step;
Note: In the deep search of the depth of the search to pay attention to how to save, there will be a fork and then converge after the situation arises, I set a global variable deep to record the depth;
Step three: Beat the wall;
Topic said, can only play once, and there are rules, first Jinzhao west of the election, if the same west to first Jinzhao south of the election, so pay attention to the direction of the traverse. When playing the wall, it highlights the importance of the color before, first we have to find the wall, and then the color on both sides of the wall is not the same, from the bottom up, from left to right to find the best solution.
PS: Note the size of the array.
Code implementation:
/*id:eashionlang:c++task:castle*/#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #define MAX 55using namespace Std;int n,m;int sum;int size;int deep;int bsize;int bx,by;int color;int Csize [Max*max];char oritation;int maze[max][max];int dx[4] = {1,0,-1,0};int Dy[4] = {0,-1,0,1};int castle[MAX*3][MAX*3];void Solve (); void Dfs (int x,int y,int col); void break (); int main () {freopen ("castle.in", "R", stdin); Freopen ("Castle.out", "w", stdout); scanf ("%d%d", &m,&n); for (int i = 1; I <=N; i++) {for (int j = 1; J <= M; j + +) {scanf ("%d", &maze[i][j]); }} solve (); return 0;} void Solve () {Sum = 0; Size = 0; bsize = 0; color = 2; memset (csize,0,sizeof (Csize)); memset (Castle,0,sizeof (Castle)); Fill the wall, use the & operation for (int i = 1; I <= N; i++) {for (int j = 1; J <= M; j + +) {int tx = i*2-1; int ty = j*2-1; if ((maze[i][j]&1) = = 1) {castle[tx][ty-1] = 1; } if ((maze[i][j]&2) = = 2) {Castle[tx-1][ty] = 1; } if ((maze[i][j]&4) = = 4) {castle[tx][ty+1] = 1; } if ((maze[i][j]&8) = = 8) {Castle[tx+1][ty] = 1; }}}//Fill the walls for (int i = 0; I <= 2*n; i++) {castle[i][0] = 1; } for (int i = 0; I <= 2*n; i++) {castle[i][2*m] = 1; } for (int i = 0; I <= 2*m; i++) {castle[0][i] = 1; } for (int i = 0; I <= 2*m; i++) {castle[2*n][i] = 1; }//complement the wall at the node, in fact, also do not access to for (int i = 2; I <= 2*n; i+=2) {for (int j = 2; J <= 2*m; j+=2) {CAs TLE[I][J] = 1; }}//for (int i = 0; I <= 2*n; i++) {//for (int j = 0; J <= 2*m; j + +) {//printf ("%d", cast LE[I][J]);//}//printf ("\ n");//}//Deep search for (int i = 1; I <= n; i++){for (int j = 1; J <= M; j + +) {int tx = i*2-1; int ty = j*2-1; Deep = 0; if (castle[tx][ty] = = 0) {dfs (tx,ty,color++); CSIZE[COLOR-1] = deep; sum++; Size = max (size,deep); }}}//Wall break (); printf ("%d\n", Sum); printf ("%d\n", Size); printf ("%d\n", bsize); printf ("%d%d%c\n", bx,by,oritation);} void Dfs (int x,int y,int col) {//depth +1 deep++ per entry; Standard color Castle[x][y] = col; for (int i = 0; i < 4; i++) {int NX = X+dx[i]; int ny = y+dy[i]; int nnx = Nx+dx[i]; int nny = Ny+dy[i]; Both must be guaranteed to be 0 at the same time, since the next node may have been dyed, causing repeated staining if (castle[nx][ny] = = 0 && Castle[nnx][nny] = = 0) {Castle[nx ][ny] = col; DFS (NNX,NNY,COL); }} return; void Break () {//Note the traversal order for (int j = 0; J <= 2*m; j + +) {for (int i = 2*n; I >= 0; i--) {if ( CasTLE[I][J] = = 1) {int ux,uy; int dx,dy; int rx,ry; int lx,ly; UX = I-1,uy = j; DX = I+1,dy = j; Rx = I,ry = j+1; LX = i,ly = j-1; Get up and down if (UX >= 0 && DX <= 2*n && Castle[ux][uy]!=castle[dx][dy]) { int tmp = Csize[castle[ux][uy]]+csize[castle[dx][dy]]; if (Bsize < tmp) {bsize = tmp; BX = (dx+1)/2; by = (dy+1)/2; oritation = ' N '; }}///About to get through if (ly >= 0 && ry <= 2*m && castle[lx][ly]!= Castle[rx][ry]) {int tmp = Csize[castle[lx][ly]]+csize[castle[rx][ry]]; if (Bsize < tmp) {bsize = tmp; BX = (lx+1)/2; by = (ly+1)/2; oritation = ' E '; }}//Put up and down front, and strict bsize < TMP, ensure that if possible first hit ' N '}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Usaco the Castle (flood fill)