称不上算法的算法-3.bfs

本文详细介绍了BFS(宽度优先搜索)算法的应用实例,包括三种不同场景:地图寻路问题的基本实现、寻找最短时间路径及同时展示行动路径。通过具体代码解析了如何利用队列、优先队列等数据结构优化算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.HRBUST1613

待优化

注意换行对于char读取的影响!!!

bfs大体步骤:建立记录地图,确定起点,起点入队,以队列不空为条件循化,在循环内不断把队列的首元素当成操作元素,并把此操作元素的所有下层可能结果入队。

这样循环往复就可以把每层搜尽。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 char map[110][110];
 6 struct point{
 7     int x;
 8     int y;
 9 };
10 int xmove[4]={0,0,-1,1};
11 int ymove[4]={1,-1,0,0};
12 int times[110][110];
13 point P[10000];
14 int main(void){
15     int t;
16     scanf("%d",&t);
17     for(int tt=0;tt<t;tt++){
18         point start;
19         int sumofp=0;
20         int judg=0;
21         memset(times,0,sizeof(times));
22         int R,C;
23         scanf("%d%d",&R,&C);
24         getchar();
25         for(int i=0;i<R;i++){
26             for(int j=0;j<C;j++){
27                 scanf("%c",&map[i][j]);
28                 if(map[i][j]=='Z'){
29                     start.x=i;
30                     start.y=j;
31                 }
32                 else if(map[i][j]=='P'){
33                     point tmp;
34                     tmp.x=i;
35                     tmp.y=j;
36                     P[sumofp]=tmp;
37                     sumofp++;
38                 }
39             }
40             getchar();
41         }
42         queue<point> que;
43         que.push(start);
44         while(!que.empty()){
45             point atmp=que.front();
46             que.pop();
47             for(int g=0;g<4;g++){
48                 point nowoption;
49                 nowoption.x=atmp.x+xmove[g];
50                 nowoption.y=atmp.y+ymove[g];
51                 if(nowoption.x>=0&& nowoption.x<R&& nowoption.y>=0&& nowoption.y<C&& map[nowoption.x][nowoption.y]!='#'&&times[nowoption.x][nowoption.y]==0){
52                     if(map[nowoption.x][nowoption.y]=='W'){
53                         judg=1;
54                         printf("%d\n",times[atmp.x][atmp.y]+1);
55                         break;
56                     }
57                     else if(map[nowoption.x][nowoption.y]=='.'){
58                         que.push(nowoption);
59                         times[nowoption.x][nowoption.y]=times[atmp.x][atmp.y]+1;
60                     }
61                     else if(map[nowoption.x][nowoption.y]=='P'){
62                         for(int j=0;j<sumofp;j++){
63                             que.push(P[j]);
64                             times[P[j].x][P[j].y]=times[atmp.x][atmp.y]+1;
65                         }
66                     }
67                 }
68             }
69             if(judg==1)
70                 break;
71         }
72         if(judg==0){
73             printf("IMPOSSIBLE\n");
74         }
75     }
76 }

 2.HRBUST1621(时间最短)

使用优先队列,时间短优先

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 char map[220][220];
 6 int times[220][220];
 7 int xmove[4]={0,0,1,-1};
 8 int ymove[4]={1,-1,0,0};
 9 struct point{
10     int x,y;
11     int per_group;
12     bool operator< (const point&a)const{
13         return times[a.x][a.y]<times[x][y];
14     }
15 };
16 int main(void){
17     int T;
18     scanf("%d",&T);
19     for(int tt=0;tt<T;tt++){
20         memset(times,0,sizeof(times));
21         point start;
22         point endp;
23         int R,C;
24         scanf("%d%d",&R,&C);
25         getchar();
26         for(int i=0;i<R;i++){
27             for(int j=0;j<C;j++){
28                 scanf("%c",&map[i][j]);
29                 if(map[i][j]=='Z'){
30                     start.x=i;
31                     start.y=j;
32                 }
33                 if(map[i][j]=='W'){
34                     endp.x=i;
35                     endp.y=j;
36                 }
37             }
38             getchar();
39         }
40         priority_queue<point> que;
41         que.push(start);
42         while(!que.empty()){
43             point nowpoint=que.top();
44             que.pop();
45             for(int i=0;i<4;i++){
46                 point tmp;
47                 tmp.x=nowpoint.x+xmove[i];
48                 tmp.y=nowpoint.y+ymove[i];
49                 if(tmp.x>=0&&tmp.x<R&&tmp.y>=0&&tmp.y<C&&map[tmp.x][tmp.y]!='#'&&times[tmp.x][tmp.y]==0){
50                     if(map[tmp.x][tmp.y]=='W'){
51                         times[tmp.x][tmp.y]=times[nowpoint.x][nowpoint.y]+1;
52                         break;
53                     }
54                     if(map[tmp.x][tmp.y]=='.'){
55                         times[tmp.x][tmp.y]=times[nowpoint.x][nowpoint.y]+1;
56                     }
57                     else{
58                         times[tmp.x][tmp.y]=times[nowpoint.x][nowpoint.y]+1+map[tmp.x][tmp.y]-'0';
59                     }
60                     que.push(tmp);
61                 }
62             }
63         }
64         if(times[endp.x][endp.y]==0)
65             printf("IMPOSSIBLE\n");
66         else
67             printf("%d\n",times[endp.x][endp.y]);
68     }
69 }

 3.HDU1026(时间最短+打印路径)

