1 条题解

  • 5
    @ 2022-12-12 19:41:42
    #include <bits/stdc++.h>//万能头文件
    using namespace std;
    int a[40][40];//地图
    int q[10000][3];//队列,存储走过的点
    int s[40][40];//存放到每个点的最小危险系数 
    //方向数组 
    int fx[4] = {0,1,0,-1};
    int fy[4] = {1,0,-1,0};
    int n,m,i,j; 
    int main()
    {
    	cin>>n>>m; 
    	for(i = 1;i <= n;i++)
        {
    		for(j = 1;j <= m;j++)
            {
    			cin>>a[i][j];
    			s[i][j] = INT_MAX;
    		}
    	}
    	int head = 1;
    	int tail = 1;
    	int tx,ty;
    	q[head][1] = 1;
    	q[head][2] = 1;
    	s[1][1] = a[1][1];//第一个点的危险系数是确定的 
    	while(head <= tail)
        {
    		for(i = 0;i < 4;i++)
            {
    			//探测新点 
    			tx = q[head][1] + fx[i];
    			ty = q[head][2] + fy[i];
    			//要去的点在棋盘内,且从head点去该点的危险系数更小,则过去 
    			if(tx >= 1 && tx <= n && ty >= 1 && ty <= m && s[q[head][1]][q[head][2]] + a[tx][ty] < s[tx][ty])
                {
    				tail++;
    				q[tail][1] = tx;
    				q[tail][2] = ty; 
    				//更新去的点的危险系数
    				s[tx][ty] = s[q[head][1]][q[head][2]] + a[tx][ty];
    			} 
    		}
    		head++;
    	}
    	cout<<s[n][m]<<endl;//输出
        return 0;
    }
    //代码已AC
    
    • 1

    信息

    ID
    538
    时间
    1000ms
    内存
    128MiB
    难度
    4
    标签
    递交数
    54
    已通过
    27
    上传者