poj3009 Curling 2.0 (good title DFS)

Source: Internet
Author: User

https://vjudge.net/problem/POJ-3009

Finish this question, feel oneself to the DFS understanding should be profound again.

1. In general, the minimum number of steps are used BFS, but this problem because the status record is very troublesome, so you can use DFS.

2. In the use of DFS, MP when a global variable, for the equality of the way, every way after the end of the state must be restored!!! (i.e. code 36-38 lines) Otherwise it will have an effect on other methods.

1#include <iostream>2#include <cstdio>3#include <queue>4#include <cstring>5#include <algorithm>6#include <cmath>7#include <stack>8 #defineLson L, M, rt<<19 #defineRson m+1, R, rt<<1|1Ten #defineIO Ios::sync_with_stdio (false); Cin.tie (0); One #defineINF 0x3f3f3f3f Atypedef unsignedLong Longll; - using namespacestd; - intN, M, mp[ -][ -], si, SJ, GI, GJ, CNT, mini; the intdir[4][2] = {0,1,0, -1,1,0, -1,0}; - voidDfsintXintYintAintBintKintc) - { -     if(k >Ten|| K > Mini)return ; +     if(x = = Gi&&y = =GJ) { -Mini =min (mini, k); +         return ; A     } at     if(A! =0|| B! =0){//not just the beginning . -         if(x<0|| x>=n| | y<0|| y>=m) { -             return;//failed -         } -         Else if(Mp[x][y] = =1){ -             if(c <=1)return ; inc =0; -Mp[x][y] =0;//the resistance is eliminated. toX-=A; +Y-= b;//go back to the previous state -              for(inti =0; I <4; i++){ theDFS (x+dir[i][0], y+dir[i][1], dir[i][0], dir[i][1], K +1, c+1); *             } $X + =A;Panax NotoginsengY + =b; -Mp[x][y] =1;//remember to State recovery!!  the         } +         Else{ ADFS (X+a, y+b, A, B, K, c+1); the         } +     } -     Else{  $          for(inti =0; I <4; i++){ $DFS (x+dir[i][0], y+dir[i][1], dir[i][0], dir[i][1], K +1, c+1); -         }  -     } the } - intMain ()Wuyi { the      while(Cin >> M >>N, N, m) { -Mini =INF; WuCnt=0; -          for(inti =0; I < n; i++){ About              for(intj =0; J < M; J + +){ $CIN >>Mp[i][j]; -                 if(Mp[i][j] = =2) si=i,sj=J; -                 if(Mp[i][j] = =3) gi=i,gj=J; -             } A         } +DFS (SI, SJ,0,0,0,0); the         if(mini = = INF) cout <<"-1"<<Endl; -         Elsecout << Mini <<Endl; $     } the     return 0; the}

poj3009 Curling 2.0 (good title 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.