码迷,mamicode.com
首页 > 其他好文 > 详细

走迷宫

时间:2015-01-15 22:12:20      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

1.迷宫中是否存在一条路从(x1,y1) (x2,y2), 矩阵为10  

#include <iostream> 
#include <cstdio>
#include <cstring>
using namespace std;
int t,n,m,x1,y1,x2,y2;
int in[1000][1000];
int mx[4]={-1,1,0,0};
int my[4]={0,0,-1,1};

int dfs(int x,int y) //深搜
{
    if(x==x2&&y==y2)
      return 1;
    for(int i=0;i<4;i++)
    {
        int x3 = x+mx[i];
        int y3 = y+my[i];
        if(x3&&x3<=n &&y3&&y3<=m &&in[x3][y3]){
            in[x3][y3] = 0;
            if(dfs(x3,y3)) return 1;
        }  //走过(x3,y3)时,令in[x3][y3]=0表示已走过该点下次不会再重复走,如果dfs(x3,y3)=1,表示通过这个点能走到终点
    }
    return 0;
}
int main()
{
    cin >> t;
    while(t--) 
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
         for(int j=1;j<=m;j++)
           cin>>in[i][j];
        cin>>x1>>y1>>x2>>y2;
        in[x1][y1]=0;
        cout<<dfs(x1,y1)<<endl;
    }
    return 0;
}  

2.能否走出迷宫以及最短路(左上角和右下角为1,即入口和出口)

#include<iostream>
#include<queue>  //广搜
using namespace std;
int maze[100][100];
int move[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct point { 
    int x, y;     
    int step; 
};
int main() {
    int n;
    point tem, next;
    queue<point> q;
    while(cin>>n,n) {
        while(!q.empty()) q.pop();
        for(int i=0; i<n; i++) 
          for(int j=0; j<n; j++)
             cin >> maze[i][j];
        maze[0][0] = 0;               
        tem.x = tem.y = 0;
        tem.step = 1;
        q.push(tem); //起点入队
        while(!q.empty()) {             
            tem = q.front();//取队头
            if (tem.x==n-1 && tem.y==n-1) break; //出口
			for(int i=0;i<4;i++)
			{  //将队头下步可走的点全部入队
				int x = tem.x+move[i][0];
				int y = tem.y+move[i][1];
			    if(x>=0&&x<n&&y>=0&&y<n 
&&maze[x][y]){
   maze[x][y] = 0;
					next.x = x;
				    next.y = y;
				    next.step = tem.step + 1;
				    q.push(next);		    
				}
			} 
            q.pop();//将队头出队,继续下一步判断 
        }
        if (tem.x == n-1 && tem.y == n-1)
            cout << q.front().step << endl;
        else cout << 0 << endl;              
    }
    return 0;
}  

3.走出迷宫最小权重(所有路都可以走)

#include<iostream>
#include<cstdio>
#include <cstring>
#include <queue>
#define INF 10001
using namespace std;
int n,m;
int g[101][101];
bool v[101][101]; 
int move[4][2]={{0,1},{0,-1},{1,0}, {-1,0}};

struct pot
{
    int x,y,sum;
    pot(int a,int b,int c):x(a),y(b),sum(c){}
    friend bool operator < (pot a,pot b){ //自定义优先级
        return a.sum > b.sum;  // sum最小的在top()里
    } 
};
int dij(pot a,pot b)
{
    priority_queue<pot> q;//优先级队列
    memset(v,false,sizeof(v));
    v[a.x][a.y]=true;
pot tmp = a;

    while(!v[b.x][b.y])
    {
        for(int i=0;i<4;i++){
            int x = tmp.x + move[i][0];
            int y = tmp.y + move[i][1];
            if(x&&x<=n &&y&&y<=m &&!v[x][y]){
            	pot next(x,y,tmp.sum+g[x][y]);
            	q.push(next);
            }
        }

        tmp = q.top();
        while(v[tmp.x][tmp.y]){//更新最短路径
            q.pop();
            tmp = q.top();
        }
        v[tmp.x][tmp.y] = true;      
}

    return tmp.sum ;
}
int main()
{
    int t,x1,y1,x2,y2;
    scanf("%d",&t);
    while(t--)
    {                                                              
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
             scanf("%d",&g[i][j]);
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        pot a(x1,y1,g[x1][y1]);
        pot b(x2,y2,g[x2][y2]);
        cout << dij(a,b) << endl;
   }
    return 0;
}   


走迷宫

标签:

原文地址:http://blog.csdn.net/u012421896/article/details/42747307

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!