发现离散数学的小作用

迷宫程序

点击查看迷宫程序代码
#include<stdio.h>
#include <stdlib.h>

/*
	迷宮求解 
*/
struct Node {
	int x;
	int y;
	struct Node* prior;
	struct Node* next;
};

struct Stack{
	Node* bottom;
	Node* top;
	int stacksize;
};

// 初始化栈 
Stack* init(); 
// 入栈
void push(Stack* s, int x, int y);
// 出栈
void pop(Stack* s);
// 显示栈中数据 
void showStack(Stack* stack);
int x,y;
int map[10][10] = {
		{1,1,1,1,1,1,1,1,1,1},
		{1,2,1,0,0,0,0,0,1,1},
		{1,0,1,1,1,1,1,0,1,1},
		{1,0,0,0,1,0,1,0,0,1},
		{1,0,1,0,1,0,0,0,0,1},
		{1,0,1,0,1,1,1,1,1,1},
		{1,1,1,0,1,0,0,0,1,1},
		{1,0,0,0,1,0,1,0,0,1},
		{1,1,0,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1}
	};
Stack* stack;
int right();
int left();
int up();
int down();

int main() {
	
	x = 1;
	y = 1;
	stack = init();
	//x != 8 && y != 8 错误 
	// x != 8 || y != 8 正确
	// x == 8 && y == 8 正确 
	int flag = 1;
	while (flag) {
		printf("\n");
		if (right()) {
			printf("右走\n");
		} else if (left()) {
			printf("左走\n");
		}else if (up()) {
			printf("上走\n");
		}else if (down()) {
			printf("下走\n");
		}else {
			printf("出栈\n");
			pop(stack);
		}
		
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				int n = map[i][j];
				printf(" %d ", n);
			}
			printf("\n");
		}
		if (x == 8 && y == 8) {
			flag = 0;
		}
		
//		if (right() || left()
//			|| up() || down()) {
//			printf("下一步\n");
//		} else {
//			printf("出栈\n");
//			pop(stack);
//		}
	} 
	return 0;
}

int right() {
	if (map[x][y + 1] != 1 && map[x][y + 1] != 2) {
		y += 1;
		map[x][y] = 2;
		push(stack, x, y);
		return 1;
	}
	return 0;
}
int left() {
	if (map[x][y - 1] != 1 && map[x][y - 1] != 2) {
		y -= 1;
		map[x][y] = 2;
		push(stack, x, y);
		return 1;
	}
	return 0;
}
int up() {
	if (map[x - 1][y] != 1 && map[x - 1][y] != 2) {
		x -= 1;
		map[x][y] = 2;
		push(stack, x, y);
		return 1;
	}
	return 0;
}
int down(){
	if (map[x + 1][y]!= 1 && map[x + 1][y] != 2) {
		x += 1;
		map[x][y] = 2;
		printf("下一步地址;%d", map[x][y]); 
		push(stack, x, y);
		return 1;
	}
	return 0;
}



// 初始化栈 
Stack* init() {
	Stack* stack = (Stack*)malloc(sizeof(Stack));
	stack->stacksize = 100;
	
	Node* head = (Node*)malloc(sizeof(Node));
	head->next = head; 
	head->prior = head;
	stack->bottom = head;
	stack->top = head;
	for (int i = 0; i < stack->stacksize; i++ ) {
		Node* newNode = (Node*)malloc(sizeof(Node));
		newNode->next = head->next;
		head->next->prior = newNode;
		newNode->prior = head;
		head->next = newNode;
	}
	return stack;
}
// 入栈
void push(Stack* stack, int x, int y) {
	if (stack->top->next != stack->bottom) {
		
		stack->top =  stack->top->next;
		stack->top->x = x;
		stack->top->y = y;
	} else {
		printf("栈满!\n"); 
	}
}
// 出栈
void pop(Stack* stack) {
	if (stack->top != stack->bottom) {
		
		stack->top =  stack->top->prior;
		x = stack->top->x;
		y = stack->top->y;
	} else {
		printf("栈空!\n"); 
	}
}

// 显示栈中数据 
void showStack(Stack* stack) {
	if (stack->bottom == stack->top) {
		printf("栈为空!");
	}
	else {
		Node* p =  stack->bottom->next;
		printf("当前栈数据为:");
		while (p != stack->top->next) {
			printf("%d  ", p->x);
			printf("%d  ", p->y);
			p = p->next;
		}
	}
	printf("\n");
	printf("\n");
}

判断是否到达终点时发生错误

为真:没到达终点
为假:到达终点

x != 8 && y != 8 错误
x != 8 || y != 8 正确
!(x == 8 && y == 8) 正确

如果是第一个条件,则当x为8或y为8的时候都没到达终点。第九行和第九列都无法到达。
如果是第二个条件或第三个条件,意思是:当x和y都为8的时候到达终点

发现的技巧

x != 8 && y != 8
的意思直接理解起来可能会理解错误,或者不好理解
这时候我们可以使用离散数学中的数理逻辑进行等价转换

设A:x等于8 设B:y等于8

x != 8 && y != 8
符号化为:┐A ∧ ┐B <=> ┐(A ∨ B) 
意思为:x等于8或y等于8为假时,到达终点
代表第九行和第九列都是终点,而不是(88)这个位置是终点

!(x == 8 && y == 8)
这个就比较好理解
意思为:x等于8并且y等于8为假,到达终点
可以等价于:┐(A ∧ B) <=> ┐A ∨ ┐B
意思为:x不等于8或者y不等于8为真

总结

可以使用等价代换去检查判断条件是否正确,通过不同的表达方式去理解判断条件的意思。从而检查我们是否理解错误判断条件的意思。

posted @   Dragon-Li  阅读(105)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示