转载自: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在内存中的存储是乱序的有关。