# Sdut 1125-New Game (DFS)

Source: Internet
Author: User
New game Time Limit: 1000 ms memory limit: 65536 k any questions? Click Here ^_^ Topic description new game is a new game that is played on an M * m Special board (number I is marked on line I of the Board. Given a number N, the player is required to move a piece from the upper left corner () to the lower right corner (M, M). When moving, the player can only move to the right or down. The number after moving is required to be n, and the number of turns is the least.

If the given n is correct, the contestant cannot find the mobile solution so that the number and the number of turns are N or the number of turns is not the least, and the contestant loses. Therefore, contestants must do everything they can to find a path that meets the conditions !! Enter two positive integers m, n (where M <= 16) to ensure data is resolved. The minimum number of turns in the output. Sample Input
`4 22`
Sample output
`1`
`Search .. Basic pruning.`
`# Include <iostream> # include <cstdio> # include <algorithm> # include <queue> # include <cctype> # include <cstring> using namespace STD; int ma [18] [18], M, N, ans; bool vis [18] [18]; const int INF = 0x3f3f3f3f; int dir [2] [2] = {1, 0}, {0, 1}; void DFS (int x, int y, bool statu, int num, int sum) {// optimal shear if (Num> ans) return; // feasible shear if (sum> N) return; If (x = M & Y = m) {If (sum = N) ans = min (ANS, num); Return ;}for (INT I = 0; I <2; I ++) {int Tx = x + dir [I] [0]; int ty = Y + dir [I] [1]; if (TX> = 1 & TX <= M & ty> = 1 & ty <= M &&! Vis [TX] [ty]) {vis [TX] [ty] = 1; if (sum = 1) DFS (TX, Ty, I, num, sum + ma [TX] [ty]); else {if (I! = Statu) DFS (TX, Ty, I, num + 1, sum + ma [TX] [ty]); elsedfs (TX, Ty, I, num, sum + ma [TX] [ty]);} vis [TX] [ty] = 0 ;}} void Init () {for (INT I = 1; I <= m; I ++) for (Int J = 1; j <= m; j ++) Ma [I] [J] = I;} int main () {While (scanf ("% d", & M, & N )! = EOF) {memset (VIS, 0, sizeof (VIS); Init (); vis [1] [1] = 1; ans = inf; DFS, 1); printf ("% d \ n", ANS);} return 0 ;}`

Sdut 1125-New Game (DFS)

Related Keywords:

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.