日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

微信小程序:一起玩连线,一个算法来搞定

發(fā)布時(shí)間:2025/3/8 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 微信小程序:一起玩连线,一个算法来搞定 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

微信小程序:一起玩連線

游戲玩法

  將相同顏色的結(jié)點(diǎn)連接在一起,連線之間不能交叉。

  

?

算法思想

  轉(zhuǎn)換為多個(gè)源點(diǎn)到達(dá)對(duì)應(yīng)終點(diǎn)的路徑問題,且路徑之間不相交。按照dfs方式尋找兩個(gè)結(jié)點(diǎn)路徑,一條路徑探索完之后,標(biāo)記地圖并記錄路徑,然后探索下一條路徑,以此類推。路徑探索失敗之后,地圖進(jìn)行標(biāo)記回退,路徑也回退。

import com.sun.org.apache.xml.internal.serialize.LineSeparator; import java.util.*; import java.util.stream.IntStream;/*** @author hujunzheng* @create 2018-07-01 16:12**/ public class LineTogether {private static final int[][] dir = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};private boolean[][] map;private List<TogetherNode> togetherNodes;public LineTogether() {Scanner scanner = new Scanner(System.in);int width = scanner.nextInt();int height = scanner.nextInt();int pairNodes = scanner.nextInt();this.map = new boolean[width + 2][];for (int i = 0; i < width + 2; ++i) {map[i] = new boolean[height + 2];}for (int i = 1; i <= width; ++i) {for (int j = 1; j <= height; ++j) {map[i][j] = true;}}togetherNodes = new ArrayList<>();IntStream.range(0, pairNodes).forEach(i -> {int bx = scanner.nextInt();int by = scanner.nextInt();map[bx][by] = false;Node begin = new Node(bx, by);int ex = scanner.nextInt();int ey = scanner.nextInt();map[ex][ey] = false;Node end = new Node(ex, ey);togetherNodes.add(new TogetherNode(begin, end));});this.printMap();}public void resolve() {if (this.togetherNodes.size() == 0) {return;}Node begin = togetherNodes.get(0).begin;Node end = togetherNodes.get(0).end;boolean success = this.process(0, begin.x, begin.y, end.x, end.y);System.out.println(success ? "路徑探測成功" : "路徑探測失敗");}private void printMap() {StringBuilder result = new StringBuilder();for (int i = 0; i < map.length; ++i) {for (int j = 0; j < map[i].length; ++j) {result.append(map[i][j] ? 1 : 0).append(" ");}result.append(LineSeparator.Windows);}System.out.println(result.toString());}private boolean process(int ix, int bx, int by, int ex, int ey) {//如果 map[bx][by] == false, 說明是端點(diǎn)boolean endpoint = !map[bx][by];map[bx][by] = false;List<Node> path = togetherNodes.get(ix).path;path.add(new Node(bx, by));//到達(dá)終點(diǎn)if (bx == ex && by == ey) {if (ix + 1 >= togetherNodes.size()) {return true;}Node begin = togetherNodes.get(ix + 1).begin;Node end = togetherNodes.get(ix + 1).end;//下一個(gè)路徑探索boolean success = this.process(ix + 1, begin.x, begin.y, end.x, end.y);if (success) return success;} else {for (int i = 0; i < dir.length; ++i) {int nextx = bx + dir[i][0];int nexty = by + dir[i][1];//如果節(jié)點(diǎn)標(biāo)記為false,并且節(jié)點(diǎn)不是終節(jié)點(diǎn)的時(shí)候if (!map[nextx][nexty] && !(nextx == ex && nexty == ey)) {continue;}boolean success = this.process(ix, nextx, nexty, ex, ey);if (success) return true;}}if (!endpoint) {map[bx][by] = true;}path.remove(path.size() - 1);return false;}public String fetchResult() {if (togetherNodes.size() == 0) {return "";}StringBuilder result = new StringBuilder();togetherNodes.stream().map(TogetherNode::getPath).forEach(path -> {for (Iterator<Node> it = path.iterator(); it.hasNext(); ) {Node node = it.next();result.append("(").append(node.x).append(":").append(node.y).append(")");if (it.hasNext()) {result.append("->");}}result.append(LineSeparator.Windows);});return result.toString();}private class Node {public int x;public int y;public Node(int x, int y) {this.x = x;this.y = y;}}private class TogetherNode {public Node begin;public Node end;private List<Node> path;public TogetherNode(Node begin, Node end) {this.begin = begin;this.end = end;path = new ArrayList<>();}public List<Node> getPath() {return path;}}public static void main(String[] args) {LineTogether lt = new LineTogether();lt.resolve();System.out.println(lt.fetchResult());} }

輸入數(shù)據(jù)

5 5 5 3 1 1 4 4 1 4 5 4 2 2 3 4 4 5 5 1 5 2 4

輸出數(shù)據(jù)

路徑探測成功 (3:1)->(2:1)->(1:1)->(1:2)->(1:3)->(1:4) (4:1)->(5:1)->(5:2)->(5:3)->(4:3)->(3:3)->(3:4)->(3:5)->(4:5) (4:2)->(3:2)->(2:2)->(2:3) (4:4)->(5:4)->(5:5) (1:5)->(2:5)->(2:4)

操作效果

  

?

轉(zhuǎn)載于:https://www.cnblogs.com/hujunzheng/p/9253505.html

總結(jié)

以上是生活随笔為你收集整理的微信小程序:一起玩连线,一个算法来搞定的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。