Ultraviolet A 10452 Marcus, help! (DFS), 10452dfs

Source: Internet
Author: User

Ultraviolet A 10452 Marcus, help! (DFS), 10452dfs



Problem I
Marcus, help!

Input:Standard input
Output:Standard output

Time Limit:2 Seconds

"First, the breath of God.

Only the penitent man will pass.

Second, the Word of God,

Only in the footsteps of God will he proceed.

Third, the Path of God,

Only in the leap from the lion's head will he prove his worth ."

(Taken from the movie "Indiana Jones and the Last Crusade", 1989)

 

To get to the grail, Indiana Jones needs to pass three challenges. he successfully masters the first one and steps up to the second. A cobblestone path lies before him, each cobble is engraved with a letter. this is the second challenge, the Word of God, the Name of God. only the cobbles having one of the letters "IEHOVA" engraved can be stepped on safely, the ones having a different letter will break apart and leave a hole.

 

Unfortunately, he stumbles and falls into the dust, and the dust comes into his eyes, making it impossible for him to see. So he CILS for Marcus Brody and asks Marcus to tell him in which

Direction to go to safely reach the other side of the cobblestone path. because of the dust in his eyes, indy only can step "forth" to the stone right in front of him or do a side-step to the stone on the "left" or the "right" side of him. these ("forth", "left", "right") are also the commands Marcus gives to him.

 

InputThe first line of the input contains a single number indicating the number of test cases that follow. each test case starts with a line containing two numbers m and n (2 <= m, n <= 8), the length m and the width n of the cobblestone path. then follow m lines, each containing n characters ('A' to 'Z', '@', '#'), the engravement of the respective cobblestone. indy's starting position is marked with the '@ 'character in the last line, the destination with the character' # 'in the first line of the cobblestone path. each of the letters in "IEHOVA" and the characters '@' and '# 'appear exactly once in each test case. there will always be exactly one path from Indy's starting position via the stones with the letters "IEHOVA" engraved on (in that order) to the destination. there will be no other way to safely reach the destination. output

For each test case, output a line with the commands Marcus gives to Indy so that Indy safely reaches the other side. Separate two commands by one space character.

Sample Input26 5PST # TBTJASTYCVMYEHOFXIBKUN @ RJB5 4JA # xjvbnxohddqrtl @ IY Sample Outputforth forth right forth forthright forth left forth right

Problem-setter: Juergen Werner, Albert Einstein University of ULM, Germany


Question:

To give you a matrix, you can only follow the IEHOVA sequence and output the trajectory direction. @ Indicates the start point and # indicates the end point.


Solution:

DFS, note that it is going from bottom up, and I is reduced by one.


Code:

# Include <iostream> # include <cstdio> # include <string> using namespace std; const int maxN = 10; const char letter [8] = {'@', 'I', 'E', 'h', 'O', 'V', 'A ','#'}; const string str0 [3] = {"left", "forth", "right"}; int dirX [3] = {-1, 0, 1}, n, m; int dirY [3] = {0,-}; // I is not a Plus Minus. String mymap [maxN]; bool cmp (int di, int dj) {if (di> = 0 & di <n & dj> = 0 & dj <m) return true; return false;} void dfs (int I, int j, int depth) {for (int k = 0; k <3; k ++) {int di = I + dirY [k], dj = j + dirX [k]; if (cmp (di, dj) & mymap [di] [dj] = letter [depth]) {cout <str0 [k]; if (depth <= 6) cout <""; dfs (di, dj, depth + 1) ;}} int main () {int t, pi, pj; scanf ("% d", & t ); while (t --) {scanf ("% d", & n, & m); for (int I = 0; I <n; I ++) {cin> myma P [I]; for (int j = 0; j <m; j ++) {if (mymap [I] [j] = letter [0]) pi = I, pj = j;} dfs (pi, pj, 1); cout <endl;} return 0 ;} # include <iostream> # include <cstdio> # include <string> using namespace std; const int maxN = 10; const char letter [8] = {'@', 'I', 'E', 'h', 'O', 'V', 'A ','#'}; const string str0 [3] = {"left", "forth", "right"}; int dirX [3] = {-1, 0, 1}, n, m; int dirY [3] = {0,-}; // I is not a Plus Minus. String mymap [maxN]; bool cmp (int di, int dj) {if (di> = 0 & di <n & dj> = 0 & dj <m) return true; return false;} void dfs (int I, int j, int depth) {for (int k = 0; k <3; k ++) {int di = I + dirY [k], dj = j + dirX [k]; if (cmp (di, dj) & mymap [di] [dj] = letter [depth]) {cout <str0 [k]; if (depth <= 6) cout <""; dfs (di, dj, depth + 1) ;}} int main () {int t, pi, pj; scanf ("% d", & t ); while (t --) {scanf ("% d", & n, & m); for (int I = 0; I <n; I ++) {cin> mymap [I]; for (int j = 0; j <m; j ++) {if (mymap [I] [j] = letter [0]) pi = I, pj = j ;}} dfs (pi, pj, 1 ); cout <endl;} return 0 ;}




