A simple ACM question

Source: Internet
Author: User

This semester I chose an algorithm analysis and design. The teacher introduced ACM questions for us to practice. to be honest, I have caught a cold on ACM, mainly because I feel that it is too far away from reality and hard to learn. however, my recent studies have given me a new idea. In my studies, I have tried some ACM questions properly (I think it makes no sense to stick to this question all the time ), it can indeed greatly improve the programming capability and logic thinking. maybe I don't have many questions. I think the thinking advocated by ACM is "From complexity to simplicity ". especially in the tree. as we all know, tree traversal takes a lot of time. Once the height of the tree increases, the efficiency of traversing the tree is often a nightmare.
The problem of tree resolution in ACM is often as follows: Abstract The tree model based on actual problems (such models are often diverse). This is a complicated process, then find out the repeated nodes of the tree (a very high tree often has many repeated nodes), and then ignore these repeated nodes in the traversal to improve efficiency.
The efficiency of the ACM tree is usually as follows: recursion = recursion + highest efficiency

On Peking University's website, a simple ACM question is to use trees to solve the problem:
Http://acm.pku.edu.cn/JudgeOnline/showproblem? Problem_id = 1088

The following is my c ++ code:
# Include <iostream>
# Include <string>
# Include <stdio. h>
Using namespace STD; // use the standard library function
Int temp [100] [100];
Int R, C;
Int point [100] [100];
Int I = 0, j = 0;
Int around (int m, int N, int K, int s) {// determines whether the point is "low" than the start point"
If (point [m] [N] <point [k] [s]) {
Return 1;
}
Else return 0;
}
Int look (int m, int N ){
Int max = 0;
Int A = 0, B = 0, c = 0, D = 0;
If (temp [m] [N]! = 0) {return temp [m] [N];} // if this point has been calculated, do not perform computation to improve efficiency.
If (m + 1 <r) if (around (m + 1, n, m, n) = 1) {A = look (m + 1, n) + 1;} // traverse the four points around the point
If (m-1> = 0) if (around (S-1, n, m, n) = 1) {B = look (m-1, n) + 1 ;}
If (n + 1 <c) if (around (m, n + 1, m, n) = 1) {c = look (m, n + 1) + 1 ;}
If (n-1> = 0) if (around (M, n-1, m, n) = 1) {d = look (M, n-1) + 1 ;}
Max =;
If (B> = max) max = B;
If (C> = max) max = C;
If (D> = max) max = D;
Return Max;
}
Int main (){
Int Ss = 0;
Cin> r> C;
For (I = 0; I <r; I ++ ){
For (j = 0; j <C; j ++ ){
Cin> point [I] [J];
}
}
For (I = 0; I <r; I ++ ){
For (j = 0; j <C; j ++ ){
Temp [I] [J] = look (I, j );
If (SS <temp [I] [J]) Ss = temp [I] [J];
}
}
Ss = SS + 1; // The height of the lowest point is not calculated. Add 1 to add
Cout <SS <Endl;
}
This algorithm was tested at Peking University for 93 ms and passed its time efficiency test. of course, there must be a better algorithm than it. My idea is just a common idea. It is very studious for us, ordinary people. Why not?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.