C语言/C++常见习题问答集锦(七十三) 之迷宫不能迷

C语言/C++常见习题问答集锦(七十三) 之迷宫不能迷

程序之美

在这里插入图片描述

1、c语言迷宫问题,以一个m×n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。

以一个m×n的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。注意,程序必须是非递归的。
Input
第一行输入m和n,接下来是代表迷宫的0-1矩阵,最后一行是入口和出口的坐标。注意,坐标的起始编号是0。
Output

假如存在通路,则输出Yes,否则输出No。

#define STACK_SIZE 1000
#define TRUE 1
#define FALSE 0
 
#include <stdio.h>
 
short DIRECTION_UP = 1;
short DIRECTION_RIGHT = 1 << 1;
short DIRECTION_DOWN = 1 << 2;
short DIRECTION_LEFT = 1 << 3;
short ALL_DIRECTIONS = DIRECTION_UP | DIRECTION_RIGHT | DIRECTION_DOWN | DIRECTION_LEFT;
 
typedef struct {
  int x;
  int y;
  short directions;
} Cell;
 
typedef struct {
  Cell elem[STACK_SIZE];
  int top;
} Stack;
 
void initStack(Stack *s) {
  s->top = -1;
}
 
int isEmpty(Stack *s) {
  return (s->top == -1 ? TRUE : FALSE);
}
 
void push(Stack *s, Cell e) {
  s->top++;
  s->elem[s->top] = e;
}
 
void pop(Stack *s, Cell *e) {
  *e = s->elem[s->top];
  s->top--;
}
 
int hasDirection(Cell *e, short d) {
  return (e->directions & d) != 0 ? 1 : 0;
}
 
void removeDirection(Cell *e, short d) {
  e->directions = e->directions & (~d);
}
 
int main() {
  int i, j;
  int m, n;
  int entranceX, entranceY, exitX, exitY;
  scanf("%d %d", &m, &n);
  short a[m][n];
  short visited[m][n];
  for (i = 0; i < m; i++) {
    for (j = 0; j < n; j++) {
      scanf("%d", &a[i][j]);
 
      visited[i][j] = 0;
    }
  }
  scanf("%d %d %d %d", &entranceX, &entranceY, &exitX, &exitY);
 
  Stack steps;
  initStack(&steps);
  Cell e;
  e.y = entranceX;
  e.x = entranceY;
  e.directions = ALL_DIRECTIONS;
 
  push(&steps, e);
  visited[entranceX][entranceY] = 1;
  while (!isEmpty(&steps)) {
    pop(&steps, &e);
    if (e.x == exitX && e.y == exitY) {
      push(&steps, e);
      break;
    }
 
    if (e.x > 0 && hasDirection(&e, DIRECTION_UP) && a[e.x - 1][e.y] == 0 && !visited[e.x - 1][e.y]) {
      removeDirection(&e, DIRECTION_UP);
      push(&steps, e);
      e.x--;
      e.directions = ALL_DIRECTIONS;
      removeDirection(&e, DIRECTION_DOWN);
      push(&steps, e);
      visited[e.x][e.y] = 1;
    } else if (e.y < n - 1 && hasDirection(&e, DIRECTION_RIGHT) && a[e.x][e.y + 1] == 0 && !visited[e.x][e.y + 1]) {
      removeDirection(&e, DIRECTION_RIGHT);
      push(&steps, e);
      e.y++;
      e.directions = ALL_DIRECTIONS;
      removeDirection(&e, DIRECTION_LEFT);
      push(&steps, e);
      visited[e.x][e.y] = 1;
    } else if (e.x < m - 1 && hasDirection(&e, DIRECTION_DOWN)
        && a[e.x + 1][e.y] == 0 && !visited[e.x + 1][e.y]) {
      removeDirection(&e, DIRECTION_DOWN);
      push(&steps, e);
      e.x++;
      e.directions = ALL_DIRECTIONS;
      removeDirection(&e, DIRECTION_UP);
      push(&steps, e);
      visited[e.x][e.y] = 1;
    } else if (e.y > 0 && hasDirection(&e, DIRECTION_LEFT)
        && a[e.x][e.y - 1] == 0 && !visited[e.x][e.y - 1]) {
      removeDirection(&e, DIRECTION_LEFT);
      push(&steps, e);
      e.y--;
      e.directions = ALL_DIRECTIONS;
      removeDirection(&e, DIRECTION_RIGHT);
      push(&steps, e);
      visited[e.x][e.y] = 1;
    }
  }
 
  if (isEmpty(&steps)) {
    printf("No");
  } else {
    printf("Yes");
  }
 
  return 0;
}

在这里插入图片描述

