3 条题解

  • 0
    @ 2024-5-27 15:04:05

    #include<bits/stdc++.h> using namespace std; int a, b, m, n, aa, ab, mark[310][310]; char d[310][310]; int ax[4] = {0, 0, 1, -1}; int ay[4] = {1, -1, 0, 0}; struct e{ int x, y, time; }; struct l{ int x1, y1, x2, y2; bool j = 0; }p[100]; queue q; void bfs(){ e c; c.x = n; c.y = m; c.time = 0; while(!q.empty()) q.pop();//清空初始化 q.push(c);//初始状态 while(!q.empty()){ e now; now = q.front();//保存现在状态

    q.pop();//去除旧状态
    	e New;
    	if(now.x == aa && now.y == ab){//判断是否到终点
    		cout << now.time;
    		break;
    	}
    	if(mark[now.x][now.y] != 0) continue;//判断是否走过
    	mark[now.x][now.y] = 1;
    	char fpx;
    	for(int i = 0; i < 4; i++){//像四个方向搜索
    		New.x = now.x + ax[i];
    		New.y = now.y + ay[i];
    		if(New.x<0||New.x>=a||New.y<0||New.y>=b||d[New.x][New.y]=='#')//是否满足要求
    			continue;
    		New.time = now.time + 1;//时间加一
    		fpx = d[New.x][New.y];
    		if(fpx >= 'A' && d[New.x][New.y] <= 'Z'){//判断传送
    			if(New.x == p[fpx].x1&&New.y == p[fpx].y1){
    				New.x = p[fpx].x2;
    				New.y = p[fpx].y2;
    			}
    			else if(New.x == p[fpx].x2&&New.y == p[fpx].y2){
    				New.x = p[fpx].x1;
    				New.y = p[fpx].y1;
    			}
    		}
    		q.push(New);
    	}
    }
    

    } int main(){ cin >> a >> b; for(int i = 0; i < a; i++){ for(int j = 0; j < b; j++){ cin >> d[i][j]; } } for(int i = 0; i < a; i++){ for(int j = 0; j < b; j++){ if(d[i][j] == '@'){ n = i, m = j; } if(d[i][j] == '='){ aa = i, ab = j; } if(d[i][j] >= 'A' && d[i][j] <= 'Z'){ if(p[d[i][j]].j == 0){ p[d[i][j]].x1 = i; p[d[i][j]].y1 = j; p[d[i][j]].j = 1; } else{ p[d[i][j]].x2 = i; p[d[i][j]].y2 = j; } } } } bfs(); return 0; }

    • 0
      @ 2024-5-16 20:16:26

      ``none````` ` #include #include #include using namespace std; int n,m,x1,y1,x2,y2; bool jud[26],mark[305][305]; char map[305][305]; int rx[4]={0,0,-1,1}; int ry[4]={1,-1,0,0}; struct zz{ int xx,yy; }zs[26][2]; struct node{ int x,y,t; }; queuea; void bfs(){ node S; S.x=x1; S.y=y1; S.t=0; a.push(S); while(!a.empty()){ node now=a.front(); a.pop(); if(now.xx2&&now.yy2){ cout<<now.t; break; } if(mark[now.x][now.y]) continue; mark[now.x][now.y]=1; for(int i=0;i<4;i++){ int t_x=now.x+rx[i]; int t_y=now.y+ry[i];

      ` if(t_x<1||t_x>n||t_y<1||t_y>m)continue; if(map[t_x][t_y]'#')continue; if(map[t_x][t_y]>='A'&&map[t_x][t_y]<='Z'){ char Now=map[t_x][t_y]-'A'; if(t_xzs[Now][0].xx&&t_yzs[Now][0].yy){ t_x=zs[Now][1].xx; t_y=zs[Now][1].yy; } else{ t_x=zs[Now][0].xx; t_y=zs[Now][0].yy; } } node nw; nw.x=t_x; nw.y=t_y; nw.t=now.t+1; a.push(nw); } } } int main(){ cin>>n>>m; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>map[i][j]; if(map[i][j]'@')x1=i,y1=j; if(map[i][j]=='=')x2=i,y2=j; if(map[i][j]!='@'&&map[i][j]!='='&&map[i][j]!='.'&&map[i][j]!='#'){ int now=map[i][j]-'A'; if(!jud[now]){ zs[now][0].xx=i; zs[now][0].yy=j; jud[now]=1; } else{ zs[now][1].xx=i; zs[now][1].yy=j; } } } } bfs(); return 0; }```

      
      
      • 0
        @ 2024-5-16 19:37:32

        `` #include #include #include using namespace std; int n,m,x1,y1,x2,y2; bool jud[26],mark[305][305]; char map[305][305]; int rx[4]={0,0,-1,1}; int ry[4]={1,-1,0,0}; struct zz{ int xx,yy; }zs[26][2]; struct node{ int x,y,t; }; queuea; void bfs(){ node S; S.x=x1; S.y=y1; S.t=0; a.push(S); while(!a.empty()){ node now=a.front(); a.pop(); if(now.xx2&&now.yy2){ cout<<now.t; break; } if(mark[now.x][now.y])continue; mark[now.x][now.y]=1; for(int i=0;i<4;i++){ int t_x=now.x+rx[i]; int t_y=now.y+ry[i];

        if(t_x<1||t_x>n||t_y<1||t_y>m)continue;
        		if(map[t_x][t_y]=='#')continue;
        		if(map[t_x][t_y]>='A'&&map[t_x][t_y]<='Z'){
        			char Now=map[t_x][t_y]-'A';
        			if(t_x==zs[Now][0].xx&&t_y==zs[Now][0].yy){
        				t_x=zs[Now][1].xx;
        				t_y=zs[Now][1].yy;
        			}
        			else{
        				t_x=zs[Now][0].xx;
        				t_y=zs[Now][0].yy; 
        			}
        		}
        		node nw;
        		nw.x=t_x;
        		nw.y=t_y;
        		nw.t=now.t+1;
        		a.push(nw);
        	}
        }
        }
        int main(){
        cin>>n>>m;
        for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
        cin>>map[i][j];
        if(map[i][j]=='@')x1=i,y1=j;
        if(map[i][j]=='=')x2=i,y2=j;
        if(map[i][j]!='@'&&map[i][j]!='='&&map[i][j]!='.'&&map[i][j]!='#'){
        int now=map[i][j]-'A';
        if(!jud[now]){
        zs[now][0].xx=i;
        zs[now][0].yy=j;
        jud[now]=1;
        }
        else{
        zs[now][1].xx=i;
        zs[now][1].yy=j;
        }
        }
        }
        }
        bfs();
        return 0;
        }```
        • 1

        信息

        ID
        757
        时间
        1000ms
        内存
        512MiB
        难度
        8
        标签
        递交数
        23
        已通过
        6
        上传者