Konjac a record of problem solving process - Los carpeted valley P1003

This is to be my problem "FireWire return" after the code to a question, getting better in a good mood, send articles to share blog

table of Contents:

· Title Description

· Analysis of title

· Problem-solving ideas

·Code

·to sum up


 

· Subject description:

  In order to prepare a special awards ceremony, organizers in a rectangular area of the venue (can be seen as the first quadrant of the Cartesian coordinate system) covered a number of rectangular carpet. A total of n carpets, numbered from 1 to n. Now in accordance with these numbers carpet ascending order successively laid parallel to the axis, after laying carpet overlaid on top of previously laid carpet. After the completion of the laying of the carpet, the organizers would like to know the carpeting covering the top floor of a number of points. Note: The points on the boundary of the rectangle, and four vertices of the carpet can be considered covered by the carpet .

* Topic Analysis:

  This question appears to be a simulation that is very water water, most people's first thought is to put three cycles , each one with a two-dimensional array of analog floor venue. Yes, I started to think so, as follows:

. 1 #include <cstdio>
 2 #include <the iostream>
 . 3  
. 4  the using  namespace STD;
 . 5  
. 6  int n-;
 . 7  int A, B, G, K, XX, YY;
 . 8  int Ground [ 10010 ] [ 10010 ] = {- . 1 }; // -1 is assigned if the object has not been assigned, can output directly -1, no longer determines the (in fact lazy)
 . 9  
10  int main ()
 . 11  {
 12 is      Scanf ( " % D " , & n- );
 13 is      for ( int I = . 1 ; I <= n-; ++ I)
14      {
 15          Scanf ( " % D% D% D% D " , & A, & B, & G, & K); treated // not an array, then each time simultaneously input (with the effect after the array is better thus treated good)
 16          for ( int J = A; J <= A + G; ++ J)
 . 17              for ( int n-= B; n-<= B + K; ++ n-) // cycle is two, each an affected (both need to be updated) coordinates enumerate the floor
 18 is                  Ground [J] [n-] = I; // attach a value to the last piece of the floor assignment (simulated the uppermost piece of carpet)
 . 19      }
 20 is      Scanf ( " % D% D " , & XX, & YY);
 21 is      the printf ( " % D " , Ground [XX] [YY]);
// direct Find top floor needs to find a rug that is 22     return 0;
23 }

  Yes, this question looks water, the above code seems perfect. But the result is this:

  

  So, what led to such results it? ? ? So, I opened the shameless point solution to a problem

  Originally, according to my approach certainly seems to be the MLE, because this question is very pit, the question's purpose is to allow us to read data from front to back, and then move forward from circulation, thus solving (specific problem-solving ideas please watch the following part)

· Problem-solving ideas:

  According to the above conclusion, we can find a faster, better, better algorithms.

  First data of 2 ~ n + 1 line is read, saved to a two-dimensional array, and then n ~ 1 enumerated from each group of data, comprising a first view point of need, carpet. If so, the number of direct output of this carpet, the program ends; if not, output "-1", the same end of the program;

·Code:

  Having said that, there is no code how can do it? ? ?

. 1 #include <cstdio>
 2 #include <the iostream>
 . 3  
. 4  the using  namespace STD;
 . 5  
. 6  int shuju [ 10010 ] [ . 5 ]; // definition of a "Data" array, for reading in each set of data
 . 7  int n-, XX, YY;
 . 8  
. 9  int main ()
 10  {
 . 11      Scanf ( " % D " , & n-);
 12 is      for ( int I = . 1 ; I <= n-; ++ I)
 13 is          for ( int J = . 1 ; J <= 4;++j)
14             scanf("%d",&shuju[i][j]);//读入数据ing
15     scanf("%d%d",&xx,&yy);
16     for(int i=n;i>=1;i--)//从n~1循环
17     {
18         if((shuju[i][1]<=xx)&&(shuju[i][3]+shuju[i][1]>=xx)&&(shuju[i][2]<=yy)&&(shuju[i][2]+shuju[i][4]>= 20        {
19YY)) // determines whether to looking for the point within the carpet 
              the printf ( " % D " , I); // If so, the output point number
 21 is              return  0 ; // terminates the program
 22 is          }
 23 is      }
 24      the printf ( " -1 " ); // if not, outputs -1
 25      return  0 ; // end of program or
 26 }

·to sum up:

  This question is still very pit, because this question is not simply mindless simulation, but the use of some kind of similar to the "reverse push to restore" the idea, the idea is not a good thought. So in the process of solving them later, do not take it for granted, we must consider carefully, then it will be able to AC!

Guess you like

Origin www.cnblogs.com/juruohqk/p/11014101.html