bfs(stl的選取)

轉載自:http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/10/2631191.html

vector : vector和built-in數組類似,擁有一段連續的內存空間,能非常好的支持隨即存取,即[]操作符,但由於它的內存空間是連續的,所以在中間進行插入和刪除會造成內存塊的拷貝,另外,當插入較多的元素後,預留內存空間可能不夠,需要重新申請一塊足夠大的內存並把原來的數據拷貝到新的內存空間。這些影響了vector的效率,但是實際上用的最多的還是vector容器,建議大多數時候使用vector效率一般是不錯的。

list:      list就是數據結構中的雙向鏈表(根據sgi stl源代碼),因此它的內存空間是不連續的,通過指針來進行數據的訪問,這個特點使得它的隨即存取變的非常沒有效率,因此它沒有提供[]操作符的重載。但由於鏈表的特點,它可以以很好的效率支持任意地方的刪除和插入。

deque: deque是一個double-ended queue,它的具體實現不太清楚,但知道它具有以下兩個特點:它支持[]操作符,也就是支持隨即存取,並且和vector的效率相差無幾,它支持在兩端的操作:push_back,push_front,pop_back,pop_front等,並且在兩端操作上與list的效率也差不多。

題目1:https://vjudge.net/problem/SPOJ-KATHTHI

題目2:http://codeforces.com/problemset/problem/1064/D

code1:

/*
https://vjudge.net/problem/SPOJ-KATHTHI#author=0
*/
#include<bits/stdc++.h>

using namespace std;

struct node{
	int x,y,s;
	node(int X,int Y,int S):x(X),y(Y),s(S){
	}
/*	bool operator < (const node &t) const{
		return s<t.s;
	}
*/
};

const int maxn=1005;

char ma[maxn][maxn];
int vd[maxn][maxn];
deque<node>q;

int main(){
//	ios::sync_with_stdio(false);
	int i,j;
	int t,r,c,x,y;
	cin>>t;
	while(t--){
		memset(vd,0,sizeof vd);
		cin>>r>>c;
		for(i=1;i<=r;i++) for(j=1;j<=c;j++) cin>>ma[i][j];
		
		int ans=1e6;
		q.push_back(node(1,1,0));
		while(!q.empty()){
			node t=q.front();
			q.pop_front();
			x=t.x;y=t.y;
			int s=t.s;
			if(x==r&&y==c) ans=min(s,ans);
			if(vd[x][y]) continue;
			vd[x][y]=1;
			
			if(x>1&&vd[x-1][y]==0){
				if(ma[x-1][y]!=ma[x][y]) q.push_back(node(x-1,y,s+1));
				else q.push_front(node(x-1,y,s));
			}
			if(x<r&&vd[x+1][y]==0){
				if(ma[x+1][y]!=ma[x][y]) q.push_back(node(x+1,y,s+1));
				else q.push_front(node(x+1,y,s));
			}
			if(y>1&&vd[x][y-1]==0){
				if(ma[x][y-1]!=ma[x][y]) q.push_back(node(x,y-1,s+1));
				else q.push_front(node(x,y-1,s));
			}
			if(y<c&&vd[x][y+1]==0){
				if(ma[x][y+1]!=ma[x][y]) q.push_back(node(x,y+1,s+1));
				else q.push_front(node(x,y+1,s));
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
/*
4
2 2
aa
aa
2 3
abc
def
6 6
akaccc
aaacfc
amdfcc
aokhdd
zyxwdp
zyxwdd
5 5
abbbc
abacc
aaacc
aefci
cdgdd
*/

code2:

#include<bits/stdc++.h>

using namespace std;

struct node{
	int r,c,x,y;
	node(int nr,int nc,int nx,int ny):r(nr),c(nc),x(nx),y(ny){
	}
};

const int maxn=2005;

char a[maxn][maxn];
int vised[maxn][maxn];
list<node>l;

int main(){
	int n,m,r,c,x,y;
	int i,j;
	cin>>n>>m>>r>>c>>x>>y;
	memset(a,'*',sizeof a);
	for(i=1;i<=n;i++) for(j=1;j<=m;++j) cin>>a[i][j];
	
	int ans=0;
	l.push_back(node(r,c,x,y));
	while(!l.empty()){
		node t=l.front();
		l.pop_front();
		if(vised[t.r][t.c]) continue;
		ans++;
		vised[t.r][t.c]=1;
		
//		cout<<t.r<<' '<<t.c<<' '<<t.x<<' '<<t.y<<endl;
		
		if(t.r>1&&a[t.r-1][t.c]=='.'){
			l.push_front(node(t.r-1 ,t.c ,t.x ,t.y));
		}
		if(t.r<n&&a[t.r+1][t.c]=='.'){
			l.push_front(node(t.r+1 ,t.c ,t.x ,t.y));
		}
		if(t.c>1&&t.x&&a[t.r][t.c-1]=='.'){
			l.push_back(node(t.r ,t.c-1 ,t.x-1,t.y));
		}
		if(t.c<m&&t.y&&a[t.r][t.c+1]=='.'){
			l.push_back(node(t.r ,t.c+1 ,t.x,t.y-1));
		}
	}
	cout<<ans<<endl;
	return 0;
}

這兩個題對路徑的選取帶有貪心的思維,第一題的話用list會超時,具體原因不太明確大致估計和list在內存中的存儲是亂序的有關。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章