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;
}