| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2669 人关注过本帖
标题:请教:迷宫问题
只看楼主 加入收藏
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
结帖率:79.37%
收藏
已结贴  问题点数:100 回复次数:28 
请教:迷宫问题
题目描述
总算期中考了,鄙人被教育局分配到了SY学校,当然是陪着很多人的。不知转了多少次车,总算到了。可惜的是,SY学校整个像个迷宫一样,就在门口贴了张学校地图。鄙人就开始研究地图了,但是学校错综复杂,等找到目的地,早就开考了。为此,鄙人取出随身携带的微型电脑(不知道从哪来的),向网上发去了求助书。注:只能往4个方向走:上、下、左、右。

输入格式
第1行,二个数,N,M。
接下来是一个N*M的矩阵,表示这个学校。(有N行,M列)。矩阵由2个数字组成。0:路;1:墙。路能走,墙不能走(这是基本常识。不过还是提醒一下,不然哪个牛又要飞檐走壁了)。
再是2行,第1行2个数X1,Y1表示校门口的坐标(即校门口在矩阵的第X1行,第Y1列)。第2行2个数X2,Y2表示鄙人的考场的坐标(即校门口在矩阵的第X2行,第Y2列)。
数据范围:0<M,N<=2000。0〈X1,X2〈=N,0〈Y1,Y2〈=M。


输出格式
一个数,表示最少要走的步数。如果走不到,则输出 No Answer!

样例输入
5 5
1 1 1 1 1
1 1 1 0 0
1 0 0 0 1
0 0 1 0 0
1 1 1 0 1
4 1
5 4
样例输出
6

我的代码有点问题:
CODE:
#include <stdio.h>
#include <stdlib.h>
int maze[100][100];
static int step=0;
int endx,endy,m,n;
int flag;
int visit(int startx,int starty)
{
    //i=startx,j=starty  
    maze[startx][starty]=2;
    if(startx==endx&&starty==endy)
        flag=1;
    if(maze[startx][starty+1]
==0&&starty<n&&flag==0)//往右
    {
        if(maze[startx][starty+1]==2)
            step--;
        else
            step++;
        visit(startx,starty+1);
    }
    if(maze[startx+1][starty]
==0&&startx<m&&flag==0)//往下
    {
        if(maze[startx+1][starty]==2)
            step--;
        else
            step++;
        visit(startx+1,starty);
    }
    if(maze[startx][starty-1]
==0&&starty>1&&flag==0)//往左
    {
        if(maze[startx][starty-1]==2)
            step--;
        else
            step++;
        visit(startx,starty-1);
    }
    if(maze[startx-1][starty]
==0&&startx>1&&flag==0)//往上
    {
        if(maze[startx-1][starty]==2)
            step--;
        else
            step++;
        visit(startx-1,starty);
    }
    return step;     
}
void print_maze(int m,int n)
{
    int i,j;
    printf("print the maze map:\n");
    for(i=0;i<m;i++)
        for(j=0;j<n;j++)
    {
    if(j%5==0)
    printf("\n");
    printf("%d ",maze[i][j]);
    }
    system("pause");
}
int main()
{
    int i,j;
    int startx,starty;//起始坐标与结束坐

    scanf("%d %d",&m,&n);// 输入迷宫的行

    for(i=1;i<=m;i++)
    for(j=1;j<=n;j++)
    scanf("%d",&maze[i][j]);//输入迷宫数

    //print_maze(m,n);//测试用,打印迷宫
    scanf("%d %d",&startx,&starty);
    scanf("%d %d",&endx,&endy);
    if(visit(startx,starty)==0)
    printf("No Answer!\n");
    else
        printf("step=%d",step);
    system("pause");
    return 0;
}
搜索更多相关主题的帖子: 迷宫 
2010-11-21 13:43
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:8 
这个帖子好没人气呀~~
2010-11-21 20:14
zdyzhang
Rank: 9Rank: 9Rank: 9
来 自:栖息地
等 级:蜘蛛侠
威 望:4
帖 子:2335
专家分:1227
注 册:2008-9-20
收藏
得分:8 
帮顶下吧。

