做题总是痛苦的
p1135
这一题,典型的bfs题,在我看来这玩意就是暴力
把一个二叉树层次遍历,从上往下从左到右
用队列保存每一层
#include<bits/stdc++.h>
using namespace std;
int start,n;
int destination;
int vis[200]={0};
int k[200];
struct cir
{
int level,steps;
};
void bfs()
{
cir now,nex;
now.level=start;
now.steps=0;
queue<cir> forward;
forward.push(now);
vis[now.level]=0;
while(!forward.empty())
{
now=forward.front();
if(now.level==destination)
{
cout<<now.steps;
return;
}
forward.pop();
nex.level=now.level+k[now.level];
nex.steps=now.steps+1;
if(nex.level<=n&&vis[nex.level]==0)
{
forward.push(nex);
vis[nex.level]++;
}
nex.level=now.level-k[now.level];
nex.steps=now.steps+1;
if(nex.level>=1&&vis[nex.level]==0)
{
forward.push(nex);
vis[nex.level]++;
}
}
cout<<-1;
}
int main()
{
cin>>n>>start>>destination;
for(int i=1;i<n+1;i++)
{
cin>>k[i];
}
bfs();
return 0;
}
p1443
嗯很温柔
就是把bfs每个节点的方向变成了十六个,然后在结构体中加入了“步数”这一属性
#include<bits/stdc++.h>
using namespace std;
int tx[4]={-1,-2,1,2};
int ty[4]={-1,-2,1,2};
int vis[401][401]={0};
vector<vector<int>> capture(401,vector<int>(401,-1));
int n,m,x,y;
struct dian
{
int x,y;
int steps;
};
bool check(dian a)
{
if(vis[a.x][a.y]==0&&a.x<=n&&a.y<=m&&a.x>=1&&a.y>=1)return true;//x坐标和y坐标别搞反了!!!!!!!!!!!!!!!
else return false;
}
void bfs()
{
dian now,next;
now.x=x;
now.y=y;
vis[x][y]=1;
capture[x][y]=0;
now.steps=0;
queue<dian> beauty;
beauty.push(now);
while(!beauty.empty())
{
now=beauty.front();
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(abs(tx[i])!=abs(ty[j]))
{
next.x=now.x+tx[i];
next.y=now.y+ty[j];
// cout<<next.x<<endl<<next.y<<endl;
if(check(next))
{
next.steps=now.steps+1;
capture[next.x][next.y]=next.steps;
beauty.push(next);
vis[next.x][next.y]=1;
}
}
}
}
beauty.pop();
}
}
int main()
{
cin>>n>>m>>x>>y;
bfs();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout<<capture[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
p3958
这题,,,我总感觉是暴力的
找出所有的底层球放入队列
然后找出每个底层球能去的球
标记去过的球
每次在循环开始时判断这个球能不能出去:如果能“Yes”并退出循环
循环结束输出:No;
并查集优化时间没时间研究啦!!
#include<bits/stdc++.h>
using namespace std;
double n,h,r;
struct circul
{
int x,y,z;
};
double operator-(circul a,circul b)//求出两个特定秋的距离
{
double p;
p=sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2)+pow(a.z-b.z,2));
return p;
};
bool check(circul a,circul b)//判断两个球是否能通行,,遍历走一遍就行了吧,不一定是向上,可以同向穿过去了后才能向上穿
{
double p=a-b;
if(p<=2*r)return true;
else return false;
}
/*bool cmp(circul a,circul b)//对球心从低到高进行排序
{
return a.z<b.z;
}*/
void bfs(vector<circul> &ar)
{
queue<circul> forward;
vector<int> vis(n,1);
for(int j=0;j<n;j++)
{
if(ar[j].z<=r)
{
forward.push(ar[j]);
vis[j]=0;
// cout<<ar[j].z<<endl;
}
else continue;
}
circul now,next;
while(!forward.empty())
{
now=forward.front();
forward.pop();
if(now.z+r>=h)
{
cout<<"Yes"<<endl;
return;
}
for(int j=0;j<n;j++)
{
if(check(ar[j],now)&&vis[j])
{
next=ar[j];
vis[j]=0;
forward.push(next);
}
}
}
cout<<"No"<<endl;
}
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
cin>>n>>h>>r;
vector<circul> arr(n);
for(int j=0;j<n;j++)cin>>arr[j].x>>arr[j].y>>arr[j].z;
bfs(arr);
}
return 0;
}
p1162
这题,,先把所有的0变成2
把这个矩阵加上一圈二
从(0,0)开始搜
是二变成0,把周围的二变成0并把坐标加入队列中
#include<bits/stdc++.h>
using namespace std;
int n;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct dian
{
int x,y;
};
int main()
{
cin>>n;
vector<vector<int>> arr(n+2,vector<int> (n+2,2));
for(int i=1;i<n+1;i++)
{
for(int j=1;j<n+1;j++)
{
cin>>arr[i][j];
if(arr[i][j]==0)arr[i][j]=2;
}
}
queue<dian> forward;
dian now,next;
now.x=now.y=0;
forward.push(now);
while(!forward.empty())
{
now=forward.front();
forward.pop();
for(int i=0;i<4;i++)
{
if(0<=now.x+dx[i]&&now.x+dx[i]<=n+1&&now.y+dy[i]<=n+1&&0<=now.y+dy[i])
{
if(arr[now.x+dx[i]][now.y+dy[i]]==2)
{
arr[now.x+dx[i]][now.y+dy[i]]=0;
next.x=now.x+dx[i];
next.y=now.y+dy[i];
forward.push(next);
}
}
}
}
for(int i=1;i<n+1;i++)
{
for(int j=1;j<n+1;j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}