【算法】程序猿不写代码是不对的64
来源:互联网 发布:性用品淘宝 编辑:程序博客网 时间:2024/06/08 07:52
package com.kingdz.algorithm.time201705;/** * <pre> * 水管工问题 * 1代表从上到右,2代表从下到右,3代表从下到左,4代表从上到左,5代表从左到右,6代表从上到下 * 表示水管的状态,给出入口和出口位置,转动水管 * </pre> * * @author kingdz * */public class Algo31 {private static int[][] source;private static int[][] mark = new int[51][51];private static int targetX;private static int targetY;// 标记是否已经结束private static int flag = 0;private static Node[] list = new Node[100];;private static int top = 0;public static void main(String[] args) {targetX = 5;targetY = 4;source = new int[][] { { 5, 3, 5, 3 }, { 1, 5, 3, 0 }, { 2, 3, 5, 1 }, { 6, 1, 1, 5 }, { 1, 5, 5, 4 } };dfs(0, 0, 1);for (int i = 0; i < list.length; i++) {if (list[i] == null) {break;}System.out.println(list[i]);}};public static void dfs(int x, int y, int front) {if (flag == 1) {return;}if (x == targetX - 1 && y == targetY) {flag = 1;System.out.println("找到铺设方案");return;}if (x < 0 || x >= source.length || y < 0 || y >= source[0].length) {// 越界return;}if (mark[x][y] == 1) {// 已经使用过该水管return;}mark[x][y] = 1;list[top] = new Node(x, y);top++;if (source[x][y] >= 5 && source[x][y] <= 6) {// 当前水管是直管if (front == 1) {// 进水口在左边dfs(x, y + 1, 1);}if (front == 2) {// 进水口在上边dfs(x + 1, y, 2);}if (front == 3) {// 进水口在右边dfs(x, y - 1, 3);}if (front == 4) {// 进水口在下边dfs(x - 1, y, 4);}}if (source[x][y] >= 1 && source[x][y] <= 4) {// 当前水管是弯管if (front == 1) {// 进水口在左边dfs(x + 1, y, 2);dfs(x - 1, y, 4);}if (front == 2) {// 进水口在上边dfs(x, y + 1, 1);dfs(x, y - 1, 3);}if (front == 3) {// 进水口在右边dfs(x - 1, y, 4);dfs(x + 1, y, 2);}if (front == 4) {// 进水口在下边dfs(x, y + 1, 1);dfs(x, y - 1, 3);}}mark[x][y] = 0;top--;return;}private static class Node {int x;int y;public Node(int x, int y) {super();this.x = x;this.y = y;}@Overridepublic String toString() {return "Node [x=" + x + ", y=" + y + "]";}}}
阅读全文
0 0
- 【算法】程序猿不写代码是不对的64
- 【算法】程序猿不写代码是不对的1
- 【算法】程序猿不写代码是不对的2
- 【算法】程序猿不写代码是不对的3
- 【算法】程序猿不写代码是不对的4
- 【算法】程序猿不写代码是不对的5
- 【算法】程序猿不写代码是不对的6
- 【算法】程序猿不写代码是不对的7
- 【算法】程序猿不写代码是不对的8
- 【算法】程序猿不写代码是不对的9
- 【算法】程序猿不写代码是不对的10
- 【算法】程序猿不写代码是不对的11
- 【算法】程序猿不写代码是不对的12
- 【算法】程序猿不写代码是不对的13
- 【算法】程序猿不写代码是不对的14
- 【算法】程序猿不写代码是不对的15
- 【算法】程序猿不写代码是不对的16
- 【算法】程序猿不写代码是不对的17
- Java实例--汇总
- 图像风格转移中神经网络模型的使用
- 5.31 JLL--实习日志--EDIT 过期数据+2个问题
- 订单历史记录
- hibernate-关系映射-多对多(双向)-12
- 【算法】程序猿不写代码是不对的64
- 阿里云+wordpress搭建个人博客网站【小白专用的图文教程】
- C语言之文件的加密与解密
- thinkphp3.2.3配置
- SpringMVC获取页面数据乱码的解决get/post
- Android 科大讯飞语音集成,文字转语音
- 大数据:Unity3D职位统计(20170531)
- android Okhttp3的基本使用
- HTML学习---------1.5认识head标签