hdoj1242 (Dfs pruning solution)

Source: Internet
Author: User

Test instructions: Rescue operations, Angel R has a number of friends a(friends, here is a few times, not see test instructions), Angels are locked in a cell, waiting for friends to save, to save the shortest distance angel.

Solution: If you do not prune, the map of 200*200 will time out, you can use the angel as the starting point for DFS, record the minimum value to reach map[x][y], to reach the minimum of each a.

#include <iostream>#include<cstring>#include<cstdio>using namespacestd;Const intM =205;CharMap[m][m];intMinl[m][m]; The record reaches the minimum of x, Y, and if it reaches the minimum value again, then there is no need to go downintMinltor;intVisited[m][m];intn,m;//int step;intdire[4][2] = {{-1,0},{0,-1},{1,0},{0,1}};voidDfsintV1,intV2,intStep) {    if(Map[v1][v2] = ='a')    {        if(Step <minltor) {Minltor=step; Record the minimum value to reach a        }        Else return; }     for(inti =0; I <4; i++)    {        intx = v1+dire[i][0]; inty = v2+dire[i][1]; if(!visited[x][y] && map[x][y]!='#'&& x < n && x >=0&& y < m && y >=0)        {            if(step+1>Minl[x][y])Continue; There's no need to go any further.ElseMinl[x][y] = step+1;//update minimum value visited[x][y]=1; if(Map[x][y] = ='x') DFS (X,y,step+2); ElseDFS (x,y,step+1); Visited[x][y]=0; }    }}intMain () { while(Cin >> N >>m) {intStep =0; Minltor=1000000; intx, y;  for(inti =0; I < n; i++)        {             for(intj =0; J < M; J + +) {cin>>Map[i][j]; VISITED[I][J]=0; MINL[I][J]=1000000; if(Map[i][j] = ='R') {x=i; Y=J;        }}} dfs (X,y,step); if(Minltor = =1000000) cout<<"Poor ANGEL have to stay in the prison all his life.\n"; Elsecout << Minltor <<Endl; }    return 0;}

Blog original, reproduced please indicate the source.

hdoj1242 (Dfs pruning solution)

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.