poj3009 Curling 2.0 Dfs water

Source: Internet
Author: User

http://poj.org/problem?id=3009

Test instructions: Given a m*n grid, where there are obstructions on these meshes, given the location of the starting point and end point, when the stone starts from the starting point, hitting the barrier will turn, otherwise it will continue to move along the direction. The obstacle disappears after hitting the barrier and stopping at the position before the obstacle. The stone can then move forward in four directions (adjacent to the barrier-free direction), asking at least how many times to stop to reach the end point from the start. The game fails after 10 steps cannot be reached or extra. If you can reach the minimum number of steps, the output is 1.

Idea: DFS, extra 10 steps for pruning conditions.

1#include <iostream>2#include <cstdio>3#include <cstdio>4#include <cstring>5#include <queue>6 using namespacestd;7 intdx[4]={-1,0,1,0};8 intdy[4]={0,1,0,-1};9 intEx,ey,n,m,ans;Ten intmap[ A][ A]; One intCheckintXinty) A { -     if(x<0|| x>=m| | y<0|| y>=N) -     return 0; the     return 1; - } - voidDfsintXintYintc) - { +     if(c>Ten) -     return; +      for(intI=0;i<4; i++) A     { at         intnx=x+Dx[i]; -         intny=y+Dy[i]; -         if(map[nx][ny]==1)//Can't get through or walk through when you turn -         Continue; -          while(!map[nx][ny])//It 's a viable way to go until it's 0. -         { innx=nx+Dx[i]; -ny=ny+Dy[i]; to         } +         if(check (Nx,ny))//If you don't run out of bounds -         { the             if(map[nx][ny]==1) *             { $map[nx][ny]=0;//Crushing ObstaclesPanax NotoginsengDFS (nx-dx[i],ny-dy[i],c+1);//step back and continue searching -map[nx][ny]=1;//Backtrack to restore the original scene, the key point the         } +         if(map[nx][ny]==3) A         if(c<ans) theans=C; +     } -     } $ } $ Main () - { -     inti,j,x,y; the      while(~SCANF ("%d%d", &n,&m) && (n!=0|| m!=0)) -     {Wuyi          for(i=0; i<m;i++) the         { -              for(j=0; j<n;j++) Wu             { -scanf"%d",&map[i][j]); About                 if(map[i][j]==2){ $map[i][j]=0; -x=i;y=J; -                 } -                 Else if(map[i][j]==3) A                 { +ex=i;ex=J; the                 } -             } $         } theans=99999; theDFS (x, Y,1); the         if(ans>Ten) theprintf"-1\n"); -         Else inprintf"%d\n", ans); the     } the}

poj3009 Curling 2.0 Dfs water

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.