1 条题解

  • 0
    @ 2025-10-10 15:48:07

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
     
    //定义方向增量
    int dx[12]={1,1,-1,-1,2,2,-2,-2};
    int dy[12]={-2,2,2,-2,1,-1,1,-1};
    int s[200][200];//定义棋盘,保存到达每个步的最少步数 
    char a[200][200];//棋盘 
    int que[10000][4];
     
    int main(){
        int n,m,i,j; 
        int s1,s2,e1,e2;//开始和结束位置 
         
        cin>>m>>n;
        for(i = 1;i <= n;i++){
            for(j = 1;j <= m;j++){
                cin>>a[i][j];
                if(a[i][j] == 'K'){
                    s1 = i;
                    s2 = j;
                }else if(a[i][j] == 'H'){
                    e1 = i;
                    e2 = j;
                }
            }
        }
         
        //初始化 
        memset(s,-1,sizeof(s));
        int head=1,tail=1;//初始化队列
        //(s1,s2)点入队,到达该点的最少步数0
        que[1][1]=s1;que[1][2]=s2;que[1][3]=0;
        while(head<=tail){
            //循环8个方向,将所能到达的点入队
            for(int i=0;i<8;i++){
                //马跳跃后的位置
                int x=que[head][1]+dx[i];
                int y=que[head][2]+dy[i];
                //如果没有越界 
                if(x>=1&&y>=1&&x<=n&&y<=m&&a[x][y]!='*'){
                    //并且该点没有走过,则点入队 
                    if(s[x][y]==-1){
                        s[x][y]=que[head][3]+1; //计算从(s1,s2)到(x,y)的最少步数
                        tail++;//队尾+1
                        //x,y入队
                        que[tail][1]=x;
                        que[tail][2]=y;
                        que[tail][3]=s[x][y];
                        //到达(e1,e2)的最小路径都被求出后输出 
                        if(s[e1][e2]>0){
                            cout<<s[e1][e2]<<endl;
                            return 0;//跳出循环,不再遍历 
                        }
                    } 
                     
                }   
            } 
            head++;//出队
        } 
    }
    
    

    Java :

    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Scanner;
    
    public class Main {
    	static int n;
    	static int m;
    	//起点K
    	static int x;
    	static int y;
    	//终点H
    	static int s;
    	static int t;
    	//记录步数
    	static int a[][] = new int[151][151];
    	static char map[][] = new char[151][151];
    	//方向数组
    	static int fx[] = {-1,-2,-2,-1,1,2,2,1};
    	static int fy[] = {-2,-1,1,2,2,1,-1,-2};
    	//队列
    	static Queue<Node> Q = new LinkedList<Node>();
    	
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		m = sc.nextInt();
    		n = sc.nextInt();
    		int i,j;
    		String st = null;
    		for(i=1;i<=n;i++) {
    			st = sc.next();
    			for(j=1;j<=m;j++) {
    				map[i][j] = st.charAt(j-1);
    				if(map[i][j]=='K'){
    					x = i;
    					y = j;
    				}else if(map[i][j]=='H') {
    					s = i;
    					t = j;
    				}
    				a[i][j] = 99;
    			}
    		}
    		Node now = new Node();
    		Node next = new Node();
    		
    		//起始点赋值
    		now.i = x;
    		now.j = y;
    		now.d = 0;
    		Q.add(now);
    		a[now.i][now.j] = now.d;
    		//队列有元素
    		while(Q.size()>0) {
    			now = new Node();
    			now = Q.peek();
    			Q.remove();
    			//方向数组遍历
    			for(int k=0;k<8;k++) {
    				next = new Node();
    				next.i = now.i + fx[k];
    				next.j = now.j + fy[k];
    				next.d = now.d + 1;
    				if(next.i>=1 && next.i<=n && next.j>=1 && next.j<=m && map[next.i][next.j]!='*' && next.d < a[next.i][next.j]) {
    					//更新最小路径
    					a[next.i][next.j] = next.d;
    					Q.add(next);
    				}
    			}
    		}
    		System.out.println(a[s][t]);
    		sc.close();
    	}	
    }
    
    class Node{
    	int i,j,d;
    
    	@Override
    	public String toString() {
    		return "Node [i=" + i + ", j=" + j + ", d=" + d + "]";
    	}
    	
    }
    
    
    • 1

    信息

    ID
    421
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者