DFS
迷宫
//迷宫 bfs
#include<iostream>
#include<queue>
using namespace std;
const int N=100;
int a[N][N];
int di[4][2]={0,1,1,0,-1,0,0,-1};
typedef struct nod{
int x,y,step;
}node;
void bfs(){
int n=6;
queue <node> q;
q.push({1,1,0});
while(!q.empty()){
node n1=q.front();
q.pop();
if(n1.x==n&&n1.y==n){
cout<<n1.step;
return;
}
for(int i=0;i<4;i++){
int newx=n1.x+di[i][0];
int newy=n1.y+di[i][1];
if(newx>0&&newx<n+1&&newy>0&&newy<n+1){
q.push({newx,newy,n1.step+1});
}
}
}
}
int main(){
bfs();
return 0;
}
n皇后
//n皇后 dfs
#include<iostream>
using namespace std;
int n;
//
int col[100];
int v[100];//需要加上v[i]
int ans=0;
void check(){
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if((col[i]+i==col[j]+j)||(col[i]-i==col[j]-j))
return;
}
}
ans++;
}
void dfs(int x){
if(x>n){
check();
}
for(int i=1;i<=n;i++){
if(v[i]==0){
v[i]=1;
col[i]=x;
dfs(x+1);
v[i]=0;
}
}
}
int main(){
cin>>n;
dfs(1);
cout<<ans;
return 0;
}
二叉树
#include<iostream>
#include<stack>
using namespace std;
typedef struct treenode{
char data;
struct treenode* lchild;
struct treenode* rchild;
}treenode,*tree;
void lastorderbystack(tree root)
{
stack <tree> s;
tree visited=NULL;
tree pmove=root;
while(pmove!=NULL)
{
s.push(pmove);
pmove=pmove->lchild;
}
while(!s.empty())
{
pmove=s.top();//取栈顶元素
s.pop();//出栈
if(pmove->rchild==NULL||pmove->rchild==visited)
{
cout<<pmove->data;
visited=pmove;
}
else
{
s.push(pmove);//先入栈,因为之前把pmove出栈了
pmove=pmove->rchild;
while(pmove!=NULL)
{
s.push(pmove);
pmove=pmove->lchild;
}
}
}
}
tree createtree()
{
char a;
tree T;
cin>>a;
if(a=='#') T=NULL;
else{
T=new treenode;//之前虽然创建了tree的指针T,但是并没有对其初始化
//我们动态分配了空间给treenode类型,并且返回内存的指针给new
T->data=a;
T->lchild=createtree();
T->rchild=createtree();
}
return T;
}
void preorder(tree root)
{
if(root==NULL) return;
cout<<root->data;
preorder(root->lchild);
preorder(root->rchild);
}
void inorderbystack(tree root)
{
stack <tree> s;
tree pmove=root;//移动指针
while(pmove!=NULL||!s.empty())
{
while(pmove)
{
s.push(pmove);
pmove=pmove->lchild;
}
while(!s.empty())
{
pmove=s.top();
s.pop();
cout<<pmove->data;
pmove=pmove->rchild;
}
}
}
int main()
{
tree T;
T=createtree();
// preorder(T);
inorderbystack(T);
// lastorderbystack(T);
}
P3371
输出一行 nn 个空格分隔的非负整数,表示 ss 到每个点的距离。
输入输出样例
输入
4 6 1
1 2 2
2 3 2
2 4 1
1 3 5
3 4 3
1 4 4
输出
0 2 4 3
#include <iostream>
#include<queue>
#include<stack>
#include<stdio.h>
#define Inf 99999999999
using namespace std;
int map[10010][10010];
int dis[10010];
int book[10010];
int n,m,s;
void dijkstra()
{
int u,min;
for(int i=1;i<=n;i++)
dis[i]=map[s][i];
for(int i=1;i<=n;i++)
{
min=Inf;
for(int j=1;j<=n;j++)
{
if(book[j]==0&&dis[j]<min)//确定该点没有被访问过
{
min=dis[j];
u=j;
}
}
book[u]=1;
for(int j=1;j<=n;j++)
{
if(map[u][j]<Inf)
{
if(dis[j]>map[u][j]+dis[u])
dis[j]=map[u][j]+dis[u];
}
}
}
}
int main()
{
int x,y;
cin>>n>>m>>s;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
map[i][j]=Inf;
for(int i=1;i<=n;i++)
map[i][i]=0;
for(int i=1;i<=m;i++)
{
cin>>x>>y;
cin>>map[x][y];
}
dijkstra();
for(int i=1;i<=n;i++)
cout<<dis[i]<<' ';
return 0;
}
floyd
#include<iostream>
using namespace std;
const int N=1e2+3;
int e[N][N];
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y,v;
cin>>x>>y>>v;
e[x][y]=v;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i!=j&&e[i][j]==0)e[i][j]=N;
}
}
//依次加入k点后
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(e[i][j]>e[i][k]+e[k][j])
e[i][j]=e[i][k]+e[k][j];
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<e[i][j]<<' ';
}
cout<<endl;
}
return 0;
}