Home > Back-end >  Similar to the function of the ps magic wand, a great god give algorithm.
Similar to the function of the ps magic wand, a great god give algorithm.

Time:10-24



How to find the matrix row next to 1 in the block, and returns the number of coordinates and 1. Magic wand of PS why will find so fast, great god literacy.

CodePudding user response:

Fyi:
//item number: 0186 
//the turtle enclosure
//the difficulty level: D; Run time limit: 1000 ms; Run space limit: 51200 KB; Code length limit: 2000000 b
//item description
//turtle small pretty strut walk around on the beach, and brutally announced: "my footprints everywhere are the turtles territory,"
//the turtle any action can be broken down into the following three basic action:
//f forward to climb one
//l place left 90 degrees
//r back 90 degrees turn right
//as a result of small pretty don't allow other turtles through its territory, so the closed area is surrounded by its footprint is its territory,
//your task is to program according to the file input a series of small pretty basic action, to calculate how much small pretty were occupied territory,
//program file called turtle,
//to deepen the understanding of the question, we observe the following basic action sequence:
//FLFFFLFFFRFRRFFFLF
//# small pretty in starting point, of course, will leave footprints, the direction of the if it starts to down, can its footprint (marked by *) as the diagram below:
//* * * *
//* *
//# *
//* * * *
//obviously, small pretty territory for a total of 15,
//enter
//only one string length less than 1000, and the string does not contain the 'f', 'l', 'r' characters,
//output
//in which there is only one positive integer that territory to the total number of lattice,
//input sample
//FLFFFLFFFRFRRFFFLF
//output sample
//15
//other description
//2011 Beijing haidian district secondary problem
# include & lt; Stdio. H>
# include & lt; Stdlib. H>
# include & lt; String. H>
# define MAXN 1000
The static char n [MAXN + 2 * 2] [MAXN + 2 * 2];//record somewhere be climbed information: 0 not climb over the | 1 climb over the
Char f;//the current direction of 'N' | 'S' | 'W' | 'E'
Int x, y;//the current position
Char [MAXN p + 1];
Int I, L;
Int a, b, h, t;
Int sp=0;
Int the x1 [MAXN];
Int x2 (MAXN);
Int y1 [MAXN];
Int yy1, xx1 xx2;
Void push (int ay1, int ax1, int ax2) {
Y1 [sp]=ay1;
X1=ax1 [sp];
X2=ax2 [sp];
If (spSp=sp + 1;
} else {
Printf (" stack overflow! \n");
The exit (1);
}
}
Void pop (void) {
Sp=sp - 1;
Yy1=y1 [sp];
Xx1=x1 (sp);
Xx2=x2 (sp);
}
//void show () {
//int zy, zx;
//
//for (zy=0; Zy//for (zx=0; Zx//printf (" % d ", n [zy] [zx]);
//}
//printf (" \ n ");
//}
//printf (" \ n ");
//}
Void scanlinefill () {
Xx, int ax1, ax2;
While (1) {
If (sp<=0) break;//
Pop ();
//printf (" O yy1, xx1 xx2=% d, % d, % d \ n ", yy1, xx1, xx2);
For (xx=xx1; Xx<=xx2; Xx++) n [yy1] [xx]=2;//fill this line
Yy1 + +;
If (yy1 & lt;=2 * MAXN)
For (xx=xx1; Xx<=xx2; Xx++) {//examines from left to right next to a line each point below
If (n [yy1] [xx]==0) {
Ax1=xx - 1;
While (1) {//to the left to find the zero
If (n [yy1] [ax1]!=0) break;
Ax1 -;
}
Ax1 + +;//non zero point for the new line on the right the left point
}
If (n [yy1] [xx]==0) {
Ax2=xx + 1;
While (1) {//to the right to find the zero
If (n [yy1] [ax2]!=0) break;
Ax2 + +;
}
Ax2 -;//not 0 points left for the new line right
//printf (" I yy1 ax1, ax2=% d, % d, % d \ n ", yy1, ax1, ax2);
Push (yy1 ax1, ax2);//record new scan lines to stack
Xx=ax2 + 1;//the next cycle starting from the new point on the right side of the scan line right test
}
}
}
}
Int main () {
The scanf (" % s ", p);
F='S'.
Y=MAXN;
X=MAXN;
N [y] [x]=1;
L=strlen (p);
for (i=0; iThe switch (p [I]) {
Case 'f' :
The switch (f) {
Case 'N' : y -; N [y] [x]=1; break;
Case 'S' : y++; N [y] [x]=1; break;
Case 'W' : x -; N [y] [x]=1; break;
Case 'E' : x++; N [y] [x]=1; break;
}
break;
Case 'l' :
The switch (f) {
Case 'N' : f='W'; break;
Case 'S' : f='E'; break;
Case 'W' : f='S'. break;
Case 'E' : f='N'; break;
}
break;
Case "r" :
The switch (f) {
Case 'N' : f='E'; break;
Case 'S' : f='W'; break;
Case 'W' : f='N'; break;
Case 'E' : f='S'. break;
}
break;
}
}
//show ();
For (y=0; YFor (x=0; x//show ();
Push (1,1,2 * MAXN);//from (y1==1, the x1==1, 2 x2==* MAXN) began to
Scanlinefill ();//to use the scan line seed filling algorithm fill 2
//show ();
a=0;
For (y=0; Y<2 * MAXN + 1; Y++) {
For (x=0; x<2 * MAXN + 1; X++) {
If (n [y] [x]!=2) +;
}
}
Printf (" % d \ n ", a);
return 0;
}
  • Related