Hdu2717 & hdu1548-retrain BFS

Source: Internet
Author: User

Hdu2717

In the question, N belongs to [1-100000] and cannot go out of 100000. At the beginning, n can always be written in half. This error is reported twice.

In addition, the question is to find the minimum number of steps, or BFs. This question is purely an exercise.

Question:

Start Point and end point. You can take one step forward and one step backward. <1. Calculate the minimum number of steps.

Analysis:

BFS.

Code:

# Include <stdio. h> # include <queue> # include <string. h> bool N [100002]; // indicates whether int min, K; struct info {int A; int C ;}; STD: queue <info> q; bool operator <(Info X, info y) {return X. c> Y. c;} void BFS (Info X) {info Y, Z, W; q. push (x); While (! Q. empty () {x = Q. front (); q. pop (); N [X. a] = 1; if (X. A = k) {min = x. c; break;} y. A = 2 * X. a; Y. C = x. c + 1; Z. A = x. A + 1; Z. C = x. c + 1; W. A = x. a-1; W. C = x. c + 1; if (Y. a> 0 & Y. A <= 100000 & N [Y. a] = 0) Q. push (y); If (Z. a> 0 & Z. A <= 100000 & N [Z. a] = 0) Q. push (z); If (W. a> 0 & W. A <= 100000 & N [W. a] = 0) Q. push (w) ;}while (! Q. Empty () {q. Pop () ;}} int main () {int N; while (~ Scanf ("% d", & N, & K) {memset (n, 0, sizeof (n); N [N] = 1; min = k-N; Info X; X. A = N; X. C = 0; If (n> = k) min = n-k; else BFS (x); printf ("% d \ n", min);} return 0 ;}

Hdu1548

Still BFs. When it cannot be reached, output-1;

You can use priority queue optimization.

#include<stdio.h>#include<queue>#include<string.h>bool N[202];int num[202];int e,n;struct info{int a;int c;};std::priority_queue<info> Q;bool operator<(info x,info y){return x.c>y.c;}int BFS(info x){info y,z,w;Q.push(x);while(!Q.empty()){x=Q.top();Q.pop();N[x.a]=1;if(x.a==e) {while(!Q.empty())Q.pop();return x.c;}y.a=x.a+num[x.a];y.c=x.c+1;z.a=x.a-num[x.a];z.c=x.c+1;if(y.a>0&&y.a<=n&&N[y.a]==0)  Q.push(y);if(z.a>0&&z.a<=n&&N[z.a]==0)  Q.push(z);}return -1;}int main() {int b,i;while(scanf("%d",&n)&&n!=0){scanf("%d%d",&b,&e);memset(N,0,sizeof(N));for(i=0;i<n;i++)scanf("%d",&num[i+1]);info x;x.a=b;x.c=0;printf("%d\n",BFS(x));}return 0;}

 

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.