日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hike on a Graph HDU - 1252(bfs)

發(fā)布時間:2023/12/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hike on a Graph HDU - 1252(bfs) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

“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
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
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
題意挺難理解的。
題意:給定一個n*n的二維數(shù)組,(i,j)代表的是第i個位置到第j個位置的路徑顏色。有三個點一開始位于三個坐標(biāo)上(有可能相同),每一次移動只能移動一個點,移動的條件是:這個點到移向點的路徑顏色要等于另外兩個點之間的路徑顏色。要使得這三個點在同一坐標(biāo)上,求最少移動次數(shù)。如果不行就輸出impossible。
思路:每次只能移動一個點,我們用一個三維數(shù)組記錄這三個點到達(dá)的坐標(biāo),如果之前這三個坐標(biāo)同時到達(dá)過,就不要再重復(fù)走了。然后bfs去枚舉這三個點的移動路徑,如果三個點坐標(biāo)相同,直接返回就好了。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=51; struct node{int x,y,z;int num;node(int as,int bs,int cs,int d){x=as,y=bs,z=cs,num=d;} }; int vis[maxx][maxx][maxx]; string s[maxx]; int n,as,bs,cs;inline int bfs() {queue<node> q;vis[as][bs][cs]=1;q.push(node(as,bs,cs,0));while(q.size()){node u=q.front();q.pop();if(u.x==u.y&&u.y==u.z) return u.num;for(int i=1;i<=n;i++){if(s[i][u.x]==s[u.y][u.z]&&vis[i][u.y][u.z]==0){vis[i][u.y][u.z]=1;q.push(node(i,u.y,u.z,u.num+1));}if(s[i][u.y]==s[u.x][u.z]&&vis[u.x][i][u.z]==0){vis[u.x][i][u.z]=1;q.push(node(u.x,i,u.z,u.num+1));}if(s[i][u.z]==s[u.x][u.y]&&vis[u.x][u.y][i]==0){vis[u.x][u.y][i]=1;q.push(node(u.x,u.y,i,u.num+1));}}}return -1; } int main() {while(scanf("%d",&n),n){scanf("%d%d%d",&as,&bs,&cs);char c;for(int i=1;i<=n;i++){s[i]="";for(int j=1;j<=n;j++) cin>>c,s[i]=s[i]+c;s[i]=' '+s[i];}memset(vis,0,sizeof(vis));int ans=bfs();if(ans==-1) cout<<"impossible"<<endl;else cout<<ans<<endl;}return 0; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Hike on a Graph HDU - 1252(bfs)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。