标题:求一个问题的解……已经想了两天了……怎么都想不出来
取消只看楼主
thomascc2009
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-10-5
结帖率:0
已结贴  问题点数:20 回复次数:0 
求一个问题的解……已经想了两天了……怎么都想不出来
input 一个map(由user输入):比方说是这个样子的
“    6
      10
      ...#.....#
      #.##.##.L#
      #......#..
      ###.......
      #X....#...
      ..#..##..#

第一行是说一共有多少行,第二行是有多少列,然手‘.’是能走的地方 '#'是走不通的地方 ,'L'是初始位置,‘X’是目的地,程序的目的是打出从L走到X的路径如:
“south
  south
  west
  ...”
如果没有通路就output“no path”,想了两天怎么也想不通,逻辑十分混乱……要疯了

一下是我目前位置的可怜成果……:
“#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int x,y,i;
    int row,col;
    scanf("%d",&x);
    scanf("%d",&y);
    x=x+2;//make boundry
    y=y+2;//make boundry
    char mapLine[y];
    int map[x][y];
    int a=0;
    int b=0;
    for (row=1;row<x-1;row++)
    {
        scanf("%s",mapLine);
        for (col=1;col<y-1;col++)
        {
            map[row][col]=mapLine[col-1];
        }
    }
    // input part ends
    for (i=0;i<y-1;i++)
    {
        map[0][i]='#';
        map[x-1][i]='#';
    }
    for (i=0;i<x;i++)
    {
        map[i][0]='#';
        map[i][y-1]='#';
    }
    // make boundary
    int startx,starty,endx,endy;
    for (row=0;row<x;row++)
    {
        for (col=0;col<y;col++)
        {

            if(map[row][col]=='L')
            {
                startx=row;
                starty=col;
            }
            if(map[row][col]=='X')
            {
                endx=row;
                endy=col;
            }
        }
     }
    a=startx;
    b=starty;
    for (row=0;row<x;row++)
    {
        printf("\n");
        for (col=0;col<y;col++)
        {
            printf("%c",map[row][col]);
        }
    }
    //Print map to check
    // map staff over

    int p=1;
    int counter=0;
    int position [2500][2];
    position[0][0]=a;
    position[0][1]=b;
    position[1][0]=a;
    position[1][1]=b;
    map[startx][starty]='.';
    map[endx][endy]='.';
    int checker=1;
    printf("\n%d %d",a,b);
    while ((a==endx&&b==endy)==0)
    {
        if (counter!=0&&p>=3)
        {
            p--;
            map[a][b]='#';
            a=position[p][0];
            b=position[p][1];
            counter=0;
        }
        else
        {
            if (counter!=0)
            {
                if (position[p-1][0]==)
                switch(map[a-1][b])
                {
                    case('.'):
                    a--;
                    counter=0;
                    p++;
                    position[p][0]=a;
                    position[p][1]=b;
                    break;
                    case('#'):
                    checker=1;
                    break;
                }
            }
            if (counter!=0)
            {
                switch(map[a+1][b])
                {
                    case('.'):
                    a++;
                    counter=0;
                    p++;
                    position[p][0]=a;
                    position[p][1]=b;
                    break;
                    case('#'):
                    checker=2;
                    break;
                }
            }
            if (counter!=0)
            {
                switch(map[a][b-1])
                {
                    case('.'):
                    b--;
                    counter=0;
                    p++;
                    position[p][0]=a;
                    position[p][1]=b;
                    break;
                    case('#'):
                    checker=3;
                    break;
                }
            }
            if (counter!=0)
            {
                switch(map[a][b+1])
                {
                    case('.'):
                    b++;
                    counter=0;
                    p++;
                    position[p][0]=a;
                    position[p][1]=b;
                    break;
                    case('#'):
                    checker=4;
                    break;
                }
            }
        }
    }

printf("\n%d %d",a,b);
return 0;
}

”感谢各位了……

搜索更多相关主题的帖子: 通路 
2010-10-05 10:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-322014-1-1.html




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

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