搜索
查看: 724|回复: 3
打印 上一主题 下一主题

请大家帮忙改正一下我的程序,谢谢!!!!!

[复制链接]
跳转到指定楼层
楼主
发表于 2015-12-3 20:04:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
5啊哈币
char A[64][64]= {"..###","#....","#.#.#","#.#.#","#.#.."};//迷宫,A,R,C这里预设,实际请改成输入
int M[64][64] = {0},    //标记走过的点
R = 5, C = 5;      

//判断点(x,y)是否可达
bool pass(int x, int y)
{
return x>=0 && x<=R && y>=0 && y<=C
  && A[x][y]=='.' && !M[x][y];
}
//广度搜索
int steps()
{
struct{ int x, y, depth;}Queue[256], t;    //队列
int front = 0, rear = 0,    //头尾指标
  di[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};    //方向数组
int i, new_x, new_y;


Queue[rear].x = 0;     //初始点入队
Queue[rear].y = 0;
Queue[rear++].depth = 1;
M[0][0] = 1;    //标记该店


while(front != rear)
{
  t = Queue[front++];        //出队
  for(i=0; i<4; i++)        //遍历每个方向
  {
   new_x = t.x+di[i][0];    //产生新的坐标
   new_y = t.y+di[i][1];
   if(pass( new_x, new_y))    //若可达
   {
    if(new_x==R-1 && new_y==C-1)return t.depth+1;    //结束条件
    //入队
    Queue[rear].x = new_x;   
    Queue[rear].y = new_y;
    Queue[rear++].depth = t.depth+1;
    M[new_x][new_y] = 1;    //同样标记入队的点
   }
  }
}
return -1;    //无法走到终点,返回-1
}
int main()
{
printf("%d", steps());
}

沙发
发表于 2016-2-8 10:45:52 | 只看该作者
#include不见了
板凳
发表于 2016-2-8 14:11:18 | 只看该作者
程序return了个1
[mw_shl_code=cpp,true]
#include <cstdio> //这个不见了
char A[64][64]= {
"..###",
"#....",
"#.#.#",
"#.#.#",
"#.#.."};//迷宫,A,R,C这里预设,实际请改成输入
int M[64][64] = {0},    //标记走过的点
R = 5, C = 5;      

//判断点(x,y)是否可达
bool pass(int x, int y)
{
return x>=0 && x<=R && y>=0 && y<=C
   && A[x][y]=='.' && !M[x][y];
}
//广度搜索
int steps()
{
struct{ int x, y, depth;}Queue[256], t;    //队列
int front = 0, rear = 0,    //头尾指标
  di[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};    //方向数组
int i, new_x, new_y;


Queue[rear].x = 0;     //初始点入队
Queue[rear].y = 0;
Queue[rear++].depth = 1;
M[0][0] = 1;    //标记该店


while(front != rear)
{
   t = Queue[front++];        //出队
  for(i=0; i<4; i++)        //遍历每个方向
  {
    new_x = t.x+di[0];    //产生新的坐标
   new_y = t.y+di[1];
    if(pass( new_x, new_y))    //若可达
   {
     if(new_x==R-1 && new_y==C-1)return t.depth+1;    //结束条件
    //入队
    Queue[rear].x = new_x;   
     Queue[rear].y = new_y;
     Queue[rear++].depth = t.depth+1;
     M[new_x][new_y] = 1;    //同样标记入队的点
   }
   }
}
return -1;    //无法走到终点,返回-1
}
int main()
{
printf("%d\n", steps());
return 0;
}
[/mw_shl_code]
地板
发表于 2016-2-8 14:13:04 | 只看该作者
请使用G++编译
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表