2、你来到一个迷宫前。该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数。还有若干双向道路连结这些房间,你沿着这些道路从一个房间走到另外一个房间需要一些时间。游戏规定了你的起点和终点房间,你首要目标是从起点尽快到达终点,在满足首要目标的前提下,使得你的得分总和尽可能大。现在问题来了,给定房间、道路、分数、起点和终点等全部信息,你能计算在尽快离开迷宫的前提下,你的最大得分是多少么?

Input

第一行4个整数n (<=500), m, start, end。n表示房间的个数,房间编号从0到(n - 1),m表示道路数,任意两个房间之间最多只有一条道路,start和end表示起点和终点房间的编号。

第二行包含n个空格分隔的正整数(不超过600),表示进入每个房间你的得分。

再接下来m行,每行3个空格分隔的整数x, y, z (0<z<=200)表示道路,表示从房间x到房间y(双向)的道路,注意,最多只有一条道路连结两个房间, 你需要的时间为z。

输入保证从start到end至少有一条路径。

Output

一行,两个空格分隔的整数,第一个表示你最少需要的时间,第二个表示你在最少时间前提下可以获得的最大得分。

Input示例

    3 2 0 2
    1 2 3
    0 1 10
    1 2 11

Output示例

    21 6
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ms(a) memset(a,0,sizeof(a))
#define Ms(a) memset(a,INF,sizeof(a))
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
const double E=exp(1);
const int maxn=1e3+10;
using namespace std;
int flag[maxn];
int way[maxn][maxn];
int dis[maxn];
int point[maxn];
int gg[maxn];
int n,m;
void Dijkstra(int v)
{
	memset(flag,0,sizeof(flag));
	int ans,x;
	gg[v]=point[v];
	for(int i=0;i<n;i++)
	{
		if(way[v][i]<INF)
			gg[i]=point[v]+point[i];
		dis[i]=way[v][i];
	}
	dis[v]=0;
	flag[v]=1;
	for(int i=1;i<n;i++)
	{
		x=0;
		ans=INF;
		for(int j=0;j<n;j++)
		{
			if(ans>dis[j]&&!flag[j])
			{
				ans=dis[j];
				x=j;
			}
		}
		flag[x]=1;
		for(int j=0;j<n;j++)
		{
			if(!flag[j]&&dis[j]>way[x][j]+dis[x])
			{
				dis[j]=way[x][j]+dis[x];
				gg[j]=gg[x]+point[j];
			}
		}
		for(int j=0;j<n;j++)
		{
			if(dis[j]==way[x][j]+dis[x])
				gg[j]=max(gg[j],gg[x]+point[j]);
		}
	}
}
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int start,end;
	cin>>n>>m>>start>>end;
	int x,y,z;
	ms(gg);
	for(int i=0;i<n;i++)
		cin>>point[i];
	Ms(way);
	while(m--)
	{
		cin>>x>>y>>z;
		way[x][y]=way[y][x]=min(way[x][y],z);
	}
	Dijkstra(start);
	cout<<dis[end]<<" "<<gg[end]<<endl;
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
程序在VC++ 6下顺利编译通过。 一、 实验目的: (1) 熟练掌握链栈的基本操作及应用。 (2) 利用链表作为栈的存储结构,计实现一个求解迷宫的非递归程序。 二、实验内容: 【问题描述】 以一个m×n的方阵表示迷宫,0和1分别表示迷宫通路障碍一个程序,对信任意定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。 【基本要求】 首先实现一个链表作存储结构的栈类型,然后编写一个求解迷宫的非递归程序。求得的通路以三元组(i,j,d)的形式输出,其:(i,j)指示迷宫一个坐标,d表示走到下一坐标的方向。如:对于下列数据的迷宫,输出的一条通路为:(1,1,1),(1,2,2),(2,2,2),(3,2,3),(3,1,2),……。 【测试数据】 迷宫的测试数据如下:左上角(1,1)为入口,右下角(8,9)为出口。 1 2 3 4 5 6 7 8 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0 0 0 0 0 以方阵形式输出迷宫及其通路。 输出: 请输入迷宫和宽:5 5 请输入迷宫内容: 0 1 1 0 0 0 0 1 1 0 1 0 0 1 1 1 0 0 1 0 1 1 0 0 0 迷宫的路径为 括号内的内容分别表示为(行坐标,列坐标,数字化方向,方向) (1,1,1,↓) (2,1,2,→) (2,2,1,↓) (3,2,1,↓) (4,2,2,→) (4,3,1,↓) (5,3,2,→) (5,4,2,→) (5,5,0,) 迷宫路径探索成功!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五一编程

程序之路有我与你同行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值