|
101- Blocks Problem |
67864 |
19.16% |
14194 |
|
Question link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 103 & page = show_problem & problem = 37
Question type: data structure, binary tree
Question:
There are n locations, numbered 0 ~ N-1, in the initial stage, place the same brick as the position number on each position, that is, brick 1, brick 2 ...... Brick N-1. There are four command operations:
1. Move a onto B: move Brick a to Brick B. If there are bricks on both A and B, put them back to the original position.
2. Move a over B: Move a to the "Brick Heap" where B is located. Before moving a, restore all the bricks on a to the original position. The heap of B remains unchanged.
3. Pile A onto B: Move (including a) above A to B. Put the brick above B back to the original position.
4. Pile A over B:
Directly move (including a) above a to the "brick stack" where B is located.
5. Quit: End command.
Idea of disintegration: At first glance, I wanted to use stack simulation, but there would be a lot of such operations, maybe TLE. So I simulate and write classes similar to stacks, but they can be stored randomly. STL is indeed easy to use, but not flexible.
# Include <cstdio> # include <iostream> # include <string> using namespace STD; int N, block_a, block_ B, pos_a, pos_ B; string command1, command2; Class pile {public: pile () {Index = 0;} void init_set (int A) {Index = 0; arr [index ++] = A;} int top () {return arr [index-1];} void add (int A) {arr [index ++] = A;} bool is_empty () {If (Index = 0) return true; return false;} int find (int A) {for (INT I = 0; I <index; ++ I) {if (ARR [I] = A) return I;} return-1;} void output () {If (index! = 0) {for (INT I = 0; I <index; ++ I) {printf ("% d", arr [I]) ;}} int arr [100]; int index ;}; pile arr [100]; // locate the heap where brick A is located. Int find_block (int A) {int result; For (INT I = 0; I <n; ++ I) {result = arr [I]. find (a); If (result! =-1) return I ;}// return all the bricks on Brick a to the initial position. Void return_block (INT POs, int A) {If (ARR [POS]. top () = A) return; int Index = arr [POS]. find (a); For (INT I = index + 1; I <arr [POS]. index; ++ I) {int temp = arr [POS]. arr [I]; arr [temp]. init_set (temp);} arr [POS]. index = index + 1;} void move_a_onto_ B (int A, int B) {if (a = B) return; pos_a = find_block (a); pos_ B = find_block (B ); if (pos_a = pos_ B) return; return_block (pos_a, a); return_block (pos_ B, B); arr [pos_ B]. add (a); -- arr [pos_a]. index;} void move_a_over_ B (int A, int B) {if (a = B) return; pos_a = find_block (a); pos_ B = find_block (B ); if (pos_a = pos_ B) return; return_block (pos_a, a); arr [pos_ B]. add (a); -- arr [pos_a]. index;} void pile_a_onto_ B (int A, int B) {if (a = B) return; pos_a = find_block (a); pos_ B = find_block (B ); if (pos_a = pos_ B) return; return_block (pos_ B, B); int Index = arr [pos_a]. find (a); For (INT I = index; I <arr [pos_a]. index; ++ I) {arr [pos_ B]. add (ARR [pos_a]. arr [I]);} arr [pos_a]. index = index;} void pile_a_over_ B (int A, int B) {if (a = B) return; pos_a = find_block (a); pos_ B = find_block (B ); if (pos_a = pos_ B) return; int Index = arr [pos_a]. find (a); For (INT I = index; I <arr [pos_a]. index; ++ I) {arr [pos_ B]. add (ARR [pos_a]. arr [I]);} arr [pos_a]. index = index;} void solve () {If (command1 = "move" & command2 = "onto") {move_a_onto_ B (block_a, block_ B );} else if (command1 = "move" & command2 = "over") {move_a_over_ B (block_a, block_ B );} else if (command1 = "pile" & command2 = "onto") {pile_a_onto_ B (block_a, block_ B );} else if (command1 = "pile" & command2 = "over") {pile_a_over_ B (block_a, block_ B) ;}} void Init (INT N) {for (INT I = 0; I <n; ++ I) {arr [I]. init_set (I) ;}} void output () {for (INT I = 0; I <n; ++ I) {printf ("% d:", I ); arr [I]. output (); printf ("\ n") ;}} int main () {freopen ("input.txt", "r", stdin); scanf ("% d ", & N); Init (n); While (CIN> command1> block_a> command2> block_ B) {If (command1 = "quit") break; solve () ;}output (); Return 0 ;}
-- The meaning of life is to give it meaning.
OriginalHttp://blog.csdn.net/shuangde800,
D_double