杭电1180 诡异的楼梯

3 篇文章 0 订阅

这道题,从WA,改到TLE,改到400ms,终于,改到了0ms,收货很多。

 

 

 一个重点就是:规范 BFS 写法!可以很有效地避免一些状态判重和状态转移上的细节错误,优先队列的使用也很好,以前我都只用队列的,这里不用优先队列会错。

 

#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
using namespace std;

struct Map{
	int x,y,step;
}s;
int m,n;
bool visit[21][21];
priority_queue<Map>q;
char map[21][21];
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//方向顺序有讲究

bool operator< (Map n1,Map n2){ 
    return n1.step>n2.step; 
}

int judge(Map p){
	if(p.x<0 || p.x>=m || p.y<0 || p.y>=n || map[p.x][p.y]=='*')
		return 1;
	return 0;
}

int direction(Map now,Map next){//判断方向,需要与上面写的方向一致
	int re;
	if(map[next.x][next.y]=='|')
		re=1;
	else
		re=0;
	re+=now.step%2;
	return re%2;
}

int BFS(){
	Map now,next;
	while(!q.empty()){
		now=q.top();
		q.pop();
		int i;
		if(map[now.x][now.y]=='T')
			return now.step;
		for(i=0;i<4;i++){
			next.x=now.x+dir[i][0];
			next.y=now.y+dir[i][1];
			next.step=now.step+1;
			if(judge(next) || visit[next.x][next.y])
				continue;
			else if(map[next.x][next.y]=='.' || map[next.x][next.y]=='T'){
				visit[next.x][next.y]=1;
				q.push(next);
				continue;
			}
			else{
				if(direction(now,next)!=(i%2))
					next.step++;
				next.x+=dir[i][0];
				next.y+=dir[i][1];
				if(judge(next) || visit[next.x][next.y])
					continue;
				else{
					visit[next.x][next.y]=1;
					q.push(next);
				}
			}
		}
	}
	return 0;
}

int main(){
	int i,j;
	while(~scanf("%d%d",&m,&n)){
		while(!q.empty())
			q.pop();
		getchar();
		memset(visit,0,sizeof(visit));
		for(i=0;i<m;i++){
			for(j=0;j<n;j++){
				scanf("%c",&map[i][j]);
				if('S'==map[i][j]){
					s.x=i;
					s.y=j;
					s.step=0;
					q.push(s);
				}
			}
			getchar();
		}
		printf("%d\n",BFS());
	}
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值