2024-01-19竞赛笔记

2024/1/19

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//T172312 走迷宫
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x;
int y;
int s;//步数
} que[10001];
int head,tail,r,s,p,q;
char c[1001][1001];//保存地图
bool flag;//标记是否达到目标点,0未到,1已到
int a[4]={-1,1,0,0},b[4]={0,0,-1,1};//可走的组合
bool f[1001][1001];//记录已走的点
void bfs()
{
while(head<tail)//队列不为空时操作
{
for(int i=0;i<=3;i++)//枚举四个方向
{
int xx=que[head].x+a[i];
int yy=que[head].y+b[i];
if(xx>=1&&xx<=r&&yy>=1&&yy<=s&&c[xx][yy]=='.'&&!f[xx][yy])//判断x,y下一步是否可走且是否走过
{
f[xx][yy]=1;//标记已走
que[tail].x=xx;
que[tail].y=yy; //更新xx和yy的值
que[tail].s=que[head].s+1;//步数是父亲的步数+1
tail++;
}
if(xx==p&&yy==q)//如果找到目标点
{
flag=1;//标记已完成
break;//退出
}
}
if(flag==1) break;
head++;//head++才能对后面的点进行二次扩展
}
}
int main()
{
cin>>r>>s;
for(int i=1;i<=r;i++)
{
for(int j=1;j<=s;j++)
{
cin>>c[i][j];
}
}
p=r;q=s;//终点坐标
head=1;tail=2;
que[tail].x=1; que[tail].y=1; que[tail].s=0+1;
tail++;
f[1][1]=1;
flag=0;
//初始化
bfs();
cout<<que[tail-1].s<<endl;
return 0;
}
//禁止直接抄袭!打击这种行为!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//B3625 迷宫寻路
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x;
int y;
int s;//步数
} que[10001];
int head,tail,r,s,p,q;
char c[1001][1001];//保存地图
bool flag;//标记是否达到目标点,0未到,1已到
int a[4]={-1,1,0,0},b[4]={0,0,-1,1};//可走的组合
bool f[1001][1001];//记录已走的点
void bfs()
{
while(head<tail)//队列不为空时操作
{
for(int i=0;i<=3;i++)//枚举四个方向
{
int xx=que[head].x+a[i];
int yy=que[head].y+b[i];
if(xx>=1&&xx<=r&&yy>=1&&yy<=s&&c[xx][yy]=='.'&&!f[xx][yy])//判断x,y下一步是否可走且是否走过
{
f[xx][yy]=1;//标记已走
que[tail].x=xx;
que[tail].y=yy; //更新xx和yy的值
que[tail].s=que[head].s+1;//步数是父亲的步数+1
tail++;
}
if(xx==p&&yy==q)//如果找到目标点
{
flag=1;//标记已完成
break;//退出
}
}
if(flag==1) break;
head++;//head++才能对后面的点进行二次扩展
}
}
int main()
{
cin>>r>>s;
for(int i=1;i<=r;i++)
{
for(int j=1;j<=s;j++)
{
cin>>c[i][j];
}
}
p=r;q=s;//终点坐标
head=1;tail=2;
que[tail].x=1; que[tail].y=1; que[tail].s=0+1;
tail++;
f[1][1]=1;
flag=0;
//初始化
bfs();
if(flag==1) cout<<"Yes";
else cout<<"No";
return 0;
}
//禁止直接抄袭!打击这种行为!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//P1451 求细胞数量
#include<bits/stdc++.h>
using namespace std;
int n,m,dx[4]={0,1,0,-1},dy[4]={-1,0,1,0},a[100][100];//保存地图,枚举方向
int sum,q[10000][4],h,t;//模拟BFS的数组
void fun(int x,int y)
{
h=1,t=1;
q[h][1]=x;
q[h][2]=y;//初始化
while(h<=t)//队列不为空
{
for(int i=0;i<4;i++)//枚举方向
{
int xx=q[h][1]+dx[i];
int yy=q[h][2]+dy[i];
if(a[xx][yy]!=0&&xx>=1&&xx<=n&&yy>=1&&yy<=m)//如果满足移动条件
{
t++;//尾部++
q[t][1]=xx;
q[t][2]=yy;
a[xx][yy]=0;//地图中标记为0
}
}
h++;//继续循环,head++
}
}
signed main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%1d",&a[i][j]);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]!=0)//是其他数字时
{
fun(i,j);
sum++;
}
}
}
cout<<sum;
return 0;
}
//禁止直接抄袭!打击这种行为!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//B3626 跳跃机器人
#include<bits/stdc++.h>
using namespace std;
int n;
queue<int> q;
int s[1000010];//记录步数
void bfs(){
memset(s,-1,sizeof(s));//初始化数组
q.push(1);//bot原位
s[1]=0;//七点不需要步数
while(!q.empty()){//队列不为空
int t=q.front();
q.pop();//提前出队第一元素
if(t==n){//到达n
cout<<s[n];
return;
}
if(t-1>=1&&t-1<=n&&s[t-1]==-1){//x-1
q.push(t-1);
s[t-1]=s[t]+1;
}
if(t+1>=1&&t+1<=n&&s[t+1]==-1){//x+1
q.push(t+1);
s[t+1]=s[t]+1;
}
if(2*t>=1&&2*t<=n&&s[t*2]==-1){//2x
q.push(t*2);
s[t*2]=s[t]+1;
}
}
return;
}
signed main()
{
cin>>n;
bfs();
return 0;
}

【初一算法基础】宽搜