打印路径用递归

 1 #include<stdio.h>
 2 #include<queue>
 3 #include<string.h>
 4 char themap[150][150];
 5 int xmove[4]={0,0,1,-1};
 6 int ymove[4]={1,-1,0,0};
 7 int dir[150][150];
 8 int ti[150][150];
 9 int n,m,sum;
10 struct node{
11     int x,y;
12     int times;
13     friend bool operator <(node a,node b){
14         return a.times>b.times;
15     }
16 };
17 using namespace std;
18 void show(int x,int y){
19     if(x==0&&y==0)
20         return;
21     show(x-xmove[dir[x][y]],y-ymove[dir[x][y]]);
22     printf("%ds:(%d,%d)->(%d,%d)\n",sum,x-xmove[dir[x][y]],y-ymove[dir[x][y]],x,y);
23     sum++;
24     while(ti[x][y]!=0){
25         printf("%ds:FIGHT AT (%d,%d)\n",sum,x,y);
26         sum++;
27         ti[x][y]--;
28     }
29 }
30 int main(void){
31     while(scanf("%d%d",&n,&m)==2){
32         int judg=0;
33         sum=1;
34         getchar();
35         memset(ti,0,sizeof(ti));
36         memset(themap,-1,sizeof(themap));
37         memset(dir,-1,sizeof(dir));
38         for(int i=0;i<n;i++){
39             for(int j=0;j<m;j++){
40                 scanf("%c",&themap[i][j]);
41             }
42             getchar();
43         }
44         node start;
45         start.x=0;
46         start.y=0;
47         start.times=0;
48         themap[0][0]='X';
49         priority_queue<node> que;
50         que.push(start);
51         while(!que.empty()){
52             node nownode;
53             nownode=que.top();
54             que.pop();
55             if(nownode.x==n-1&&nownode.y==m-1){
56                 judg=1;
57                 printf("It takes %d seconds to reach the target position, let me show you the way.\n",nownode.times);
58                 break;
59             }
60             for(int i=0;i<4;i++){
61                 node tmp;
62                 tmp.x=nownode.x+xmove[i];
63                 tmp.y=nownode.y+ymove[i];
64                 tmp.times=nownode.times+1;
65                 if(tmp.x>=0&&tmp.x<n&&tmp.y>=0&&tmp.y<m&&themap[tmp.x][tmp.y]!='X'){
66                     if(themap[tmp.x][tmp.y]=='.'){
67                         themap[tmp.x][tmp.y]='X';
68                     }
69                     else{
70                         tmp.times+=themap[tmp.x][tmp.y]-'0';
71                         ti[tmp.x][tmp.y]=themap[tmp.x][tmp.y]-'0';
72                         themap[tmp.x][tmp.y]='X';
73                     }
74                     dir[tmp.x][tmp.y]=i;
75                     que.push(tmp);
76                 }
77             }
78         }
79         if(judg==0){
80             printf("God please help our poor hero.\n");
81         }
82         else{
83             show(n-1,m-1);
84         }
85         printf("FINISH\n");
86     }
87 }

 

转载于:https://www.cnblogs.com/liuzey/p/8965206.html

内容概要:该PPT详细介绍了企业架构设计的方法论,涵盖业务架构、数据架构、应用架构和技术架构四大核心模块。首先分析了企业架构现状,包括业务、数据、应用和技术四大架构的内容和关系,明确了企业架构设计的重要性。接着,阐述了新版企业架构总体框架(CSG-EAF 2.0)的形成过程,强调其融合了传统架构设计(TOGAF)和领域驱动设计(DDD)的优势,以适应数字化转型需求。业务架构部分通过梳理企业级和专业级价值流,细化业务能力、流程和对象,确保业务战略的有效落地。数据架构部分则遵循五大原则,确保数据的准确、一致和高效使用。应用架构方面,提出了分层解耦和服务化的设计原则,以提高灵活性和响应速度。最后,技术架构部分围绕技术框架、组件、平台和部署节点进行了详细设计,确保技术架构的稳定性和扩展性。 适合人群:适用于具有一定企业架构设计经验的IT架构师、项目经理和业务分析师,特别是那些希望深入了解如何将企业架构设计与数字化转型相结合的专业人士。 使用场景及目标:①帮助企业和组织梳理业务流程,优化业务能力,实现战略目标;②指导数据管理和应用开发,确保数据的一致性和应用的高效性;③为技术选型和系统部署提供科学依据,确保技术架构的稳定性和扩展性。 阅读建议:此资源内容详尽,涵盖企业架构设计的各个方面。建议读者在学习过程中,结合实际案例进行理解和实践,重点关注各架构模块之间的关联和协同,以便更好地应用于实际工作中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值