先给不同状态的水管编给号吧

跟走迷宫一样的,深搜,每个dfs里遍历水管的状态,如果当前状态和水管指向的下一个状态能连通就深搜过去直到走到终点为止
上代码吧,这个代码是加强版,支持了更多的水管的编号,以及出口,入口和出发点是由用户输入的,以后可以用python做7k7k的水管工游戏辅助哈哈
游戏地址

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define Max 10
typedef struct suiguan {int type;int lt[4 + 1];
}sNode;
typedef struct {int x;int y;
}mNode;
mNode stack[Max*2];
int tail=0;
int m, n;
sNode map[Max + 2][Max + 2];
int book[Max + 2][Max + 2];
int ansFlag = 0;
int _x[3 + 1];
int _y[3 + 1];
int _type[3 + 1];int move[4 + 1][2] = {{ 0, 0 },
{ 0, -1 },
{ -1, 0 },
{ 0, 1 },
{ 1, 0},
};void display() {for (int i = 0; i <= m+1; i++){for (int j = 0; j <= n+1; j++){printf("%d\t",map[i][j].type);}printf("\n");}
}
sNode creat(int type) {sNode* shuiguan = (sNode*)malloc(sizeof(sNode)*(10+1));for (int i = 0; i <= 10; i++){memset(shuiguan[i].lt, 0, sizeof(int) * 5);shuiguan[i].type = i;}shuiguan[1].lt[2] = shuiguan[1].lt[3] = 1;shuiguan[2].lt[3] = shuiguan[2].lt[4] = 1;shuiguan[3].lt[1] = shuiguan[3].lt[4] = 1;shuiguan[4].lt[1] = shuiguan[4].lt[2] = 1;shuiguan[5].lt[1] = shuiguan[5].lt[3] = 1;shuiguan[6].lt[2] = shuiguan[6].lt[4] = 1;shuiguan[7].lt[1] = shuiguan[7].lt[3] = shuiguan[7].lt[4] = 1;shuiguan[8].lt[1] = shuiguan[8].lt[2] = shuiguan[8].lt[4] = 1;shuiguan[9].lt[1] = shuiguan[9].lt[2] = shuiguan[9].lt[3] = 1;shuiguan[10].lt[2] = shuiguan[10].lt[3] = shuiguan[10].lt[4] = 1;return shuiguan[type];
}
int check(int x, int y, int oldX, int oldY ,mNode* newNode,int* tailN) {int flag1 = 0;//代表着从旧节点能否到达当前节点int flag2 = 0;//代表着从当前节点能否到达旧节点int flag3 = 0;//代表着从当前节点能否到达新节点int flag4 = 0;//代表着从新节点能否到达当前节点for (int i = 1; i <= 4; i++){int temX= oldX, temY= oldY;if (map[oldX][oldY].lt[i]) {temX += move[i][0];temY += move[i][1];if (temX >= 0 && temX <= m+1 && temY >= 0 && temY <= n + 1 && map[temX][temY].type!=0 && map[temX][temY].type != -1)if (&map[temX][temY] == &map[x][y]) {flag1 = 1;break;}}}if (!flag1)return 0;for (int i = 1; i <= 4; i++) {int temX = x, temY = y;if (map[x][y].lt[i]) {temX += move[i][0]; temY += move[i][1];if (temX >= 0 && temX <= m+1 && temY >= 0 && temY <= n + 1 && map[temX][temY].type != 0 && map[temX][temY].type != -1)if (&map[temX][temY] == &map[oldX][oldY]) {flag2 = 1;}else {//如果(x,y)能与(oldX,oldY)连通,那么它有至多两个方向能到新节点//可是新节点又不一定能够和当前节点连接//但是这里不用管//现在出现了新的情况,有的水管能连通两个flag3 = 1;newNode[*tailN].x = temX; newNode[*tailN].y = temY; (*tailN)++;}}}if (!flag2)return 0;return 1;//flag3是一定成立的}
int dfs(int step, int x, int y, int oldX, int oldY);
int check2(int step,int x, int y, int oldX, int oldY) {mNode newNode[2];int tailN = 0;if (check(x, y, oldX, oldY, newNode, &tailN)) {for (int i = 0; i < tailN; i++) {int newX = newNode[i].x, newY = newNode[i].y;if (!book[newX][newY]) {book[newX][newY] = 1;stack[tail].x = newX; stack[tail].y = newY;tail++;dfs(step + 1, newX, newY, x, y);book[newX][newY] = 0;if (ansFlag)return 1;tail--;}}}
}
int dfs(int step,int x,int y,int oldX,int oldY) {if (x == _x[2] && y == _y[2]) {ansFlag = 1;for (int i = 0; i <= tail-2; i++){printf("(%d,%d)",stack[i].x, stack[i].y);}return 1;}if (ansFlag)return 1;if (map[x][y].type == 0)return 0;if (map[x][y].type <= 4) {for (int i = 1; i <= 4; i++){map[x][y] = creat(i);check2(step, x, y, oldX, oldY);}}else if (map[x][y].type <= 6) {for (int i = 5; i <= 6; i++) {map[x][y] = creat(i);check2(step, x, y, oldX, oldY);}}else if (map[x][y].type <= 10) {for (int i = 7; i <= 10; i++) {map[x][y] = creat(i);check2(step, x, y, oldX, oldY);}}}
int main(){printf("please input the map size\n");scanf("%d%d", &m, &n);printf("Please input the outlet, the first point connected with the entrance(x,y,type)3's\n");//1入口//2出口//3出发点for (int i = 1; i <= 3; i++){scanf("%d%d%d", &_x[i], &_y[i], &_type[i]);map[_x[i]][_y[i]] = creat(_type[i]);}for (int i = 1; i <= m; i++){for (int j = 1; j <= n; j++) {scanf("%d",&map[i][j].type);if (!map[i][j].type) {book[i][j] = map[i][j].type = -1;continue;}map[i][j] = creat(map[i][j].type);}}display();book[_x[3]][_y[3]] = 1;stack[tail].x = _x[3]; stack[tail].y = _y[3]; tail++;dfs(0, _x[3], _y[3], _x[1], _y[1]);if (ansFlag) {return 0;}else printf("impossible");return 0;
}

啊哈算法之水管工游戏相关推荐

  1. 啊哈算法, 水管工游戏

    游戏大致规则是:一块矩形土地被分成n*m的单位正方形,现在这块土地已经埋设了一些水管. 水管将从矩形土地的左上角左部边缘,延伸到右下角右部边缘. 水管只有两种:弯管和直管 弯管有四种状态 直管有两种状 ...

  2. 水管工游戏——dfs

    问题描述: 水管工游戏是指如下图中的矩阵中,一共有两种管道,一个是直的,一个是弯的,所有管道都可以自由旋转,最终就是要连通入水口可出水口.其中的树为障碍物. 方案: 输入格式:输入的第一行为两个整数N ...

  3. 水管工游戏 (深搜)

    水管工游戏 本题依然是采用搜索,深搜,广搜都可以,本代码采用深搜,此题在搜索时需要增加一些判断条件以及下一步要搜索的位置即可. 代码如下: #include<stdio.h> int a[ ...

  4. 深度搜索--水管工游戏

    package com.xjj.Ah;import java.util.LinkedList;/*----深度搜索--水管工游戏----* 1. remove(),removeFirst():先移除在 ...

  5. android自定义水管流动,Android水管工游戏的简单脚本

    Unity3d脚本Android水管工游戏的简单脚本 using UnityEngine; using System.Collections; public class TestGoto : Mono ...

  6. 水管工游戏(随机地图版)

    /*水管工游戏 一块快矩形的土地 被分成N*M的单位正方形 现在这个土地上已经买有一些水管   水管将从(1,1)的左上方 延伸到(N,M)d的矩形下方的右下角右部边缘 水管只有两种     &quo ...

  7. tianchai 12023 水管工游戏(DFS搜索)

    题目描述(ID:12023) 标题: 水管工游戏 详情: 这小节有点难,看不太懂可以跳过哦. 最近小哼又迷上一个叫做水管工的游戏.游戏的大致规则是这样的.一块矩形土地被分为N*M的单位正方形,现在这块 ...

  8. 啊哈算法---水管工游戏

    游戏大致规则是:一块矩形土地被分成N*M的单位正方形,现在这块土地已经埋设了一些水管. 水管将从矩形土地的左上角左部边缘,延伸到右下角右部边缘. 水管只有两种:弯管和直管 弯管有四种状态 直管有两种状 ...

  9. 2018-2-22 《啊哈,算法》再练习广度优先搜索,题:炸怪兽, 2-23改用深度优先搜索。宝岛探险(广度,深度,及地图着色)2-24水管工游戏,2-25测试水管工代码...

    2小时. 先是是纠错,通过对代码运行过程的测试.发现是变量打错.以及录入地图❌. 重构练习题,改使用while..end代替for in. ⚠️ : 在while(k <= n)中如果用到nex ...

最新文章

  1. 第五章 有限脉冲响应滤波器(ba,我终于懂FIR滤波器了)
  2. ActionForm类及表单数据验证
  3. 210305设计共享内存
  4. beta阶段——项目复审
  5. 352.将数据流变成多个不相交间隔
  6. C#使用Minidump导出内存快照Minidumper
  7. Ubuntu16安装Nvidia驱动(GTX1060显卡)
  8. m2增长率曲线_中国通胀率(中国m2历年数据曲线图)
  9. opencv读取海康威视摄像头
  10. 走进JavaWeb技术世界11:单元测试框架Junit
  11. 医学院校教师备课系统的信息安全性研究
  12. 前端,java后端开发,数据分析师应该掌握的技术,不要盲目跟风
  13. 【XLA】一、【构图阶段】图优化器的注册和执行
  14. 纸短情长 寄语青春 2021 我们毕业了!
  15. 为系统添加预装软件--雨滴应用商店APP
  16. 慧办公 EXCEL 提取表名到A列
  17. CocosCreator | 微信小游戏排行榜 微信开放域
  18. 用ChemDraw画3D图的方法
  19. 大数据时代 你的“云端”安全吗
  20. 按键精灵抓抓位置不对

热门文章

  1. 自定义iTerm2主题配置(iTerm2-Color-Schemes)
  2. Oracle数据库有哪些应用结构?
  3. vue实现消息提示框
  4. android按钮被遮住,解决Android 虚拟按键遮住了页面内容的问题
  5. 【论文阅读】Stroke Controllable Fast Style Transfer with Adaptive Receptive Fields
  6. 关于android某些手机java.lang.UnsatisfiedLinkError: No implementation found for ......的问题
  7. Linux下Makefile的automake生成全攻略[zz]
  8. 【单调栈】P4147 玉蟾宫
  9. RocketMQ实战2
  10. Windows10搭建EDK2环境