Poj 3009 Curling 2.0 DFS

Source: Internet
Author: User

DescriptionOn Planet MM-21, after their Olympus games this year, curling is getting popular. but the rules are somewhat different from ours. the game is played on an ice game board on which a square mesh is marked. they use only a single stone. the purpose of the game is to lead the stone from the start to the goal with the minimum number of moves. fig. 1 shows an example of a game board. some squar Es may be occupied with blocks. there are two special squares namely the start and the goal, which are not occupied with blocks. (These two squares are distinct .) once the stone begins to move, it will proceed until it hits a block. in order to bring the stone to the goal, you may have to stop the stone by hitting it against a block, and throw again. fig. 1: Example of board (S: start, G: goal) Movement of the stone obeys the following rules: At the beginning, the stone stands still at the start square. the movements of the stone are restricted to x and y directions ctions. diagonal moves are prohibited. when the stone stands still, you can make it moving by throwing it. you may throw it to any direction unless it is blocked immediately (Fig. 2 ()). once thrown, the stone keeps moving to the same d Irection until one of the following occurs: The stone hits a block (Fig. 2 (B), (c )). the stone stops at the square next to the block it hit. the block disappears. the stone gets out of the board. the game ends in failure. the stone reaches the goal square. the stone stops there and the game ends in success. you cannot throw the stone more than 10 times in a game. if the stone does not reach the goal in 10 Moves, the game ends in failure. fig. 2: Stone movementsUnder the rules, we wocould like to know whether the stone at the start can reach the goal and, if yes, the minimum number of moves required. with the initial configuration shown in Fig. 1, 4 moves are required to bring the stone from the start to the goal. the route is shown in Fig. 3 (). notice when the stone reaches the goal, the board confi Guration has changed as in Fig. 3 (B ). fig. 3: The solution for Fig. d-1 and the final board configurationInputThe input is a sequence of datasets. the end of the input is indicated by a line containing two zeros separated by a space. the number of datasets never exceeds 100. each dataset is formatted as follows. the width (= w) and the height (= h) of the board First row of the board... h-th row of BoardThe width and the height of the board satisfy: 2 <= w <= 20, 1 <= h <= 20. each line consists of w decimal numbers delimited by a space. the number describes the status of the corresponding square.0 vacant square1 block2 start position3 goal positionThe dataset for Fig. d-1 is as follows: 6 6 1 0 0 0 2 1 1 1 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 1 0 0 0 0 1 0 1 1 1 1 1 OutputFor each dataset, print Line having a decimal integer indicating the minimum number of moves along a route from the start to the goal. if there are no such routes, print-1 instead. each line shoshould not have any character other than this number. sample Input2 13 26 61 0 0 2 1 01 1 0 0 00 0 0 0 0 30 0 0 0 01 0 0 0 0 0 0 10 1 1 1 1 1 16 11 1 1 1 1 36 11 0 2 1 312 12 0 1 1 1 1 1 1 1 1 1 1 313 12 0 1 1 1 1 1 1 1 1 1 30 0 Samp Le Output14-1410-1 idea: DFS goes along one direction until it encounters obstacles, pay attention to some special input.

# Include <iostream> # include <cstdio> # include <fstream> using namespace std; const int maxn = 25; int maze [maxn] [maxn]; int dir [4] [2] = {, 0,-, 0,-}; int ans, N, M, sx, sy; void dfs (int x, int y, int step) {if (step> 10) return; for (int I = 0; I <4; I ++) {int nx = x + dir [I] [0], ny = y + dir [I] [1]; if (nx <0 | nx> = N | ny <0 | ny> = M) continue; if (maze [nx] [ny] = 1) continue; // There is an obstacle that cannot be taken. While (maze [nx] [ny]! = 1) {if (maze [nx] [ny] = 3) // be sure to judge first, if the end point is next to it. {If (ans> step) {ans = step; return ;}} nx + = dir [I] [0]; ny + = dir [I] [1]; if (nx <0 | nx> = N | ny <0 | ny> = M) break ;} if (nx> = 0 & nx <N & ny> = 0 & ny <M) {maze [nx] [ny] = 0; dfs (nx-dir [I] [0], ny-dir [I] [1], step + 1); maze [nx] [ny] = 1 ;}}} int main () {// ifstream fin; // fin. open ("input.txt"); while (cin> M> N) {if (N + M = 0) break; for (int I = 0; I <N; I ++) for (int j = 0; j <M; j ++) {cin> maze [I] [j]; if (maze [I] [j] = 2) {sx = I; sy = j;} ans = 999999; dfs (sx, sy, 1 ); if (ans> 10) cout <-1 <endl; else cout <ans <endl;} return 0 ;}

 


Related Article

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.