华为OD机试-上班之路-2022Q4 A卷-Py/Java/JS

Jungle 生活在美丽的蓝鲸城,大马路都是方方正正,但是每天马路的封闭情况都不一样。地图由以下元素组成:
1)”.” - 空地,可以达到;

2)”*” - 路障,不可达到;

3)"S” - Jungle的家;

4)”T” - 公司.
其中我们会限制Jungle拐弯的次数,同时Jungle可以清除给定个数的路障,现在你的任务是计算Jungle是否可以从家里出发到达公司。

输入描述

输入的第一行为两个整数tc(o

输入的第二行为两个整数n,m(1≤n,m≤100),代表地图的大小。

接下来是n行包含m个字符的地图。n和m可能不一样大。
我们保证地图里有S和T。

输出描述
输出是否可以从家里出发到达公司,是则输出YES,不能则输出NO。

示例1:

输入
2 0

5 5

..S..
****.

T.... 
****.

.....
输出

YES 

示例2:

输入

1 2

5 5

.*S*.
***** 
..*..

*****

T....
输出

NO 

说明 该用例中,至少需要拐弯1次,清除3个路障,所以无法到达

Java 代码

import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
 
class Main {
    private static final int[][] directions = {{0, 1, 1}, {0, -1, 2}, {1, 0, 3}, {-1, 0, 4}};
	private static int t, c, n, m;
    private static String[][] matrix;
    
    public static void main(String[] args) {
        // 处理输入
        Scanner in = new Scanner(System.in);
        t = in.nextInt();
        c = in.nextInt();
    
        n = in.nextInt();
        m = in.nextInt();
    
        matrix = new String[n][m];
        for (int i = 0; i < n; i++) {
            matrix[i] = in.next().split("");
        }
 
        //遍历矩阵,找到起点
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                boolean[][] visited = new boolean[m][n];
                if ("S".equals(matrix[i][j])) {
                    if (dfs(visited, i, j, 0, 0, 0)) {
                        System.out.println("YES");
                        return;
                    } else {
                        System.out.println("NO");
                        return;
                    }
                }
            }
        }
        System.out.println("NO");
        return;
    }
 
    public static boolean dfs(boolean[][] visited, int x, int y, int ut, int uc, int last_direct) {
        // 找到目的地
        if ("T".equals(matrix[x][y])) {
            return true;
        }
        //表示当前点已走过
        visited[x][y] = true;
 
        // 有四个方向选择走下一步
        for (int[] direction : directions) {
            int direct = direction[2];
            int new_x = x + direction[0];
            int new_y = y + direction[1];
        
            // 转向+破壁标记
            boolean turn_flag = false;
            boolean break_flag = false;
 
            // 越界 + 是否当前点已访问判断
            if (new_x >= 0 && new_x < n && new_y >= 0 && new_y < m && !visited[new_x][new_y]) {
 
                //转向+破壁判断
                if (last_direct != 0 && last_direct != direct) {
                    // 转向次数已用尽
                    if (ut + 1 > t) {
                        continue;
                    }
                    turn_flag = true;
                }
        
                if ("*".equals(matrix[new_x][new_y])) {
                    // 破壁次数已用尽
                    if (uc + 1 > c) {
                        continue;
                    }
                    break_flag = true;
                }
                // 可到达目的地T, 返回true
                if (dfs(visited, new_x, new_y, ut + (turn_flag ? 1 : 0), uc + (break_flag ? 1 : 0), direct)) {
                    return true;
                }
            }
        }
        return false;
    }
 
}

Python代码

import functools
import collections
import math
 
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
 
#并查集模板
class UF:
    def __init__(self, n=0):
        self.count = n
        self.item = [0 for x in range(n+1)]
        for i in range(n):
            self.item[i] = i
    def find(self, x):
        if (x != self.item[x]):
            self.item[x] = self.find(self.item[x])
            return 0
        return x
    
 
    def union_connect(self, x, y):
        x_item = self.find(x)
        y_item = self.find(y)
    
        if (x_item != y_item):
            self.item[y_item] = x_item
            self.count-=1
 
    
 
#处理输入
directions = [[0, 1,1], [0, -1,2], [1, 0,3], [-1, 0,4]]
 
# 处理输入
params = [int(x) for x in input().split(" ")]
t = params[0]
c = params[1]
params1 = [int(x) for x in input().split(" ")]
n = params1[0]
m = params1[1]
matrix = []
for i in range(n):
    matrix.append([x for x in input()])
 
def dfs(visited, x, y, ut, uc, last_direct):
    global t,c,n,m,directions
    # 找到目的地
    if ('T' == matrix[x][y]):
        return True
    
    #表示当前点已走过
    visited[x][y] = True
    #print(visited)
    # 有四个方向选择走下一步
    for direction in directions:
 
        direct = direction[2]
        new_x = x + direction[0]
        new_y = y + direction[1]
        # 转向+破壁标记
        turn_flag = False
        break_flag = False
    
        # 越界 + 是否当前点已访问判断
        if (new_x >= 0 and new_x < n and new_y >= 0 and new_y < m and not visited[new_x][new_y]):
 
            #转向+破壁判断
            if (last_direct != 0 and last_direct != direct):
                # 转向次数已用尽
                if (ut + 1 > t):
                    continue
                turn_flag = True
            
    
            if ('*' == matrix[new_x][new_y]):
                # 破壁次数已用尽
                if (uc + 1 > c):
                    continue
                break_flag = True
            
            # 可到达目的地T, 返回True
            if (dfs(visited, new_x, new_y, ut+1 if turn_flag else ut , uc+1 if break_flag else uc, direct)):
                return True
 
    return False
 
 
 
flag = True
for i in range(n):
    for j in range(m):
        visited = [[False for y in range(m)] for x in range(n)]
        if ('S' == matrix[i][j]):
            if (dfs(visited, i, j, 0, 0, 0)):
                print("YES")
                flag = False
                break
            else:
                print("NO")
                flag = False
                break
    if (not flag):
        break
 
if (flag):
    print("NO")

JS代码

let directions = [[0, 1,1], [0, -1,2], [1, 0,3], [-1, 0,4]]
let t=0
let c=0
let n=0
let m=0
function dfs(visited, x, y, ut, uc, last_direct,matrix){
    // 找到目的地
    if ('T' == matrix[x][y])
        return true
    
    //表示当前点已走过
    visited[x][y] = true
    //print(visited)
    // 有四个方向选择走下一步
    for(let direction of directions ) {
        let direct = direction[2]
        let new_x = x + direction[0]
        let new_y = y + direction[1]
        // 转向+破壁标记
        let turn_flag = false
        let break_flag = false
 
        // 越界 + 是否当前点已访问判断
        if (new_x >= 0 && new_x < n && new_y >= 0 && new_y < m && !visited[new_x][new_y]){
            
            //转向+破壁判断
            if (last_direct != 0 && last_direct != direct){
                // 转向次数已用尽
                if (ut + 1 > t)
                    continue
                turn_flag = true
            }
            
            
            if ('*' == matrix[new_x][new_y]){
                // 破壁次数已用尽
                if (uc + 1 > c)
                    continue
                break_flag = true
            }
            
            // 可到达目的地T, 返回True
            let new_ut = ut
            if (turn_flag)
                new_ut += 1
            let new_uc = uc
            if (turn_flag)
                new_uc += 1
            if (dfs(visited, new_x, new_y, new_ut, new_uc, direct,matrix))
                return true
        }
    }
 
    return false
}
 
function main(input_t,input_c,input_n,input_m,matrix_strs) {
    let matrix = []
    for (let i=0;i

你可能感兴趣的:(java,javascript,华为)