Hdoj 1010 tempter of the bone [DFS] + [parity pruning]

Source: Internet
Author: User

Starting from S to stopping D, and arriving at t at the specified time (not early or late ). Note that each '.' can only go once.

Analysis: DFS, but common DFS and TL are used, so pruning is required. We can think that if we can arrive at, but there are other points around the D point that we can step on, and we can observe that, from a point (not a D point) if the distance from point D (ABS (X-dx) + ABS (Y-dy) is an odd number, the odd number step is required, and the even number is the even number step ,()

Code:

# Include <stdio. h> # include <string. h> # include <stdlib. h >#include <iostream> using namespace STD; # define M 10 const int DX [4] = {0, 0,-1, 1 }; const int dy [4] = {1,-1, 0, 0}; char map [m] [m]; int n, m, T; int Sx, Sy, ex, ey; int limit (int x, int y) {return (Map [x] [Y]! = 'X' & x> = 0 & x <n & Y> = 0 & Y <m);} int DFS (int x, int y, int step) {If (x = ex & Y = ey & step = T) return 1; int NX, NY; int temp = T-step-ABS (X-Ex)-ABS (Y-ey); // T-step is the rest to go, ABS (..) Is the distance from the D point. The subtraction must be an even number. If (temp <0 | temp & 1) return 0; int I; for (I = 0; I <4; I ++) {Nx = x + dx [I]; ny = Y + dy [I]; If (limit (NX, NY )) {map [NX] [NY] = 'X'; If (DFS (NX, NY, step + 1) return 1; Map [NX] [NY] = '. ';}} return 0 ;}int main () {int I, j; while (scanf ("% d", & N, & M, & T), N | M | T) {memset (MAP, 0, sizeof (MAP); for (I = 0; I <n; I ++) {scanf ("% s", map [I]) ;}for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {If (Map [I] [J] ='S ') {SX = I; Sy = J;} If (Map [I] [J] = 'D') {EX = I; ey = J ;}} map [SX] [sy] = 'X'; // be sure to initialize int ans = DFS (sx, Sy, 0 ); printf ("% s \ n", ANS? "Yes": "no");} return 0 ;}

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1010

Hdoj 1010 tempter of the bone [DFS] + [parity pruning]

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.