zoukankan      html  css  js  c++  java
  • POJ3984——迷宫问题

    迷宫问题
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 31616   Accepted: 18100

    Description

    定义一个二维数组: 

    int maze[5][5] = {
    0, 1, 0, 0, 0,
    0, 1, 0, 1, 0,
    0, 0, 0, 0, 0,
    0, 1, 1, 1, 0,
    0, 0, 0, 1, 0,
    };

    它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

    Input

    一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

    Output

    左上角到右下角的最短路径,格式如样例所示。

    Sample Input

    0 1 0 0 0
    0 1 0 1 0
    0 0 0 0 0
    0 1 1 1 0
    0 0 0 1 0

    Sample Output

    (0, 0)
    (1, 0)
    (2, 0)
    (2, 1)
    (2, 2)
    (2, 3)
    (2, 4)
    (3, 4)
    (4, 4)

    Source

     

    1.简单DFS

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<algorithm>
    #include<vector>
    #include<cmath>
    #include<cstring>
    #include<string> 
     
    #define N 100010
    
    using namespace std;
    
    void in(int &x){
        register char c=getchar();x=0;int f=1;
        while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
        while(isdigit(c)){x=x*10+c-'0';c=getchar();}
        x*=f;
    }
    
    int a[6][6],tot;
    struct node{int x,y;}e[30],ans[30];
    int mx[4]={0,0,1,-1},	
    	my[4]={1,-1,0,0};
    bool vis[6][6];
    
    void print(int k){
    	if(k==2) for(int i=1;i<=tot;i++) printf("(%d, %d)
    ",ans[i].x,ans[i].y);
    	else for(int i=1;i<=tot;i++) ans[i].x=e[i].x,ans[i].y=e[i].y;
    }
    
    void DFS(int x,int y,int tim){
    	e[tim].x=x;e[tim].y=y;
    	if(tim>tot) return;
    	if(x==4&&y==4){
    		if(tim<tot){
    			tot=tim;print(1);
    		}return;
    	}for(int i=0;i<4;i++){
    		int tx=x+mx[i],ty=y+my[i];
    		if(tx>=0&&tx<=4&&ty>=0&&ty<=4&&vis[tx][ty]==0&&a[tx][ty]==0){
    			vis[tx][ty]=1;
    			DFS(tx,ty,tim+1);
    			vis[tx][ty]=0;
    		}
    	}
    }	
    
    int main()
    {
    	tot=0x7fffffff;
    	for(int i=0;i<5;i++)
    		for(int j=0;j<5;j++)
    			in(a[i][j]);
    	DFS(0,0,1);
    	print(2);
    	return 0;
    } 
    

      2.简单BFS(考虑如何更新)

  • 相关阅读:
    [AS3]AMF3+JAVA的调用范例
    SmartFoxServer,多人flash游戏开发
    Flex2 发现之旅:在Flex中嵌入完整HTML页面
    让.Net 程序脱离.net framework框架运行
    ASP.NET 数据列表控件的分页总结
    中国移动飞信免费发短信API接口(第三方 Fetion API)[原创]
    Ubuntu下安装wine详细介绍
    SQL SERVER 2008 R2 序列号大全
    linux 下安装ATI 显卡驱动
    jdk 环境变量设置
  • 原文地址:https://www.cnblogs.com/song-/p/9264536.html
Copyright © 2011-2022 走看看