2964 问题 L: long time no see

问题 L: long time no see
时间限制: 10 Sec 内存限制: 64 MB
提交: 25 解决: 5
[提交][状态][讨论版]
题目描述
Little X lives in (1,1),and little Y lives in (N,N).One day they decide to meet each other.However, the road is full of danger. They can only go to adjacent cells and take time that is equal to the value on the cell.Please tell them the minimum time they should take.

输入
There is a T at the beginning of file, show that there is T case follow.
For every test case, the first line contain one integer N.Then come two N * N matrices A and B.(1000 >= N >= 2)
If little X wants to go to (i,j),he will spend A(i,j) on moving to adjacent cell (i,j).
If little Y wants to go to (i,j),he will spend B(i,j) on moving to adjacent cell (i,j).

0<=A(i,j)<=10 0<=B(i,j)<=10

输出
For every case,print the minimum time in one line.

样例输入
2
2
0 8
5 1
10 5
9 0
3
0 5 6
6 2 8
2 2 6
3 8 7
2 5 3
4 3 0

样例输出
6
8

/*H.long time no see(搜索)
从小X和小Y的那个点出发,用bfs等方法找出从出发点到任意一点的最短距离,
最后再遍历一下所有点,更新答案既可.
注意:题目并没有限制要求小X只能向下向右走或者小Y只能向左向上走。*/
/*什么时候入队列,值更新就入队列*/ 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#include <cstring>
#include <fstream>
#include <string>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
#define pii pair<int,int> //点 广搜队列 
typedef long long ll;
const int maxn = 1005;
ofstream out("out.txt");
int dp1[maxn][maxn];
int dp2[maxn][maxn];
int ma1[maxn][maxn];
int ma2[maxn][maxn];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
int N;
int T;
int xx,yy;
queue<pii> que;
void bfs1(){        //广搜 
    que.push(pii(1,1));     //(1,1)入队 
    while(!que.empty()){    //队列不空 
        pii temp = que.front(); //temp队列的头 
        que.pop(); // 扩展过的点弹出 
   //     xx = temp.first + 1;  //? + 1 
    //  yy = temp.second;
        for(int i = 0;i < 4;i++){    //枚举四个方向 
            xx = temp.first + dx[i];
            yy = temp.second + dy[i];
            if(xx >= 1 && xx <= N && yy >= 1 && yy <= N){  //可以到那点 
                if(dp1[xx][yy] > dp1[temp.first][temp.second] + ma1[xx][yy]){    //更新距离 
                    dp1[xx][yy] = dp1[temp.first][temp.second] + ma1[xx][yy];
                    que.push(pii(xx,yy));
                }
            }
        }
    }
}
void bfs2(){
    que.push(pii(N,N));
    while(!que.empty()){
        pii temp = que.front();
        que.pop();
        for(int i = 0;i < 4;i++){
            xx = temp.first + dx[i];
            yy = temp.second + dy[i];
            if(xx >= 1 && xx <= N && yy >= 1 && yy <= N){
                if(dp2[xx][yy] > dp2[temp.first][temp.second] + ma2[xx][yy]){
                    dp2[xx][yy] = dp2[temp.first][temp.second] + ma2[xx][yy];
                    que.push(pii(xx,yy));
                }
            }
        }

    }
}

int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        for(int i = 1;i <= N;i++)for(int j = 1;j <= N;j++)
        scanf("%d",&ma1[i][j]),dp1[i][j] = dp2[i][j] = 999999999;   //图的值 
        for(int i = 1;i <= N;i++)for(int j = 1;j <= N;j++)scanf("%d",&ma2[i][j]);

        dp1[1][1] = 0;
        bfs1();

        dp2[N][N] = 0;
        bfs2();
        int ans = 2100000000;
        for(int i = 1;i <= N;i++)    
            for(int j = 1;j <= N;j++)
                ans = min(ans,max(dp1[i][j],dp2[i][j]));    //到同一点的最大值 
        cout<<ans<<endl;
//      out<<ans<<endl;
    }   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值