| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8695 | Accepted: 5125 |
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,
};
Input
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
题解:
每个节点记录父节点,递归打印路劲。
CODE:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos(-1.0);
typedef long long ll;
const int INF=1000010;
using namespace std;
int mp[10][10];
int x[4]= {-1,0,1,0};
int y[4]= {0,1,0,-1};
struct node
{
int x,y;
int nextx,nexty;
int op;
} a[1000];
queue<node>que;
void print(int x)
{
if(a[x].op!=-1)
{
print(a[x].op);
printf("(%d, %d)\n",a[x].x,a[x].y);
}
}
void bfs()
{
int first=0,num=0;
node s,t;
a[first].x=0,a[first].y=0;
a[first].op=-1;
que.push(a[first++]);
while(que.size())
{
t=que.front();
que.pop();
if(t.x==4&&t.y==4)
{
print(num);
break;
}
for(int i=0; i<4; i++)
{
a[first].x=t.x+x[i];
a[first].y=t.y+y[i];
if(a[first].x>=0&&a[first].x<=4&&a[first].y>=0&&a[first].y<=4&&mp[a[first].x][a[first].y]==0)
{
a[first].nextx=t.x;
a[first].nexty=t.y;
a[first].op=num;
mp[a[first].x][a[first].y]=1;
que.push(a[first++]);
}
}
num++;
}
}
int main()
{
freopen("test.in","r",stdin);
while(cin>>mp[0][0]>>mp[0][1]>>mp[0][2]>>mp[0][3]>>mp[0][4])
{
for(int i=1; i<5; i++)
for(int j=0; j<5; j++)
cin>>mp[i][j];
printf("(0, 0)\n");
bfs();
}
return 0;
}
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/42341873