标签:style color os io for ar div cti html
| input | output |
|---|---|
4 8 5 5 4 7 4 1 5 4 1 8 |
2 4 2 |
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
using namespace std;
const int maxn=2000011;
const int INF=1<<29;
bool vis[2010][2010];
struct node{
int x,y,pre,cur,step;
}que[maxn];
int m,s,e,xx[maxn],yy[maxn];
void bfs(int x,int y,int goal)
{
s=e=0;int pos;
memset(vis,0,sizeof(vis));
node t,f;
t.step=0;t.x=x;t.y=y;t.pre=0;t.cur=0;
vis[t.x][t.y]=1;
que[e++]=t;
while(s<e)
{
f=que[s];pos=s;s++;
if(f.x==goal||f.y==goal)
{
stack <int> ss;
printf("%d\n",f.step);
int tem=pos;
for(int i=0;i<f.step;i++)
{
ss.push(que[tem].cur);
tem=que[tem].pre;
}
while(!ss.empty())
{
printf("%d\n",ss.top());
ss.pop();
}
return ;
}
for(int i=1;i<=m;i++)
{
if(xx[i]==f.x||xx[i]==f.y)
{
t.x=xx[i];
t.y=yy[i];
t.pre=pos;
t.cur=i;
t.step=f.step+1;
if(!vis[t.x][t.y])
{
vis[t.x][t.y]=1;
que[e++]=t;
}
}
}
}
puts("IMPOSSIBLE");
}
int main()
{
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&xx[i],&yy[i]);
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
bfs(b,c,a);
return 0;
}
Ural 1096-Get the Right Route Plate!(bfs)
标签:style color os io for ar div cti html
原文地址:http://blog.csdn.net/qq_16255321/article/details/38892983