Sha 10599 Robots (II) -- Chapter Dp of Liu rujia -- Interpretation of the standard process --

The biggest feature of this program is to store each target point into an array without creating an adjacent matrix.
Why is LIS required. This seems that the route is not obvious, and LIS can implement nlogn using stacks. In this case, you can perform a memory-based search to traverse the vertices with a large serial number and determine whether they are in the lower right corner.

What's wrong with the WordPress 548-Tree?

The AC code. In the question, we will give you a binary tree's central and backward traversal.
The nodes here are in the range of 1 to 10000, with a maximum of 10000 nodes. However, the nodes here are not consecutive, not that there are N nodes that are numbered from 1 to N.
For example
1 7 8
1, 8, and 7.
Below is my AC code
# Include <cstdlib>
# Include <stdio. h>
# Include <string. h>
Const int MAX = 10005;
Int in [MAX];
Int post [MAX];
Int son [MAX] [2];
Char s [1000000];
Int p [MAX];
Int ind, dis [MAX];
Int deal (int x [], char s [])
{
Int n = 0, I;
For (I = 0; s [I]; I ++)
{
If (s [I]> = '0' & s [I] <= '9 ')
{
X [n] = 0;
While (s [I] & s [I]> = '0' & s [I] <= '9 ')
{
X [n] = x [n] * 10 + s [I]-'0 ';
I ++;
}
I --;
N ++;
}
}
Return n;
}
Int DFS (int low, int high)
{
If (low> = high) return-1;
If (ind =-1) return-1;
Int r = post [ind];
Int rp = p [r];
If (rp> = low & rp {
Ind --;

Son [r] [1] = DFS (rp + 1, high );
Son [r] [0] = DFS (low, rp );
Return r;
}
Else return-1;
}
Void getDis (int r)
{
Int t;
If (son [r] [0]! =-1)
{
T = son [r] [0];
Dis [t] = dis [r] + t;
GetDis (t );
}
If (son [r] [1]! =-1)
{
T = son [r] [1];
Dis [t] = dis [r] + t;
GetDis (t );
}
}
Void inorder (int r)
{
If (son [r] [0]! =-1) inorder (son [r] [0]);
Printf ("% d", r );
If (son [r] [1]! =-1) inorder (son [r] [1]);
}
Void postorder (int r)
{
If (son [r] [0]! =-1) postorder (son [r] [0]);

If (son [r] [1]! =-1) postorder (son [r] [1]);
Printf ("% d", r );
}
Int main ()
{
Int n, I, k;
While (gets (s ))
{
N = deal (in, s );
Gets (s );
N = deal (post, s );
If (n = 1)
{
Printf ("% d \ n", in [0]);
Continue;
}
Memset (son, 0, sizeof (son ));
For (I = 0; I <n; I ++) p [in [I] = I;
Ind = n-2;

... The remaining full text>

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.