当前位置: 首页 > news >正文

泉州企业免费建站双桥seo排名优化培训

泉州企业免费建站,双桥seo排名优化培训,广东深圳华强北,长沙网站建设服务公司1、题目描述 【上班之路】 Jungle 生活在美丽的蓝鲸城,大马路都是方方正正,但是每天马路的封闭情况都不一样。 地图由以下元素组成: 1)”.” — 空地,可以达到; 2)”*” — 路障,不可达到; 3&a…

1、题目描述

【上班之路】
Jungle 生活在美丽的蓝鲸城,大马路都是方方正正,但是每天马路的封闭情况都不一样。
地图由以下元素组成:
1)”.” — 空地,可以达到;
2)”*” — 路障,不可达到;
3)”S” — Jungle的家;
4)”T” — 公司.
其中我们会限制Jungle拐弯的次数,同时Jungle可以清除给定个数的路障,现在你的任务是计算Jungle是否可以从家里出发到达公司。

【输入描述】
输入的第一行为两个整数t,c(0<=t,c<=100), t代表可以拐弯的次数,c代表可以清除的路障个数。
输入的第二行为两个整数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个路障,所以无法到达

2、解题思路

首先找到S的位置,再利用回溯算法从S位置开始遍历上、下、左、右四个方向可到达的位置,当到达公司位置T则代表可以到达公司,所有方向都遍历完成都无法到达T则无法到达公司。

3、参考代码

方法一:

import java.util.Scanner;/*** @Author Long* @Date 2023/5/3 20:45*/
public class 上班之路 {private static final int[][] directions = {{0, 1, 1}, {0, -1, 2}, {1, 0, 3}, {-1, 0, 4}};private static int maxTurns, maxClears, rows, cols;private static String[][] matrix;public static void main(String[] args) {Scanner in = new Scanner(System.in);while (in.hasNext()) {// 处理输入maxTurns = in.nextInt();maxClears = in.nextInt();rows = in.nextInt();cols = in.nextInt();matrix = new String[rows][cols];char[][] chars = new char[rows][cols];for (int i = 0; i < rows; i++) {String string = in.next();matrix[i] = string.split("");chars[i] = string.toCharArray();}for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {boolean[][] visited = new boolean[cols][rows];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 turnsUsed, int clearsUsed, int lastDirection) {if ("T".equals(matrix[x][y])) {return true;}visited[x][y] = true;for (int[] direction : directions) {int curDirection = direction[2];int newX = x + direction[0];int newY = y + direction[1];boolean turnFlag = false;boolean breakFlag = false;if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && !visited[newX][newY]) {if (lastDirection != 0 && lastDirection != curDirection) {if (turnsUsed + 1 > maxTurns) {continue;}turnFlag = true;}if ("*".equals(matrix[newX][newY])) {if (clearsUsed + 1 > maxClears) {continue;}breakFlag = true;}if (dfs(visited, newX, newY, turnsUsed + (turnFlag ? 1: 0), clearsUsed + (breakFlag ? 1 : 0), curDirection)) {return true;}}}return false;}
}

方法二:

import java.util.Scanner;/*** @Author * @Date 2023/5/3 20:45*/
public class 上班之路 {private static final int[][] directions = {{0, 1, 1}, {0, -1, 2}, {1, 0, 3}, {-1, 0, 4}};public static void main(String[] args) {Scanner in = new Scanner(System.in);while (in.hasNext()) {// 处理输入int t = in.nextInt();int c = in.nextInt();int n = in.nextInt();int m = in.nextInt();char[][] chars = new char[n][m];for (int i = 0; i < n; i++) {String string = in.next();chars[i] = string.toCharArray();}int sX = 0;int sY = 0;for (int i = 0; i < n; i++) {for (int j = 0; j < m; j++) {if (chars[i][j] == 'S') {sX = i;sY = j;break;}}if (chars[sX][sY] == 'S') {break;}}boolean[][] used = new boolean[n][m];if (chars[sX][sY] == 'S') {if (dfs(chars, sX, sY, t, c, 0, used)) {System.out.println("YES");} else {System.out.println("NO");}} else {System.out.println("NO");}}}public static boolean dfs(char[][] chars, int x, int y, int t, int c, int dis, boolean[][] used) {int n = chars.length;int m = chars[0].length;if (x < 0 || x >= n || y < 0 || y >= m) {return false;}if (chars[x][y] == 'T') {return true;}used[x][y] = true;for (int i = 0; i < directions.length; i++) {int newX = x + directions[i][0];int newY = y + directions[i][1];// 超过边界if (newX < 0 || newX >= n || newY < 0 || newY >= m || used[newX][newY]) {continue;}if (chars[newX][newY] == '*') {  // 当前是障碍if (c == 0) {  // 清除障碍数用完了continue;}if (dis == 0 || dis == directions[i][2]) {if (dfs(chars, newX, newY, t, c - 1, dis, used)) {return true;}} else {  // 上一步方向跟当前方向不一致if (t == 0) {  // 拐弯数用完continue;}if (dfs(chars, newX, newY, t - 1, c - 1, dis, used)) {return true;}}} else {if (dis == 0 || dis == directions[i][2]) {if (dfs(chars, newX, newY, t, c, dis, used)) {return true;}} else {if (t == 0) {continue;}if (dfs(chars, newX, newY, t - 1, c, dis, used)) {return true;}}}}used[x][y] = false;return false;}}

4、相似题目

(1)西天取经
(2)士兵突击

http://www.dinnco.com/news/67795.html

相关文章:

  • 在线做GO分析的网站百度云资源搜索网站
  • 股权分配方案创业初期北京百度推广优化
  • 扬州网站建设价格低手机百度云网页版登录
  • 用lnmp做网站河北网站seo策划
  • 摄影师 网站 模板seo推广专员招聘
  • 网站续费骗局游戏推广
  • app开发和网站建设区别seo问答
  • 以遇见为主题做网站软件开发自学步骤
  • 产品设计优秀网站东莞seo网站制作报价
  • 六安论坛招聘网最新招聘网站seo服务公司
  • 昌乐做网站信息流优化师招聘
  • 什么网站可以做图赚钱吗网络营销外包
  • 网站如何接入支付宝重庆seo网站哪家好
  • 福州住房建设厅网站成都seo正规优化
  • 网站还没有做解析是什么意思网站seo方案
  • 龙岗地区做网站公司北京seo网站优化培训
  • 汽车金融网站怎么做怎么让关键词快速上首页
  • 怎么做自己的简历网站百度推广后台登录
  • 有了网站源码可以做网站吗如何对一个网站进行seo
  • 洛阳霞光做网站的公司深圳app推广平台
  • b2c平台网站建设排名优化哪家好
  • php网站开发最新需求品牌策划案
  • 集团网站百度客服电话人工服务热线电话
  • 网站开发者专业优化网站排名
  • 青岛做网站建设价格陕西网站推广公司
  • 网站建设会议信息流广告投放
  • 开一家做网站公司成本某企业网站的分析优化与推广
  • 做鞋用什么网站好seo外包
  • 安新建设局网站网站有哪些平台
  • 新手学做免费网站谷歌搜索引擎入口2022