求助老鼠迷宮問題

Home Home
引用 | 編輯 abc10227
2010-04-11 14:50
樓主
推文 x0
編譯都可以過

要問的是 地圖是因為太大所以他一直跑嗎??


有試著把地圖改成8X8 他有跑出3種走法


可是我只要1種就好

他怎麼會一直跑哩??

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int vert;
int horiz;
}offsets;
offsets move ={
{1,0},{-1,0},{0,-1},{0,1},{1,1},{-1,1},{1,-1},{-1,-1}
};


int maze={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,0,0,1,1,0,0,0,1,1,1,1,1,1},
{1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,1},
{1,0,1,1,0,0,0,0,1,1,1,1,0,0,1,1,1},
..

訪客只能看到部份內容,免費 加入會員



獻花 x0
引用 | 編輯 su5gsm
2010-11-24 20:17
1樓
  
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
    int vert;
    int horiz;
}offsets;
offsets move[8] ={
    {1,0},{-1,0},{0,-1},{0,1},{1,1},{-1,1},{1,-1},{-1,-1}
    };


int maze[13][17]={
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,0,0,1,1,0,0,0,1,1,1,1,1,1},
        {1,1,0,0,0,1,1,0,1,1,1,0,0,1,1,1,1},
        {1,0,1,1,0,0,0,0,1,1,1,1,0,0,1,1,1},
        {1,1,1,0,1,1,1,0,0,1,1,0,1,1,0,0,1},
        {1,1,1,0,1,0,0,1,0,0,1,1,1,1,1,1,1},
        {1,0,0,1,1,0,1,1,1,0,0,0,0,0,0,1,1},
        {1,0,1,1,1,1,0,0,1,1,1,1,1,1,0,1,1},
        {1,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1},
        {1,1,1,0,0,0,1,1,0,1,1,0,0,0,0,0,1},
        {1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,0,1},
        {1,0,1,0,0,1,1,1,1,1,0,1,1,1,1,0,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
     
        };   //迷宮
       
void print_ans()
{
  int i,j;
  for(i=0;i<13;i++)
    {
    for(j=0;j<17;j++) printf("%d ",maze[j]);
    printf("\n");
    }
    printf("\n");
  return ;  

}       //印答案
     
     
     
void findpath(int row, int col)
{
  int direct,next_row,next_col,k,l;
  direct = 0;
  maze[row][col]=2;
  while(direct<8)
  {
            next_row =row + move[direct].vert;
            next_col =col + move[direct].horiz;
    if(maze[next_row][next_col]==0)
    {
      maze[next_row][next_col] =2;
      if(next_row==11 && next_col==15) print_ans();
        else {
            findpath(next_row,next_col);
            maze[next_row][next_col] = 0;
            }
    }
      direct++;
    }    
}


int main() {
    findpath(1,1);
system("pause");
  }

獻花 x0