Hdu/hdoj 1548 A Strange Lift Bfs,dfs

Source: Internet
Author: User

Topic Link:

http://acm.hdu.edu.cn/showproblem.php?pid=1548

I use two methods to do, Dfs no AC, do not know where the wrong, posted out of the great God has been free to enlighten, the second classic method BFs, 76MS 360k, the idea is very simple, is a very normative breadth first search.

Code:

BFS:

#include <iostream>
#include <queue>
using namespace std;
int n,a,b;
BOOL visit[210];
int ki[210];
struct node{
	int x,step;
} P,q;
Queue<node> Q;
int BFs () {while
	! Q.empty ()) Q.pop ();
	P.x=a;
	P.step=0;
	Q.push (p);
	Visit[p.x]=1;
	if (p.x==b) return 0;
	while (! Q.empty ())
	{
		p=q.front ();
		Q.pop ();
		if (p.x==b) return p.step;
		for (int i=0;i<2;i++)
		{
			if (i==0) {
				q.x=p.x+ki[p.x];
				if (q.x>n) continue;
				if (visit[q.x]) continue;
				q.step=p.step+1;
				Visit[q.x]=1;
				Q.push (q);
			}
			if (i==1) {
				q.x=p.x-ki[p.x];
				if (q.x<1) continue;
				if (visit[q.x]) continue;
				q.step=p.step+1;
				Visit[q.x]=1;
				Q.push (q);
	}} return-1;
	
}
int main ()
{while
	(cin>>n)
	{
		if (n==0) break;
		cin>>a>>b;
		for (int i=1;i<=n;i++)
			cin>>ki[i];
		memset (visit,0,sizeof (visit));	
		COUT<<BFS () <<endl;	
	}
	return 0;
}

Dfs:

#include <iostream> #include <string> #include <cstdio> #include <cmath> #include <vector > #include <algorithm> #include <sstream> #include <cstdlib> #include <fstream> #include <
Queue> using namespace std;
int N,a,b,cnt,flag;
int cmin;
BOOL visit[210];
int ki[210];
		void Dfs (int x) {if (x==b) {flag=1;
		if (cnt<cmin) cmin=cnt;
	return;
	
	} if (flag) return;
			if (x+ki[x]<=n) {if (!visit[x+ki[x]]) {cnt++;
			Visit[x+ki[x]]=1;
			DFS (x+ki[x]);
			visit[x+ki[x]]=0;
		cnt--;
			} if (x-ki[x]>=1) {if (!visit[x-ki[x]]) {cnt++;
			
			Visit[x-ki[x]]=1;
			DFS (x-ki[x]);
			visit[x-ki[x]]=0;
		cnt--;
	int main () {//ifstream fin;
	
	Fin.open ("Data1.txt");
		while (cin>>n) {if (n==0) break;
		cin>>a>>b;
		for (int i=1;i<=n;i++) cin>>ki[i];
		cnt=0;
		cmin=999999;
		flag=0;
		memset (visit,0,sizeof (visit));
		Visit[a]=1;
		DFS (a);	
		if (flag) cout<<cmin<<endl;else cout<<-1<<endl;

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.