uva 10047 The Monocycle

7 篇文章 0 订阅
2 篇文章 0 订阅
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define N 30
char Map[N][N];
int n,m;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
struct node{ 
	int x,y;
	int steps;
	int dir;
	int color;
	bool operator<(node st) const{
		return st.steps<steps;
	}
}st;
bool vis[N][N][4][5];
priority_queue<node> p;

bool judge(node b){
	if(b.x<0||b.y<0||b.x>=n||b.y>=m) return false;
	return true;
}


int bfs(node st){
	while(!p.empty()) p.pop();
	memset(vis,0,sizeof(vis));
	st.dir=0;
	st.color=0;
	vis[st.x][st.y][0][0]=1;
	p.push(st);
	while(!p.empty()){
		node a=p.top();
		p.pop();
		if(Map[a.x][a.y]=='T'&&a.color==0) return a.steps;
		for(int i=0;i<4;i++){
			node b;
			if(a.dir!=i){刚开始一直wrong,在转向之后,直接又走了一步,但这样算是两次操作了,在第一次操作后产生的状态,同样有四种可能,直接走只是一种可能情况。.
				b.x=a.x;
				b.y=a.y;
				int dis=(int)abs(a.dir-i);
				if(dis==2)
					b.steps=a.steps+2;
				else b.steps=a.steps+1;
				b.dir=i;
				b.color=a.color;
			}
			else {
				b.x=a.x+dx[i];
				b.y=a.y+dy[i];
				b.steps=a.steps+1;
				b.color=(a.color+1)%5;
				b.dir=a.dir;
			}
			if(!judge(b)) continue;
			if(Map[b.x][b.y]=='#') continue;
			if(vis[b.x][b.y][b.dir][b.color]) continue;
			vis[b.x][b.y][b.dir][b.color]=1;
			p.push(b);
		}
	}
	return -1;
}

int main(){
	int f=1;
	while(scanf("%d%d",&n,&m)!=EOF){
		if(!n&&!m) break;
		for(int i=0;i<n;i++){
			scanf("%s",Map[i]);
			for(int j=0;j<m;j++){
				if(Map[i][j]=='S'){
					st.x=i;
					st.y=j;
					st.steps=0;
				}
			}
		}
		if(f!=1) puts("");
		int result=bfs(st);
		printf("Case #%d\n",f++);
		if(result==-1) printf("destination not reachable\n");
		else printf("minimum time = %d sec\n",result);
		//puts("");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值