java 十字路口,十字路口

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.IOException;

import java.util.Queue;

import java.util.LinkedList;

class Node {

public int x;

public int y;

public Node(int x, int y) {

this.x = x;

this.y = y;

}

}

public class Main {

static int n, m, xs, ys, xt, yt;

static int[][] a, b;             // 十字路口的属性

static boolean[][] visited;      // 节点是否已经被走过

static int[] direction = new int[]{0, -1, 1};   // 移动方向

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String[] params = br.readLine().trim().split(" ");

n = Integer.parseInt(params[0]);

m = Integer.parseInt(params[1]);

xs = Integer.parseInt(params[2]);

ys = Integer.parseInt(params[3]);

xt = Integer.parseInt(params[4]);

yt = Integer.parseInt(params[5]);

a = new int[n + 1][m + 1];

b = new int[n + 1][m + 1];

visited = new boolean[n + 1][m + 1];

for(int i = 1; i <= n; i++){

params = br.readLine().trim().split(" ");

for(int j = 1; j <= m; j++)

a[i][j] = Integer.parseInt(params[j - 1]);

}

for(int i = 1; i <= n; i++){

params = br.readLine().trim().split(" ");

for(int j = 1; j <= m; j++)

b[i][j] = Integer.parseInt(params[j - 1]);

}

int cost = 0;   // 初始化花费的时间

visited[xs][ys] = true;

System.out.println(bfs(cost));

}

private static int bfs(int cost) {

// 用于存储同一时间点可能到达的节点

Queue queue = new LinkedList<>();

// 先把起点加入队列

queue.offer(new Node(xs, ys));

while(!queue.isEmpty()){

int len = queue.size();   // 当前时刻有len个可能的位置

while(len-- > 0){

Node cur = queue.poll();

// 找到了小团,直接返回耗时

if(cur.x == xt && cur.y == yt) return cost;

// 在当前位置尝试移动

for(int j = 0; j 

int remainder = cost % (a[cur.x][cur.y] + b[cur.x][cur.y]);

int x = cur.x, y = cur.y;

if(remainder 

// 时间处于[k*aij+k*bij), (k+1)*aij+k*bij)

x += direction[j];

}else{

// 时间处于[(k+1)*aij+k*bij), (k+1)*aij+(k+1)*bij)

y += direction[j];

}

// 移动位置不合法

if(x  n || y  m) continue;

// 如果下一个位置还没有走过或者当前时刻不进行移动,就往队列中添加节点

if(!visited[x][y] || j == 0){

queue.offer(new Node(x, y));

visited[x][y] = true;

}

}

}

cost ++;

}

return cost;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值