Hike on a Graph

Hike on a Graph

链接:http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=4&sectionid=2&problemid=17
题目:“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.
题目的大致意思是,你有三个木块,需要你使用最小的步数移动到同一个位置。但是他有限制条件,也是这道题目的关键。那就是你将要移动的木块的位置到你需要移动到的木块的位置之间路径的颜色的与另外两块木块所在位置的路径是相同的,否则,你将不能移动。(本人就是这个地方没看懂,导致运行内存超限好多次)
看懂了题意,那么这道题就很简单了,就是一道简单的bfs()。

#include<stdio.h>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
struct Node{
	int r[4];
	int step;
	friend bool operator <(Node a,Node b){
			return a.step>b.step;
	}
};
Node a;
int n;
bool visit[60][60][60];
char yanshe[60][60];
void BFS(){
	priority_queue<Node> q;
	q.push(a);
	visit[a.r[1]][a.r[2]][a.r[3]]=false;
	Node next1;
	while(!q.empty()){
		a=q.top();
		q.pop();
		if(a.r[1]==a.r[2]&&a.r[1]==a.r[3]&&a.r[1]==a.r[2]){
				printf("%d\n",a.step);//如果你在外面输出你一定要注意当初始位置就已经在同一个位置的时候
				return ;
		}
		for(int j=1;j<=n;j++){
				next1=a;
				if(j!=next1.r[1]&&visit[j][next1.r[2]][next1.r[3]]&&yanshe[next1.r[1]][j]==yanshe[next1.r[2]][next1.r[3]]){
					next1.step+=1;
					next1.r[1]=j;
					visit[j][next1.r[2]][next1.r[3]]=false;
					q.push(next1);
				}
		}		
		for(int j=1;j<=n;j++){
				next1=a;
				if(j!=next1.r[2]&&visit[next1.r[1]][j][next1.r[3]]&&yanshe[next1.r[2]][j]==yanshe[next1.r[1]][next1.r[3]]){
					next1.step+=1;
					next1.r[2]=j;
					visit[next1.r[1]][j][next1.r[3]]=false;
					q.push(next1);
				}
			}
		for(int j=1;j<=n;j++){
				next1=a;
				if(j!=next1.r[3]&&visit[next1.r[1]][next1.r[2]][j]&&yanshe[next1.r[3]][j]==yanshe[next1.r[2]][next1.r[1]]){
					next1.step+=1;
					next1.r[3]=j;
					visit[next1.r[1]][next1.r[2]][j]=false;
					q.push(next1);
				}
	}
	}
	printf("impossible\n");
}
int main(void){
	while(~scanf("%d",&n)&&n){
		for(int i=1;i<=3;i++){
			scanf("%d",a.r+i);
		}
		a.step=0;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++){
				cin>>yanshe[i][j];//使用scanf要注意吃掉空格和回车
			}
		memset(visit,1,sizeof(visit));
		BFS();	
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值