Poj 3009 curling 2.0 (DFS)

Source: Internet
Author: User

Curling 2.0
Time limit:1000 ms   Memory limit:65536 K
Total submissions:11879   Accepted:5028

Description

On planet MM-21, after their Olympus IC 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 squares 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)

The 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 direction 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 movements

Under 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 configuration has changed as in Fig. 3 (B ).


Fig. 3: The solution for fig. D-1 and the final board configuration

Input

The 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 the Board

The width and the height of the Board satisfy: 2 <=W<= 20, 1 <=H<= 20.

Each line consistsWDecimal Numbers delimited by a space. The number describes the status of the corresponding square.

0 Vacant Square
1 Block
2 Start position
3 Goal position

The dataset for fig. D-1 is as follows:

6 6
1 0 0 2 1 0
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

Output

For each dataset, print a 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 Input

2 13 26 61 0 0 2 1 01 1 0 0 0 00 0 0 0 0 30 0 0 0 0 01 0 0 0 0 10 1 1 1 1 16 11 1 2 1 1 36 11 0 2 1 1 312 12 0 1 1 1 1 1 1 1 1 1 313 12 0 1 1 1 1 1 1 1 1 1 1 30 0

Sample output

14-1410-1

The minimum number of movements from the starting point to the target point. The starting point is 2, the ending point is 3, the open space is 0, and the obstacle is 1.

You can move in four directions each time, but you cannot move in the direction of the obstacle if you are closely tied to the obstacle.

Each exercise must end with reaching the target point, going out of the world, or encountering obstacles. When encountering an obstacle, it will stop at the point next to the obstacle and the obstacle will disappear.

Idea: when moving in four directions each time, it determines whether it is legal (whether it is close to the obstacle). The motion process is a straight forward. Exercise up to 10 times.

# Include <stdio. h> # include <queue> # include <map> # include <string. h> using namespace STD; # define n 25 const int INF = 0x1f1f1f1f; int G [N] [N]; int ans, n, m; int DX [4] = {, 0,-1}; int dy [4] = {,-}; void work (int x, int y, int T); void DFS (int x, int y, int T, int K) {If (T> 10) return; int Di, DJ; DI = DX [k] + X; dj = Dy [k] + Y; if (di <0 | di> = n | DJ <0 | DJ> = m) // return is not supported; if (G [di] [DJ] = 3) {ans = min (ANS, t ); Return;} else if (G [di] [DJ] = 0 | G [di] [DJ] = 2) DFS (Di, DJ, T, k ); else if (G [di] [DJ] = 1) {G [di] [DJ] = 0; Work (X, Y, t + 1 ); G [di] [DJ] = 1 ;}} void work (int x, int y, int t) {for (int K = 0; k <4; k ++) {If (G [x + dx [k] [Y + dy [k]! = 1) DFS (X, Y, T, k) ;}} int main () {int I, j, Si, SJ; while (scanf ("% d", & M, & N), N | M) {for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {scanf ("% d", & G [I] [J]); if (G [I] [J] = 2) {Si = I; SJ = J ;}}ans = inf; Work (Si, SJ, 1 ); if (ANS = inf) ans =-1; printf ("% d \ n", ANS);} return 0 ;}


Poj 3009 curling 2.0 (DFS)

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.