poj3984迷宫问题

这题有两个解法。
先看dfs,其实这个题用了一点技巧,认为向右向下即为最小路径。
所以其实就是简单的dfs再用向量存储一下路径
几个注意点:
1.定义char mp作为地图的话,判断的时候用‘1’不要用1啊。这个真的无语
2.开数组范围(由于我喜欢从1-n记录,所以数组要开大一个
3.毒瘤输入
见代码



for(int i=1;i<=5;i++)

       
for(int j=1;j<=5;j++)

       
{scanf("%c",&mp[i][j]);getchar();}


以下为ac代码



#include<iostream>

#include<stdio.h>

#include<vector>

using namespace std;

#include<string.h>

const int maxn=6;//erwei 40 yiwei 80

int w,h,fx,fy,ans;

char mp[maxn][maxn];

int flag[maxn][maxn];

int dir[4][2]={1,0,0,1,-1,0,0,-1};

struct node

{

   
int x,y;

   
//node(int xx,int yy){x=xx;y=yy;}

};

vector<node> path;

bool pd(int x,int y)

{

   
//cout<<x<<y<<mp[x][y]<<flag[x][y]<<endl;

   
return(x>=1)&&(x<=5)&&(y>=1)&&(y<=5)&&(!flag[x][y])&&(mp[x][y]!='1');

}

 

int dfs(int x,int y)

{

   
int nx,ny;

   
node tmp;

   
tmp.x=x,tmp.y=y;

   
path.push_back(tmp);

   
if(x==5&&y==5)

    {

       
return 1;

    }

   
for(int e=0;e<4;e++)

   
{nx=x+dir[e][0];

    
ny=y+dir[e][1];

    
if(pd(nx,ny))

    
{

        
flag[nx][ny]=1;

        
if(dfs(nx,ny))return 1;

        
path.pop_back();

        
flag[nx][ny]=0;

    
}

}

 

return 0;

}

int main()

{

       
memset(flag,0,sizeof(flag));

       
memset(mp,0,sizeof(mp));

       
for(int i=1;i<=5;i++)

       
for(int j=1;j<=5;j++)

       
{scanf("%c",&mp[i][j]);getchar();}

       
//cin>>mp[i]+1;

       
dfs(1,1);

       
for(int i=0;!path.empty();i++)

           
{printf("(%d, %d)\n",path[i].x-1,path[i].y-1);

           
path.pop_back();}

}


这题bfs是正解。一般来说bfs做最短路径。dfs需要通过比较每一次路径的长短来的姐,但是bfs是每一层逐渐向目的点走,所以先到的一定是最短的,所以直接就可以得到正确答案。由于这个题太简单了(而且很容易投机取巧,就不写这题的代码了)放个模板吧



#include<iostream>//(10.16)

#include<string.h>

#include<queue>

using namespace std;

struct node

{

   
int x,y;

   
node(int c=0,int a=0):x(c),y(a){};

};const int edge=5;/*几阶矩阵*/

int fx=0,fy=0;/*起点坐标*/

int ex=4,ey=4;/*终点坐标*/

int arc[edge][edge];

int dis[edge][edge];

node path[edge][edge];

int dir[4][2]={0,1,1,0,-1,0,0,-1};

bool pd(int x,int y)

{

   
return (x<edge&&y<edge&&x>=0&&y>=0/*障碍与访问的判定&&!arc[x][y]&&dis[x][y]==-1*/);

}

void bfs()

{

   
queue<node>a;

   
a.push(node(fx,fy));

   
memset(dis,-1,sizeof(dis));

   
dis[fx][fy]=0;

   
while(!a.empty())

    {

       
node cur=a.front();a.pop();

       
for(int e=0;e<4;e++)

       
{

           
int dx=cur.x+dir[e][0];

           
int dy=cur.y+dir[e][1];

           
if(pd(dx,dy))

           
{

               
dis[dx][dy]=dis[cur.x][cur.y]+1;

                path[dx][dy]=cur;

                a.push(node(dx,dy));

           
}

       
}

    }

}

void coutpath(node cur)

{

   
if(cur.x==fx&&cur.y==fy)

       
{cout<<"("<<fx<<",
"<<fy<<")"<<endl;

       
return;}

       
coutpath(path[cur.x][cur.y]);

       
cout<<"("<<cur.x<<",
"<<cur.y<<")"<<endl;

}

int main()

{

   
for(int w=0;w<edge;w++)

       
for(int e=0;e<edge;e++)

    {

       
cin>>arc[w][e];

    }bfs();

coutpath(node(ex,ey));

cout<<dis[ex][ey]<<endl;(如果有7个坐标那么此处为6)

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以得知这是一道关于迷宫问题的题目,需要使用Java语言进行编写。具体来说,这道题目需要实现一个迷宫的搜索算法,找到从起点到终点的最短路径。可以使用广度优先搜索或者深度优先搜索算法来解决这个问题。 下面是一个使用广度优先搜索算法的Java代码示例: ```java import java.util.*; public class Main { static int[][] maze = new int[5][5]; // 迷宫地图 static int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // 方向数组 static boolean[][] vis = new boolean[5][5]; // 标记数组 static int[][] pre = new int[5][5]; // 记录路径 public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { maze[i][j] = sc.nextInt(); } } bfs(0, 0); Stack<Integer> stack = new Stack<>(); int x = 4, y = 4; while (x != 0 || y != 0) { stack.push(x * 5 + y); int t = pre[x][y]; x = t / 5; y = t % 5; } stack.push(0); while (!stack.empty()) { System.out.print(stack.pop() + " "); } } static void bfs(int x, int y) { Queue<Integer> qx = new LinkedList<>(); Queue<Integer> qy = new LinkedList<>(); qx.offer(x); qy.offer(y); vis[x][y] = true; while (!qx.isEmpty()) { int tx = qx.poll(); int ty = qy.poll(); if (tx == 4 && ty == 4) { return; } for (int i = 0; i < 4; i++) { int nx = tx + dir[i][0]; int ny = ty + dir[i][1]; if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5 && maze[nx][ny] == 0 && !vis[nx][ny]) { vis[nx][ny] = true; pre[nx][ny] = tx * 5 + ty; qx.offer(nx); qy.offer(ny); } } } } } ``` 该代码使用了广度优先搜索算法,首先读入迷宫地图,然后从起点开始进行搜索,直到找到终点为止。在搜索的过程中,使用标记数组记录已经访问过的位置,使用路径数组记录路径。最后,使用栈来输出路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值