您的位置:首页 > 其它

ZOJ 1103(POJ 2415)(HDU 1252)Hike…

2012-12-05 17:14 411 查看
Hike on a
Graph
Time Limit: 2 Seconds
    Memory Limit: 65536 KB
"Hike on a Graph" is a game that is played on a board on which
an undirected graph is drawn. The graph is complete and has all
loops, i.e. for any two locations there is exactly one arrow
between them. The arrows are coloured. There are three players, and
each of them has a piece. At the beginning of the game, the three
pieces are in fixed locations on the graph. In turn, the players
may do a move. A move consists of moving one's own piece along an
arrow to a new location on the board. The following constraint is
imposed on this: the piece may only be moved along arrows of the
same colour as the arrow between the two opponents' pieces.

In the sixties ("make love not war") a one-person variant of the
game emerged. In this variant one person moves all the three
pieces, not necessarily one after the other, but of course only one
at a time. Goal of this game is to get all pieces onto the same
location, using as few moves as possible. Find out the smallest
number of moves that is necessary to get all three pieces onto the
same location, for a given board layout and starting positions.

Input Specification

The input file contains several test cases. Each test case
starts with the number n. Input is terminated by n=0.
Otherwise, 1<=n<=50. Then
follow three integers p1, p2,
p3
with
1<=pi<=n denoting
the starting locations of the game pieces. The colours of the
arrows are given next as a m×m matrix of
whitespace-separated lower-case letters. The element
mij denotes the colour of the arrow between the
locations i and j. Since the graph is undirected, you
can assume the matrix to be symmetrical.

Output Specification

For each test case output on a single line the minimum number of
moves required to get all three pieces onto the same location, or
the word "impossible" if that is not possible for the given board
and starting locations.

Sample Input

3 1 2 3
r b r
b b b
r b r
2 1 2 2
y g
g y
0
Sample Output

2
impossible

Source: University of Ulm Local Contest 2000
 

好吧,我承认这题我的确是做不出来了,于是乎,转向百度求救,好悲催,发现我竟然读错题意了…………

 

我用了n个晚上,做了n遍题,TLE了n次,WA了n次,结果却发现自己读错题意了,真的有种想要自杀的冲动(如果可以复活的话)……

 

发现一个非常普遍的问题,就是现在的大牛们都抛弃了C了,转向了C++,这让我们这些C都还没学好的小菜鸟情何以堪啊(

),好郁闷,看了N久才看明白代码,不过看了之后发现也蛮好的,从今天开始,为了装出一个大牛的样子,决定向C++靠拢……

 

先说题意吧,省的大家又弄错题意了,给出n个结点,每两个结点之间都有一个线路想通,不过有不同的颜色,然后给三个piece(至今没搞懂是什么东东,不过无所谓了),分别在三个初始位置,piece移动的条件是通过的路径的颜色必须要与另外两个piece所在位置之间的路径颜色想同,求使三个点移动到同一个位置上所需要的最小步数……

 

都说是BFS,可是我感觉队列还是多一些……

既然最后三个点要移动到一个点的位置上,我们可以通过记录三个点移动到每个位置时候的步数,用队列的方式

存储下移动步数最小的那些点,直到找到第一个三点相同的位置,结束查找

 

由于要用到队列,所以不可避免的用到了C++,代码基本上和网上那些代码一样

 

代码:

C++语言:
高亮代码由发芽网提供
// User:
cuixuan
//Memory: 1308K 
Time: 125MS
//Language: C++ 
Result: Accepted

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using
namespace
std;
#define MAXN 55
int
mark[MAXN][MAXN][MAXN],n;
char
map[MAXN][MAXN];
struct
node{

   int a,b,c;

   int time;
}cur,next;

int
BFS()
{

   int i;

   queue<node> q;

   memset(mark,0,sizeof(mark));//存储三个piece移动到该位置没有

   cur.time=0;

   mark[cur.a][cur.b][cur.c]=1;

   q.push(cur);

   while(!q.empty())//队列为空退出

   {

       cur=q.front();//取队首元素

       q.pop();//删除队首元素

       for(i=1;i<=n;i++)

       {

           if(map[cur.a][i]==map[cur.b][cur.c]
&&
!mark[i][cur.b][cur.c])

           {//当每个piece移动到i位置的时候记下时间,入队列

               mark[i][cur.b][cur.c]=1;

               next.time=cur.time+1;

               next.a=i;next.b=cur.b;next.c=cur.c;

               if(next.a==next.b&&next.b==next.c)return
next.time;//如果找到一个位置,退出

               q.push(next);//新找到的位置入队

           }

           if(map[cur.b][i]==map[cur.a][cur.c]
&&
!mark[cur.a][i][cur.c])

           {

               mark[cur.a][i][cur.c]=1;

               next.time=cur.time+1;

               next.a=cur.a;next.b=i;next.c=cur.c;

               if(next.a==next.b&&next.b==next.c)return
next.time;

    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: