Ural 1096-Get the Right Route Plate!(bfs)

1096. Get the Right Route Plate!

Time limit: 1.0 second

Memory limit: 64 MB

Everybody who had ridden a Ekaterinburg bus could notice that on the inner side of the plate with the number of the route there was a number of another route.

One day the driver of a new bus came to the storehouse and found that there was no plate with the number of the route he had been assigned to ride. The storekeeper simply gave him a random plate and
advised to change it for a plate from another bus. But the drivers who had the necessary plates did not need the plate given by the storekeeper. Any driver will agree to change his plate for another only if this plate has the number of his route. Help the
new driver to find a shortest sequence of changes that will enable him to get a plate with the number of his route.

Input

The first line contain the number K ≤ 1000 of the acting buses (excluding the new bus). The buses are numbered from 1 to K. The next K lines contain the number of the route
of the corresponding bus and the number on the other side of its plate. Numbers of routes are integers from 1 to 2000.

The last line of the input contains the number of the route of the new bus and the numbers on the plate given by the storekeeper.

Output

The first line of the output should contain the word IMPOSSIBLE if it is impossible to get the needed number by a sequence of changes otherwise it should contain the least necessary number of changes M > 0
followed by an M lines that contain sequentially numbers of buses (not routes!) with drivers of which the plates must be changed. If there are several solutions, you can output any one.

Sample

input output
4
8 5
5 4
7 4
1 5
4 1 8
2
4
2

题意:给出k个车牌,正反两面都有号码,然后给你一个车牌,还有一个路线号,你的任务是和别人交换车牌,来使车牌上的号码和你的路线号相同(正反面有一个相同即可)交换规则是你的牌子上面的号码要有和别人的牌子正面的号码相同的,求最小交换次数,然后打印路径(即和每个车牌交换的路径 车牌编号从1--k)bfs+记录路径即可。

#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;
}
时间: 2024-10-19 02:12:04

Ural 1096-Get the Right Route Plate!(bfs)的相关文章

URAL 1930 Ivan&#39;s Car(BFS)

Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are

poj2243&amp;&amp;hdu1372 Knight Moves(BFS)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http://poj.org/problem?id=2243 HDU: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where y

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

牛汇(BFS)入金具体流程(图文指导)

牛汇开户流程:bfsforex牛汇入金教程 所谓入金,也就是充值的意思,必须充钱到平台才能进行外汇交易.首先,我们先登录bfsforex牛汇官方网站,在交易办公室功能区域下面,点击账户入金: 为您提供中国各大银行的网银支付解决方案,支持人民币支付,和信用卡入金,入金是实时到账的. 牛汇(BFS)入金具体流程(图文指导),布布扣,bubuko.com

【判重+广搜(bfs)】魔板

判重+广搜(bfs)]魔板 Time Limit: 1000MS Memory Limit: 32768KB Special Judge 有一个两行四列的魔板,每个格子里有一个1到8的数字(数字唯一),现在我们可以对魔板进行以下操作: 1.交换两行的数字. 2.将第一列移到第二列,第二列到第三列,第三列到第四列,第四列到第一列. 3.将中间四个数顺时针转一次. 现给你初始状态,我末状态请你用最小的步数将它从初始状态变到末状态. 输入: 前两行,每行4个数表示初状态. 后两行,每行4个数表示末状态

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

USACO Mother&amp;#39;s Milk(bfs)

a=9MvljJDNdls&S=milk3">题目请点我 题解: 水杯倒水的问题非常经典,套路也是一样的,bfs找出全部状态. 这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的非常方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include &l