[Usaco] open11 bronze Division

Source: Internet
Author: User

Problem 11: bfire

A lot of questions are mentioned. In fact, the problem of abstracting into programming is: given n numbers 1 ~ Place N in the range from 1 ~ The N position is now moved so that N1 is placed at N2, N2 is placed at N4, and NK is placed at n2k. If n2k is out of bounds, the count is continued from position 1, and so on, ask which number will appear at location 1 or where the number is to be placed has been moved? For example:

1 2 3-> 2

1 2 3 4-> 4

1 2 3 4 5-> 3

1 2 3 4 5 6-> 4

The solution is actually very simple. Just simulate the movement of numbers. The boundary condition is the two stop conditions mentioned above.

 

/* <Br/> ID: <br/> LANG: C <br/> task: bfire <br/> */<br/> # include <stdio. h> <br/> # include <string. h> <br/> # include <stdlib. h> <br/> # define n 250 <br/> int main () {<br/> file * fin = fopen ("bfire. in "," R "); <br/> file * fout = fopen (" bfire. out "," W "); <br/> int cows [n + 1] = {0}; <br/> int N, cur, next; <br/> fscanf (FIN, "% d", & N); <br/> next = 2; <br/> while (next! = 1 & cows [next] = 0) {<br/> cows [next] = 1; <br/> cur = next; <br/> next + = next; <br/> If (next> N) Next = Next-N; // next = next % N; <br/>}< br/> fprintf (fout, "% d/N", cur); <br/> exit (0 ); <br/>}< br/> 

 

 

Note that for convenience, my array subscript starts from 1, so that all the k power of 2, such as 128, returns 0, which is actually 128, therefore, we should not take the modulo. Just subtract n.

 

<---------------------------------------------------------------------------- Chic split line break>

 

Problem 12: ssort

It's a bit like a fast sorting, a very clear recursive solution, testing is complete, great!

 

/* <Br/> ID: <br/> LANG: C <br/> task: ssort <br/> */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # define N 10 <br/> int dist_moved = 0; <br/> void ssort (int * cows, int start, int size) {<br/> If (size = 1) <br/> return; <br/> int half = size/2; <br/> int I, j, TMP; <br/> ssort (cows, start, half); <br/> ssort (cows, start + half, half ); <br/> If (COWS [start]> cows [start + half]) {<br/> J = start + half; <br/> for (I = 0; I <palf; I ++) {<br/> TMP = cows [start + I]; <br/> cows [start + I] = cows [J + I]; <br/> cows [J + I] = TMP; <br/>}< br/> dist_moved + = half * size; <br/>}< br/> int main () {<br/> file * fin = fopen ("ssort. in "," R "); <br/> file * fout = fopen (" ssort. out "," W "); <br/> int cows [1 <n]; <br/> int I, j, N; <br/> fscanf (FIN, "% d", & N); <br/> for (I = 0; I <1 <n; I ++) {<br/> fscanf (FIN, "% d", & J); <br/> cows [I] = J; <br/>}< br/> ssort (cows, 0, 1 <n); <br/> fprintf (fout, "% d/N", dist_moved); <br/> for (I = 0; I <1 <n; I ++) <br/> fprintf (fout, "% d/N", cows [I]); <br/> exit (0); <br/>}< br/> 

 

 

<-------------------------------------------------------------------------- Division line of complaints>

 

Problem 13: space3d

After reading the analysis, I mentioned a flood fill algorithm. I searched the internet and wrote a recursive solution. The result is segmentation fault...

I thought it would be stack overflow, and the recursion level is too deep.

Refer to this article flood-fill Seed Filling

/* <Br/> ID: <br/> LANG: C <br/> task: space3d <br/> */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # define n 100 <br/> void flood_fill (char (* s) [N] [N], INT (* V) [N] [N], int N, int I, Int J, int K) {<br/> If (I <0 | j <0 | K <0 | I> N | j> N | K> N | V [I] [J] [k] | s [I] [J] [k]! = '*') <Br/> return; <br/> V [I] [J] [k] = 1; <br/> // printf ("(% d, % d, % d) is visited/N", I, j, k); <br/> flood_fill (S, v, N, I-1, j, k); <br/> flood_fill (S, V, N, I + 1, j, k); <br/> flood_fill (S, v, N, I, J-1, k); <br/> flood_fill (S, V, N, I, j + 1, k); <br/> flood_fill (S, v, N, I, j, k-1); <br/> flood_fill (S, V, N, I, j, k + 1 ); <br/>}< br/> int main () {<br/> file * fin = fopen ("space3d. in "," R "); <br/> file * fout = Fopen ("space3d. out "," W "); <br/> char C, space [N] [N] [N]; <br/> int visit [N] [N] [N]; <br/> int N, Count = 0; <br/> int I, J, K; <br/> fscanf (FIN, "% d", & N); <br/> GETC (FIN); <br/> for (k = 0; k <N; k ++) <br/> for (I = 0; I <n; I ++) {<br/> for (j = 0; j <N; j ++) {<br/> C = GETC (FIN); <br/> space [I] [J] [k] = C; <br/> visit [I] [J] [k] = 0; <br/>}< br/> GETC (FIN ); <br/>}< br/> for (k = 0; k <n; k ++) <br/> for (I = 0; I <N; I ++) <br/> for (j = 0; j <n; j ++) <br/> If (space [I] [J] [k] = '*'&&! Visit [I] [J] [k]) {<br/> flood_fill (space, visit, N, I, j, k); <br/> count ++; <br/>}< br/> fprintf (fout, "% d/N", count); <br/> exit (0 ); <br/>}< br/> 

