Poj3009 curling2.0

Source: Internet
Author: User

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 consists of w decimal 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 1
3 2
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
6 1
1 1 2 1 3
6 1
1 0 2 1 1 3
12 1
2 0 1 1 1 1 1 1 1 1 1 1 3
13 1
2 0 1 1 1 1 1 1 1 1 1 1 1 3
0 0 Sample Output

1
4
-1
4
10
-1 simple deep search, because there will be changes in the search process, you can only use deep search for all values! Obtain the minimum value

# Include <stdio. h> # include <iostream> # include <string. h> using namespace std; int map [25] [25], dir [4] [2] = {0,-1 }, {-1, 0 }}, n, m, re; bool dfs (int sx, int sy, int ex, int ey, int step) {// printf ("% d \ n", sx, sy, ex, ey); int I, flag [5], x, y, xx, yy, k; if (step> 10) return false; if (sx = ex) & (sy = ey) {if (step <re) re = step; return true;} for (I = 0; I <4; I ++) flag [I] = 0; for (I = 0; I <4; I ++) // Four Directions {x = sx + dir [I] [0]; y = sy + di R [I] [1]; if (x = ex) & (y = ey) {if (step <re) re = step; return true ;} if (x> = 0) & (x <n) & (y> = 0) & (y <m) & (map [x] [y]! = 1) {bool zhe = true; while (1) {x = x + dir [I] [0]; y = y + dir [I] [1]; if (x <0) | (x> = n) | (y <0) | (y> = m) {zhe = false; break ;} if (x = ex) & (y = ey) {if (step <re) re = step; return true ;} if (map [x] [y] = 1) {x = x-dir [I] [0]; y = y-dir [I] [1]; break ;}} if (zhe) {xx = x + dir [I] [0]; yy = y + dir [I] [1]; if (map [xx] [yy] = 1) map [xx] [yy] = 0; // note that only hit ones will disappear, the process will not disappear dfs (x, y, ex, ey, step + 1); map [xx] [yy] = 1 ;}} return false ;}int main () {while (scanf ("% d", & m, & n )! = EOF) {if (m = 0 & n = 0) break; int I, j, startx, starty, endx, endy; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) {scanf ("% d", & map [I] [j]); if (map [I] [j] = 2) {startx = I; starty = j; map [I] [j] = 0 ;} if (map [I] [j] = 3) {map [I] [j] = 0; endx = I; endy = j ;}} re = 20; dfs (startx, starty, endx, endy, 0); if (re <10) printf ("% d \ n", re + 1 ); else printf ("-1 \ n");} 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.