poj 3984 迷宫问题

来源:互联网 发布:json在线处理 编辑:程序博客网 时间:2024/06/08 09:12




第一感觉就是想用刚学过的结构体指针输出坐标...于是试了一下..结果就过了  嘻嘻

迷宫问题

Time Limit: 1000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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<iostream>#include<cstdio>#include<algorithm>#include<queue>#include<string.h>using namespace std;struct node{    int x;    int y;    node *bf;}temp,temp1;int a[10][10];int vis[10][10];int xy[10][2];queue<node >q;int d[4][2]={1,0,-1,0,0,1,0,-1};int main(){    node s;node *head=NULL;    node *p=NULL;    while(~scanf("%d",&a[0][0]))    {        for(int k=1;k<5;k++)            cin>>a[0][k];        for(int i=1;i<5;i++)        {            for(int j=0;j<5;j++)            {                cin>>a[i][j];            }        }        while(!q.empty())        {            q.pop();        }        memset(vis,0,sizeof(vis));        s.x = 0;        s.y = 0;        s.bf=NULL;        q.push(s);        vis[0][0]=1;        while(!q.empty())        {            temp = q.front();            q.pop();            for(int i=0;i<4;i++)            {                 temp1.x = temp.x + d[i][0];                 temp1.y = temp.y + d[i][1];                 if(temp1.x>=0&&temp1.x<=4&&temp1.y>=0&&temp1.y<=4&&a[temp1.x][temp1.y]!=1&&!vis[temp1.x][temp1.y])                 {                     vis[temp1.x][temp1.y]=1;                     temp1.x = temp.x + d[i][0];                     temp1.y = temp.y + d[i][1];                     p = new node;                     *p=temp;                     temp1.bf=p;                     q.push(temp1);                 }                 if(vis[4][4])                 {                     head=temp1.bf;                     break;                 }            }        }        int cnt=0;        while(head!=NULL)        {           // printf("%d %d\n",head->x,head->y);            xy[cnt][0]=head->x;            xy[cnt][1]=head->y;            head=head->bf;            cnt++;        }        for(int i=cnt-1;i>=0;i--)        {            printf("(%d, %d)\n",xy[i][0],xy[i][1]);        }        printf("(4, 4)\n");    }    return 0;}

原创粉丝点击