For further optimization, refer to the article "optimization of seed filling".

If you do not have to take a closer look, let's take a look at the idea and use the queue to solve the inefficiency of recursion. The following is the code

/* <Br/> ID: <br/> LANG: C <br/> task: space3d <br/> */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # define n 100 <br/> // int queue [N * n + 1] [3]; <br/> char queue [N * n + 1] [3]; <br/> char space [N] [N] [N]; <br/> char visit [N] [N] [N]; <br/> void flood_fill (char (* s) [N] [N], char (* V) [N] [N], int N, int I, Int J, int K) {<br/> // char queue [N * n + 1] [3]; <br/> int head, end; <br/> head = END = 0; <br/> V [I] [J] [K] = 1; <br/> queue [end] [0] = I; <br/> queue [end] [1] = J; <br/> queue [end] [2] = K; <br/> end ++; <br/> // printf ("add (% d, % d, % d), head = % d, end = % d/N ", I, J, K, Head, end); <br/> while (Head <End) {<br/> I = queue [head] [0]; <br/> J = queue [head] [1]; <br/> K = queue [head] [2]; <br/> head ++; <br/> if (I + 1> = 0 & I + 1 <n & S [I + 1] [J] [k] = '*'&&! V [I + 1] [J] [k]) {<br/> V [I + 1] [J] [k] = 1; <br/> queue [end] [0] = I + 1; <br/> queue [end] [1] = J; <br/> queue [end] [2] = K; <br/> end ++; <br/> // printf ("add (% d, % d, % d), head = % d, end = % d/N ", I + 1, J, K, Head, end ); <br/>}< br/> If (I-1> = 0 & I-1 <n & S [I-1] [J] [k] = '*'&&! V [I-1] [J] [k]) {<br/> V [I-1] [J] [k] = 1; <br/> queue [end] [0] = I-1; <br/> queue [end] [1] = J; <br/> queue [end] [2] = K; <br/> end ++; <br/> // printf ("add (% d, % d, % d), head = % d, end = % d/N ", I-1, J, K, Head, end ); <br/>}< br/> If (J + 1> = 0 & J + 1 <n & S [I] [J + 1] [k] = '*'&&! V [I] [J + 1] [k]) {<br/> V [I] [J + 1] [k] = 1; <br/> queue [end] [0] = I; <br/> queue [end] [1] = J + 1; <br/> queue [end] [2] = K; <br/> end ++; <br/> // printf ("add (% d, % d, % d), head = % d, end = % d/N ", I, j + 1, K, Head, end ); <br/>}< br/> If (J-1> = 0 & J-1 <n & S [I] [J-1] [k] = '*'&&! V [I] [J-1] [k]) {<br/> V [I] [J-1] [k] = 1; <br/> queue [end] [0] = I; <br/> queue [end] [1] = J-1; <br/> queue [end] [2] = K; <br/> end ++; <br/> // printf ("add (% d, % d, % d), head = % d, end = % d/N ", I, J-1, K, Head, end ); <br/>}< br/> If (k + 1> = 0 & K + 1 <n & S [I] [J] [k + 1] = '*'&&! V [I] [J] [k + 1]) {<br/> V [I] [J] [k + 1] = 1; <br/> queue [end] [0] = I; <br/> queue [end] [1] = J; <br/> queue [end] [2] = k + 1; <br/> end ++; <br/> // printf ("add (% d, % d, % d), head = % d, end = % d/N ", I, j, k + 1, Head, end ); <br/>}< br/> If (k-1> = 0 & K-1 <n & S [I] [J] [k-1] = '*'&&! V [I] [J] [k-1]) {<br/> V [I] [J] [k-1] = 1; <br/> queue [end] [0] = I; <br/> queue [end] [1] = J; <br/> queue [end] [2] = K-1; <br/> end ++; <br/> // printf ("add (% d, % d, % d), head = % d, end = % d/N ", I, j, K-1, Head, end ); <br/>}< br/> int main () {<br/> file * fin = fopen ("space3d. in "," R "); <br/> file * fout = fopen (" space3d. out "," W "); <br/> char C; <br/> // char space [N] [N] [N]; <br/> // int visit [N] [N] [N]; <B R/> int N, Count = 0; <br/> int I, j, k; <br/> fscanf (FIN, "% d", & N ); <br/> GETC (FIN); <br/> for (k = 0; k <n; k ++) <br/> for (I = 0; I <n; I ++) {<br/> for (j = 0; j <n; j ++) {<br/> C = GETC (FIN ); <br/> space [I] [J] [k] = C; <br/> visit [I] [J] [k] = 0; <br/>}< br/> GETC (FIN); <br/>}< br/> for (k = 0; k <n; k ++) <br/> for (I = 0; I <n; I ++) <br/> for (j = 0; j <n; j ++) <br/> If (space [I] [J] [k] = '*'&&! Visit [I] [J] [k]) {<br/> // printf ("Flood Center (% d, % d, % d)/n", I, j, k); <br/> flood_fill (space, visit, N, I, j, k); <br/> count ++; <br/>}< br/> fprintf (fout, "% d/N", count); <br/> // printf ("% d/N ", count); <br/> exit (0); <br/>}< br/>

