1.BFS宽度优先搜索
- 输入n,找到n的整数倍是1,0的组合。如2的五倍是1,3的37倍为111.
分析为在初始位置时为1,终止位置时应该为current%n==0,判断是否为n的整数倍应该由于只有0和1的数字组合,所以我们可以利用current当前的*10+0,以及*10+1来继续执行查找。
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
void BFS(int n)
{
queue<long long> myQueue;
myQueue.push(1);
while(!myQueue.empty())
{
long long current = myQueue.front();
myQueue.pop();
if(current%n==0) // 目标状态
{
printf("%lld\n",current);
return ;
}
myQueue.push(current*10);
myQueue.push(current*10+1);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
BFS(n);
}
return 0;
}
3的过程应该就是(右为队首)
11 10;
101 100 11;
111 110 101 100;
1000 1001 111 110 101;
2.DFS深度优先搜索
- 骑士巡游
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
int direction[8][2]={ {-1,-2}, {1,-2}, {-2,-1}, {2,-1}, {-2,1}, {2,1}, {-1,2}, {1,2} };
const int MAXN=30;
bool visit[MAXN][MAXN];
bool DFS(int x,int y,int step,string answer,int p,int q)
{
if(step==p*q)
{
cout<<answer<<endl<<endl;
return true;
}
for(int i=0;i<8;++i)
{
int nx=x+direction[i][0];
int ny=y+direction[i][1];
if(nx<0||nx>=p||ny<0||ny>=q||visit[nx][ny])
{
continue;
}
visit[nx][ny]=true;
char col= ny+'A';
char row= nx+'1';
if(DFS(nx,ny,step+1,answer+col+row,p,q)) //此时判断的下一步是否能走
{
return true;
}
visit[nx][ny]=false; //此时不能走,所以我们还没有访问过nxny。
}
return false;
}
int main()
{
int n;
scanf("%d",&n);
int caseNumber=0;
while(n--)
{
int p,q;
scanf("%d%d",&p,&q);
memset(visit,false,sizeof(visit));
cout<<"Scenario #"<<++caseNumber<<":"<<endl;
visit[0][0]=true;
if(DFS(0,0,1,"A1",p,q))
continue;
else
cout<<"impossible"<<endl<<endl;
}
return 0;
}
- 木棍组正方形
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN= 25;
int sticks[MAXN];
bool visit[MAXN];
bool DFS(int sum,int num,int side,int m)
{
if(num==4) // 则说明已经找到了
{
return true;
}
for(int i=0;i<m;i++)
{
if(visit[i]||sum+sticks[i]>side)
{
continue;
}
visit[i]=true;
if(sum+sticks[i]==side)
{
if(DFS(0,num+1,side,m))
return true;
}
else
{
if(DFS(sum+sticks[i],num,side,m))
return true;
}
visit[i]=false;
}
return false;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int m;
scanf("%d",&m);
int length=0;
for(int i=0;i<m;i++)
{
scanf("%d",&sticks[i]);
length+=sticks[i];
}
int side=length/4;
memset(visit,false,sizeof(visit));
if(DFS(0,0,side,m))
printf("yes\n");
else
printf("no\n");
}
return 0;
}
但这样可能找到之后资源以及耗尽,所以最好剪枝。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN= 25;
int sticks[MAXN];
bool visit[MAXN];
bool DFS(int sum,int num,int position,int side,int m)
{
if(num==3) // 则说明已经找到了
{
return true;
}
int sample = 0;
for(int i=position;i<m;i++)
{
if(visit[i]||sum+sticks[i]>side||sticks[i]==sample) //sample记录错误的棍子如果错了就直接跳过这个棍子
{
continue;
}
visit[i]=true;
if(sum+sticks[i]==side)
{
if(DFS(0,num+1,0,side,m))
return true;
else
sample=sticks[i];
}
else
{
if(DFS(sum+sticks[i],num,i+1,side,m))
return true;
else
sample=sticks[i];
}
visit[i]=false;
}
return false;
}
bool compare(int x,int y)
{
return x>y;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int m;
scanf("%d",&m);
int length=0;
for(int i=0;i<m;i++)
{
scanf("%d",&sticks[i]);
length+=sticks[i];
}
if(length%4!=0) // 边长不为整数肯定不行
{
printf("no\n");
continue;
}
int side=length/4;
memset(visit,false,sizeof(visit));
sort(sticks,sticks+m,compare);
if(sticks[0]>side) // 最大的都大于了边长肯定不行
{
printf("no\n");
continue;
}
if(DFS(0,0,0,side,m))
printf("yes\n");
else
printf("no\n");
}
return 0;
}
queue的应用
- 优先队列
#include<iostream>
#include<queue>
#include<cstdio>
#include<string>
using namespace std;
struct Complex
{
int real;
int imag;
Complex(int a,int b):real(a),imag(b){}
bool operator < (Complex c) const
{
if(real*real+imag*imag== c.real * c.real + c.imag * c.imag)
{
return imag>c.imag;
}
else
{
return real*real+imag*imag < c.real * c.real + c.imag * c.imag;
}
}
};
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
priority_queue<Complex> myPriorityQueue;
while(n--)
{
string str;
cin>>str;
if(str=="POP")
{
if(myPriorityQueue.empty())
{
cout<<"empty"<<endl;
}
else
{
Complex current =myPriorityQueue.top();
myPriorityQueue.pop();
printf("%d+i%d\n",current.real,current.imag);
printf("SIZE=%d\n",myPriorityQueue.size());
}
}
else
{
int a,b;
scanf("%d+i%d",&a,&b);
myPriorityQueue.push(Complex(a,b));
printf("SIZE=%d\n",myPriorityQueue.size());
}
}
}
return 0;
}
- 哈弗曼树,带权路径长度的计算
哈弗曼树再书上是那种,利用求每个叶子节点的权值*高度 的和来求出带权路径长度。同样我们可以用叶子节点的双亲所求的权值(我不知道是不是这样说啊。。),然后利用那几个点的权值求和也可以得到同样的值。
结果均为37.
#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
priority_queue<int,vector<int>,greater<int> > MyPriorityQueue;
while(n--)
{
int x;
scanf("%d",&x);
MyPriorityQueue.push(x);
}
int answer=0;
while(MyPriorityQueue.size()>1)
{
int a=MyPriorityQueue.top();
MyPriorityQueue.pop();
int b=MyPriorityQueue.top();
MyPriorityQueue.pop();
answer+=a+b;
MyPriorityQueue.push(a+b);
}
printf("%d",answer);
}
return 0;
}
散列表应用
map:红黑树的查询,插入,删除,logn的复杂度。
unordered_map:hash table,查询,插入,删除,log1的复杂度
- map的查找,删除,插入
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
map<string,int>myMap;
int main()
{
myMap["Emma"]=67;
myMap["Ben"]=100;
myMap.insert(pair<string,int>("Bob",72));
myMap.insert(pair<string,int>("May",88));
cout<<"the score of Ben"<<myMap["Ben"]<<endl;
cout<<"the score of May"<<myMap.at("May")<<endl;
myMap.erase("Ben");
map<string,int>::iterator it;
for(it=myMap.begin(); it!=myMap.end(); it++)
{
cout<<"the score of "<<it->first<<":"<<it->second<<endl;
}
it=myMap.find("Bob");
if(it!=myMap.end())
{
cout<<"Bob is found"<<endl;
}
else
{
cout<<"Bob is not found"<<endl;
}
return 0;
}
- map查找学生信息
#include<cstdio>
#include<iostream>
#include<map>
#include<string>
using namespace std;
map<string,string> student;
int main()
{
int n;
scanf("%d",&n);
getchar();
while(n--)
{
string str;
getline(cin,str);
int position=str.find(" ");
string key =str.substr(0,position);
student[key]=str;
}
int m;
scanf("%d",&m);
while(m--)
{
string key;
cin>>key;
string answer = student[key];
if(answer==" ")
{
cout<<"no answer"<<endl;
}
else
{
cout<<answer<<endl;
}
}
return 0;
}