悲剧源于生活。
2010-11-21 23:00
袁野
Rank: 2
来 自:湖北省
等 级:论坛游民
帖 子:45
专家分:56
注 册:2010-10-13
收藏
得分:8 
怪事
我第一遍输入出结果,但结果是错的
后来就不行了
始终提示程序已停止工作
2010-11-22 14:07
huliangmao
Rank: 2
等 级:论坛游民
帖 子:26
专家分:53
注 册:2010-11-17
收藏
得分:8 
我想知道你在第一个函数中定义的flag是什么意思?
2010-11-22 16:23
huliangmao
Rank: 2
等 级:论坛游民
帖 子:26
专家分:53
注 册:2010-11-17
收藏
得分:0 
输入
2 2
1 0
0 0
2 1
1 2
时输出是对的
2
这是为什么??
输入
2 3
0 0 1
1 0 0
1 1
2 3时结果也对
反复试了几次
问题应该出在一个零周围有三个或四个是零的地方
2010-11-22 16:44
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:0 
总算AC了,CODE:
#include <stdio.h>
#include <stdlib.h>
int maze[100][100];
static int step=0;
int endx,endy,m,n;
int flag;
int minstep = 100000;
void  visit(int startx,int starty)
{
 //i=startx,j=starty
 maze[startx][starty]=2;
 step++;
 if(startx==endx&&starty==endy) {
  flag=1;//actually useless
  if (step<minstep)
    minstep = step;
 }
 
 if(maze[startx][starty+1]==0&&starty<n)//往右
 {
  visit(startx,starty+1);
 }
 if(maze[startx+1][starty]==0&&startx<m)//往下
 {
  visit(startx+1,starty);
 }
 if(maze[startx][starty-1]==0&&starty>1)//往左
 {
  visit(startx,starty-1);
 }
 if(maze[startx-1][starty]==0&&startx>1)//往上
 {
  visit(startx-1,starty);
 }
 maze[startx][starty]=0;
 step--;
}
void print_maze(int m,int n)
{
 int i,j;
 printf("print the maze map:\n");
 for(i=0;i<m;i++)
  for(j=0;j<n;j++)
 {
 if(j%5==0)
 printf("\n");
 printf("%d ",maze[i][j]);
 }
 system("pause");
}
int main()
{
 int i,j;
 int startx,starty;//起始坐标与结束坐标
 scanf("%d %d",&m,&n);// 输入迷宫的行列
 for(i=1;i<=m;i++)
 for(j=1;j<=n;j++)
 scanf("%d",&maze[i][j]);//输入迷宫数据
 //print_maze(m,n);//测试用,打印迷宫
 scanf("%d %d",&startx,&starty);
 scanf("%d %d",&endx,&endy);
 visit(startx,starty);
 if (!flag)//could instead check 'if (minstep==100000)'
     printf("No Answer!\n");
 else
  printf("%d",minstep-1);//think why?
 return 0;
}

欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-11-22 18:22
jack10141
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:陕西西安
等 级:小飞侠
威 望:6
帖 子:706
专家分:2271
注 册:2010-8-10
收藏
得分:8 
顶一个!!呵呵 AC 了就好!

最近比较忙!上来的时间少了!大家还是很有劲头的啊!!加油!

sunyh1999 在哪里找那么多中文题目的?我英文不好,可否给我说说你做中文OJ题目的网址?

Coding就像一盒巧克力,你永远不会知道你会遇到什么BUG
别跟我说你是不能的,这让我愤怒,因为这侮辱了你的智慧
2010-11-22 21:25
fightingsss
Rank: 6Rank: 6
等 级:侠之大者
帖 子:97
专家分:471
注 册:2010-11-12
收藏
得分:8 
强大!
2010-11-23 09:07
sunyh1999
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:14
帖 子:1178
专家分:3032
注 册:2009-5-17
收藏
得分:0 
回复 8楼 jack10141
好啊,网站 :www.

欢迎来到我的博客:http://blog..cn/noisunyuhong
2010-11-23 10:25
快速回复:请教:迷宫问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018402 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved