4 条题解

  • 3
    @ 2022-10-28 21:27:01

    其实可以用A*做

    #pragma GCC optimize(1)
    #pragma GCC optimize(2)
    #pragma GCC optimize(3)
    #pragma GCC optimize("Ofast")
    #include <cmath>
    #include <cstdio>
    #include <algorithm>
    #include <queue>
    using namespace std;
    
    struct QWQ{
    	int x , y , g , v;
    	//位置,估值,当前步数
    	bool operator < (const QWQ &x) const{
    		return x.g < g;
    	}
    };
    int n , m , zx , zy;
    bool vis[105][105];
    int dis[10][5] = {{} , {0 , 1 , 0} , {0 , -1 , 0} , {0 , 0 , 1} , {0 , 0 , -1}};
    priority_queue <QWQ> q;
    
    int g(int x , int y , int v){
    	return abs(zy + zx - y - x) + v;
    }
    
    void A_Star(){
    	while(!q.empty()){
    		QWQ t = q.top();
    		q.pop();
    		// printf("%d %d\n" , t.x , t.y);
    		if(t.x == zx && t.y == zy){
    			printf("%d\n" , t.v);
    			return ;
    		}
    		for(register int i = 1;i <= 4;i++){
    			int xx = t.x + dis[i][1];
    			int yy = t.y + dis[i][2];
    			if(xx <= 0 || yy <= 0 || xx > n || yy > m) continue;
    			if(vis[xx][yy]) continue;
    			vis[xx][yy] = true;
    			q.push((QWQ){xx , yy , g(xx , yy , t.v + 1) , t.v + 1});
    		}
    	}
    	puts("-1");
    }
    
    int main(void){
    	// freopen("qwq.in" , "r" , stdin);
    	scanf("%d%d" , &n , &m);
    	for(register int i = 1;i <= n;i++){
    		char s[105];
    		scanf("%s" , s + 1);
    		for(register int j = 1;j <= m;j++){
    			if(s[j] == 'S'){
    				q.push((QWQ){i , j , 0 , 0});
    				vis[i][j] = true;
    			}
    			if(s[j] == 'T') zx = i , zy = j;
    			if(s[j] == '#') vis[i][j] = true;
    		}
    	}
    	A_Star();
    }
    
    • 1
      @ 2024-1-24 15:48:56

      深搜(DFS)————驾到!!!做过"走出迷宫的最少步数"的人都知道代码是长这样的。

      #include <bits/stdc++.h>
      using namespace std;
      int n,m,b[100][100];//b数组用来存储步数
      char a[100][100];
      int fx[5]={0,0,1,0,-1};//定义方向数组
      int fy[5]={0,1,0,-1,0};
      void fun(int x,int y,int k){//递归深搜
      	b[x][y]=k;//初始化b[x][y]
      	for (int i=1;i<=4;i++){//简单循环求解
      		int tx=x+fx[i];//以x,y点为中心的右下左上
      		int ty=y+fy[i];
      		if (a[tx][ty]=='.'&&k+1<b[tx][ty])fun(tx,ty,k+1)//递归
              //判断该点是否在迷宫内and是否比上一次求出来走到该点的步数多
      	}
      }
      int main(){
      	cin>>n>>m;//输入
      	for (int i=1;i<=n;i++){
      		for (int j=1;j<=m;j++){
      			cin>>a[i][j];
      			b[i][j]=INT_MAX;//初始化b数组里的职全为最大
      		}
      	}
      	fun(1,1,1);//调用函数
      	cout<<b[n][m];//输出
      	return 0;
      }
      

      但本题要求我们从S点——T点这不一模一样。只要找到S点与T点下标即可!!!

      #include <bits/stdc++.h>
      using namespace std;
      int n,m,b[100][100],k1,k2,c1,c2;//定义变量存储
      char a[100][100];
      int fx[5]={0,0,1,0,-1};
      int fy[5]={0,1,0,-1,0};
      void fun(int x,int y,int k){
      	b[x][y]=k;
      	for (int i=1;i<=4;i++){
      		int tx=x+fx[i];
      		int ty=y+fy[i];
      		if (a[tx][ty]!='#'&&k+1<b[tx][ty])fun(tx,ty,k+1);
      	}
      }
      int main(){
      	cin>>n>>m;
      	for (int i=1;i<=n;i++){
      		for (int j=1;j<=m;j++){
      			cin>>a[i][j];
      			b[i][j]=INT_MAX;
      			if (a[i][j]=='S'){
      				k1=i;
      				k2=j;
      			}
      			if (a[i][j]=='T'){
      				c1=i;
      				c2=j;
      			}
      		}
      	}
      	fun(k1,k2,1);
      	cout<<b[c1][c2]-1;//这里必须减一,算上终点
      	return 0;
      }
      

      🎉️ easy🎉️

      • 0
        @ 2022-10-26 13:36:57
        #include<bits/stdc++.h>
        using namespace std;
        int r,c,dx[]={-1,0,0,1},dy[]={0,-1,1,0};
        char a[105][105];
        bool vis[105][105];
        struct Point{
            int x,y,step;
        };
        queue<Point> q;
        bool check(int x,int y){ // 检查该点(x,y)是否可以通行以及是否未走过
            return x>=1&&y>=1&&x<=r&&y<=c&&!vis[x][y]&&a[x][y]!='#';
        }
        int bfs(){
            while(q.size()){
                Point now=q.front();
                q.pop();
                for(short i=0;i<4;i++){
                    int nx=now.x+dx[i],ny=now.y+dy[i];
                    if(check(nx,ny)){
                        if(a[nx][ny]=='T')return now.step+1;
                        vis[nx][ny]=1;
                        q.push((Point){nx,ny,now.step+1});
                    }
                }
            }
        }
        int main(){
            ios::sync_with_stdio(false);
            cin.tie(0);
            cout.tie(0);
            cin>>r>>c;
            for(int i=1;i<=r;i++)
                for(int j=1;j<=c;j++){
                    cin>>a[i][j];
                    if(a[i][j]=='S'){
                        vis[i][j]=1;
                        q.push((Point){i,j,0});
                    }
                }
            cout<<bfs();
            return 0;
        }
        
        
        • 0
          @ 2022-4-24 17:50:38

          写题解请注意

          鼓励大家写题解,但注意题解格式。

          题解一定要有思路解析或代码注释,能否让别人理解你的思路

          也是你的能力的检验,不要只放无意义的代码给大家复制,那就失去了做题的初心。

          给代码两端加上这个会舒服一些

          ```cpp

          你的代码

          ```

          </span>

          这个点在键盘的左上角tab上面那个键,注意切换输入法

          #include<iostream>
          using namespace std;
          int main()
          {
              int n;
              cin>>n;//这是一个注释
              return 0;
          } 
          

          请注意严禁抄袭题解,写题解不要只放代码,需加上你的思路或代码注释。

          抄袭题解一经发现直接取消成绩。

          题解被删除的可能

          1. 代码不符合格式规范
          2. 没有思路讲解或者没有注释,
          3. 无意义的题解

          大家携手共同维护一个良好的编程环境,如果一经发现,多次作乱。可能会被管理员拉黑,请注意,一旦拉黑即失去登陆资格。

          • 1

          【基础】走出迷宫的最少步数2

          信息

          ID
          430
          时间
          1000ms
          内存
          128MiB
          难度
          4
          标签
          递交数
          76
          已通过
          36
          上传者