poj 3984

来源:互联网 发布:如何开个淘宝店铺 编辑:程序博客网 时间:2024/06/11 06:00
迷宫问题
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 20998 Accepted: 12293

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 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0

Sample Output

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

(4, 4)

#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <cstdio>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>using namespace std;#define pi acos(-1)#define endl '\n'#define me(x) memset(x,0,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0);typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;//const int dx[]={-1,0,1,0,-1,-1,1,1};//const int dy[]={0,1,0,-1,1,-1,1,-1};const int maxn=1e3+5;const int maxx=5e4+100;const double EPS=1e-7;const int MOD=1000000007;#define mod(x) ((x)%MOD);template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;/*lch[root] = build(L1,p-1,L2+1,L2+cnt);    rch[root] = build(p+1,R1,L2+cnt+1,R2);中前*//*lch[root] = build(L1,p-1,L2,L2+cnt-1);    rch[root] = build(p+1,R1,L2+cnt,R2-1);中后*/long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}int a[5][5];bool visit[5][5];int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};struct Node{    int x,y;    int s;///路径长度    int l[30];///每走一步的方向};///通过记录方向来记录路径bool judge(int x,int y){    if(x<0||x>=5||y<0||y>=5)        return true;    if(visit[x][y])        return true;    if(a[x][y]==1)        return true;    return false;}Node& bfs(){    queue<Node> que;    Node cur,next;    cur.x=0;cur.y=0;cur.s=0;    visit[0][0]=1;    que.push(cur);    while(que.size())    {        cur=que.front();        que.pop();        if(cur.x==4&&cur.y==4)            return cur;        int i,nx,ny;        for(i=0;i<4;i++)        {            nx=cur.x+dx[i];            ny=cur.y+dy[i];            if(judge(nx,ny))                continue;            next=cur;            next.x=nx;            next.y=ny;            next.s=cur.s+1;            next.l[cur.s]=i;            visit[nx][ny]=1;            que.push(next);        }    }    return cur;}int main(){    for(int i=0;i<5;i++)    {        for(int j=0;j<5;j++)        {            scanf("%d",&a[i][j]);        }    }    Node ans=bfs();    printf("(0, 0)\n");    int x=0,y=0;    for(int i=0;i<ans.s;i++)    {        x+=dx[ans.l[i]];        y+=dy[ans.l[i]];        printf("(%d, %d)\n",x,y);    }    return 0;}


0 0
原创粉丝点击