NOJ cyber competition D Question tianshen Elementary School, noj tianshen

Source: Internet
Author: User

NOJ cyber competition D Question tianshen Elementary School, noj tianshen

Question:

Days God elementary school time limit (General/Java): 1000 MS/3000 MS running memory limit: 65536 KByte
Total submission: 148 pass the test: 53

Description

Corpse party: blood drive has such a paragraph. The squad leader, Sakaki Yayu, obtained the crystal stone of six Ghost doors, resulting in nirvana? The crash of the day, relying on the power of the child to escape the day. (Plot or something is not important)

Now we assume that there is no lucky son, and the shift leader needs to escape from the tianshen elementary school by his own strength. The tianshen primary school can be regarded as a two-dimensional maze. Every second, we can only go from the current position to the four adjacent grids on the upper and lower sides, because tianxiao has been crashing, therefore, there are many points that cannot be walked. Ask the shift leader if they can completely collapse in the day, that is, they can escape from the day God elementary school within t seconds.



Input

The first line is an integer T, indicating the number of data groups.

Enter 3 integers n, m, and t in the first row of each data group to indicate the number of rows, columns, and the remaining time from the day crash. (3 ≤ n, m ≤ 20, t ≤ 100)

Next, enter n rows. Each row has a string of m.

The character '.' indicates that traffic is accessible.

The character '*' indicates that it is not accessible.

Character 'O' indicates exit

Character 'X' indicates the start position of the shift leader


Output

If you can escape the output "happy end"

Otherwise, "bad end" is output"


Sample Input

2

5 5 13
.....
.***.
. * X * O
.*.*.
...*.

5 5 14
.....
.***.
. * X * O
.*.*.
...*.

Sample output

Bad end
Happy end

Question Source

Kojimai



Question Analysis:

Search questions. The following code can be AC, but I don't know if I understand the meaning of the subject correctly. The following code is for reference only. It uses DFS.



The Code is as follows:

/**. Cpp ** Created on: February 24, 2015 * Author: Administrator */# include <iostream> # include <cstdio> using namespace std; const int maxn = 25; char map [maxn] [maxn]; // map matrix. int n; // number of rows int m; // Number of columns int t; // destination time int x1, y1; // start point int x2, y2; // end point int dir [4] [2] = {// direction matrix {1, 0}, {-1, 0}, {0, 1}, {0, -1 }};/*** determine whether the next step is legal */bool check (int x, int y) {if (x <0 | x> = n | y <0 | y> = m) {// example If the coordinates cross-border return false; // indicates that the next step is invalid} return true; // otherwise, the next step is valid}/*** DFS. * Si: number of rows of the current node * sj: Number of columns of the current node * cnt: Arrival (si, sj) is the time used */bool dfs (int si, int sj, int cnt) {if (si = x2 & sj = y2 & cnt <= t) {// return true if the target point is reached within the specified time; // return true} int I; for (I = 0; I <4; ++ I) {// traverse the adjacent node int tempi = si + dir [I] [0] of the current node; // calculate the coordinates of the next node. int tempj = sj + dir [I] [1]; if (check (tempi, tempj) = false) {// if the coordinates of the next vertex are invalid, continue; // skip this vertex and calculate the next vertex.} if (map [tempi] [tempj]! = '*') {// If the current node is not a wall map [tempi] [tempj] = '*'; // set the current node to a wall bool flag = dfs (tempi, tempj, cnt + 1); // search down this node if (flag = true) {// return true if this path is feasible; // return true. indicates that the target node can arrive} // when the following code is notified, the target node cannot be reached through this node map [tempi] [tempj] = '. '; // reset the current node '. 'availability status} return false; // If none of the above paths can be found. now, it indicates that the target node cannot be reached} int main () {int ttt; scanf ("% d", & ttt); while (ttt --) {scanf ("% d", & n, & m, & t); int I; int j; for (I = 0; I <n; ++ I) {cin> map [I];} int wall = 0; // Number of walls used in the village for (I = 0; I <n; ++ I) {for (j = 0; j <m; ++ j) {if (map [I] [j] = 'X ') {// record start point position x1 = I; y1 = j;} else if (map [I] [j] = 'O ') {// record end position x2 = I; y2 = j;} else if (map [I] [j] = '*') {// count the number of walls wall ++ }}} map [x1] [y1] = '*'; // set the prize start point to 'X' bool result = dfs (x1, y1, 0); // traverse from the start if (result = true) {// If resultweighttrue indicates that there is such a path printf ("happy end \ n");} else {printf ("bad end \ n "); // otherwise it indicates that there is no such path} return 0 ;}







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.