First of all, the program is quite bad! The six if statements are really bad !! Look at other people's programs and you will know... (This will be improved later)

Second, I spent half an afternoon in memory allocation exceeding the limit. When both the queue array and visit array are declared as int type, the program exceeds the memory limit.

After reading other people's solutions, I tried to declare these two arrays as char type. That's it. It's speechless...

Char visit-> 15636kb

Char queue-> 9784kb

Char both-> 6852kb

 

 

 

<-------------------------------------------------------------------------- Broken line of egg pain>

 

Problem 14: stringe

The question is very simple, but segmentation fault !! The buffer length definition is small, for example

0 9 chess data, in fact, after the execution, there should be two 9 Power chess, so you need to modify the macro definition:

# Change define max_len * 12

# Define max_len Len * 1 <12

The maximum overhead is to allocate two static arrays of over 400 K...

Below is the program

 

/* <Br/> ID: <br/> LANG: C <br/> task: stringe <br/> */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> # define z 100 <br/> # define Len 100 <br/> # define max_len Len * 1 <12 <br/> typedef struct {<br /> char s [Len + 1]; <br/> int n_ I, C_ I; <br/>} stringe; <br/> int main () {<br/> file * fin = fopen ("stringe. in "," R "); <br/> file * fout = fopen (" stringe. out "," W "); <br/> stringe STR [Z]; <br/> char sbuf [max_len + 1], tbuf [max_len + 1]; <br/> int I, j, k, n; <br/> fscanf (FIN, "% d", & N); <br/> for (I = 0; I <n; I ++) <br/> fscanf (FIN, "% d % s", & STR [I]. n_ I, & STR [I]. c_ I, STR [I]. s); <br/> for (I = 0; I <n; I ++) {<br/> strcpy (sbuf, STR [I]. s); <br/> for (j = 0; j <STR [I]. c_ I; j ++) {<br/> memset (tbuf, '/0', max_len); <br/> for (k = STR [I]. n_ I; k <strlen (sbuf); k ++) <br/> tbuf [k-str [I]. n_ I] = sbuf [k]; <br/> strcat (tbuf, sbuf); <br/> strcpy (sbuf, tbuf ); <br/>}< br/> fprintf (fout, "% s/n", tbuf); <br/> // printf ("% s/n ", tbuf); <br/>}< br/> exit (0); <br/>}< br/> 

 

 

As a result, your sister has timed out! Continue optimization. Remove the struct and assign the string length value to a variable. The two buffers exchange output results to avoid unnecessary string copying, similar to the use of the backup buffer in image processing...

/* <Br/> ID: <br/> LANG: C <br/> task: stringe <br/> */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <string. h> <br/> # define Len 100 <br/> # define max_len Len * 1 <12 <br/> int main () {<br/> file * fin = fopen ("stringe. in "," R "); <br/> file * fout = fopen (" stringe. out "," W "); <br/> char sbuf [max_len + 1], tbuf [max_len + 1]; <br/> int I, j, k, n, len; <br/> int n_ I, C_ I; <br/> fscanf (FIN, "% d", & N); <br/> for (I = 0; I <n; I ++) {<br/> fscanf (FIN, "% d % s", & n_ I, & C_ I, sbuf ); <br/> for (j = 0; j <C_ I; j ++) {<br/> If (J % 2 = 0) {<br/> memset (tbuf, '/0', max_len); <br/> Len = strlen (sbuf); <br/> for (k = n_ I; k <Len; k ++) <br/> tbuf [k-n_ I] = sbuf [k]; <br/> strcat (tbuf, sbuf ); <br/>}< br/> else {<br/> memset (sbuf, '/0', max_len); <br/> Len = strlen (tbuf ); <br/> for (k = n_ I; k <Len; k ++) <br/> sbuf [k-n_ I] = tbuf [k]; <br/> strcat (sbuf, tbuf); <br/>}< br/> If (J % 2 = 0) <br/> fprintf (fout, "% s/n", sbuf); <br/> else <br/> fprintf (fout, "% s/n ", tbuf); <br/>}< br/> exit (0); <br/>}< br/> 

 

This article once again confirms the warning sentence "The program is not debugged". When writing a program, you should pay attention to space allocation, program efficiency, and other issues. A good design can produce good results, the program is to pursue simplicity and efficiency. Like your previous program, does redundant string copying not take time? Wipe!

 

 

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.