【BFS】noj 独轮车

描述

独轮车的轮子上有红、黄、蓝、白、绿(依顺时针序)5种颜色,在一个如下图所示的20*20的迷宫内每走一个格子,轮子上的颜色变化一次。独轮车只能向前推或在原地转向。每走一格或原地转向90度均消耗一个单位时间。现给定一个起点(S)和一个终点(T),求独轮车以轮子上的指定颜色到达终点所需的最短时间。

在这里插入图片描述

输入

本题包含一个测例。测例中分别用一个大写字母表示方向和轮子的颜色,其对应关系为:E-东、S-南、W-西、N-北;R-红、Y-黄、B-蓝、W-白、G-绿。在测试数据的第一行有以空格分隔的两个整数和两个大写字母,分别表示起点的坐标S(x,y)、轮子的颜色和开始的方向,第二行有以空格分隔的两个整数和一个大写字母,表示终点的坐标T(x,y)和到达终点时轮子的颜色,从第三行开始的20行每行内包含20个字符,表示迷宫的状态。其中’X’表示建筑物,’.'表示路.

输出

在单独的一行内输出一个整数,即满足题目要求的最短时间。

输入样例

3 4 R N
15 17 Y
XXXXXXXXXXXXXXXXXXXX
X.X...XXXXXX......XX
X.X.X.....X..XXXX..X
X.XXXXXXX.XXXXXXXX.X
X.X.XX....X........X
X...XXXXX.X.XX.X.XXX
X.X.XX....X.X..X.X.X
X.X.X..XX...XXXX.XXX
X.X.XX.XX.X....X.X.X
X.X....XX.X.XX.X.X.X
X.X.X.XXXXX.XX.X.XXX
X.X.X.XXXXX....X...X
X.X.......X.XX...X.X
X.XXX.XXX.X.XXXXXXXX
X.....XX.......X...X
XXXXX....X.XXXXXXX.X
X..XXXXXXX.XXX.XXX.X
X.XX...........X...X
X..X.XXXX.XXXX...XXX
XXXXXXXXXXXXXXXXXXXX

输出样例

56

审题-思路

本题的基本思路是使用广搜来完成。需要注意的是与一般的迷宫问题不同,该题在每个可以到达的点上有 4X5=20 种状态,需要对这些状态进行记录并根据记录来查重,而不是简单的将走过的点标记。
充分认识广搜的状态以及对状态进行查重避免重复搜索时广搜最关键的一点。

完整代码

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

queue<struct node> qu;
char color[5] = {'R', 'Y', 'B', 'W', 'G'};//颜色数组  
int d[4] = {'N', 'E', 'S', 'W'}; //方向数组
int x[4] = {-1, 0, 1, 0};
int y[4] = {0, 1, 0, -1};

int mark[21][21][4][5]; //表示每个格子上的不同的状态  位置 - 方向 - 颜色 
char maze[21][21];//地图 

struct node{
	int x;
	int y;
	int n_color;
	int d;
	int c;//层次变量 
};

void bfs(int sx, int sy, int ex, int ey, int d, int s_color, char e_color){
	int i, j; 
	struct node start,temp,tmp;
	int c = 0;//广搜层次变量 
	start.x = sx;
	start.y = sy;
	start.n_color = s_color;
	start.d = d;
	start.c = c; 	
	qu.push(start);	
	mark[sx][sy][d][s_color] = 1;//标记该点的该状态	
	while(!qu.empty()){
		temp = qu.front();
		qu.pop();
		
		if(temp.x == ex && temp.y == ey && color[temp.n_color] == e_color){
			printf("%d\n",temp.c);
			return;
		}
		else{   
			if(maze[temp.x + x[temp.d]][temp.y + y[temp.d]] == '.' && mark[temp.x + x[temp.d]][temp.y + y[temp.d]][temp.d][(temp.n_color + 1)%5] == 0){//直走
				mark[temp.x + x[temp.d]][temp.y + y[temp.d]][temp.d][(temp.n_color + 1)%5] = 1; 
			 	tmp.x = temp.x + x[temp.d];  //位置,方向,颜色 ,层次 
			 	tmp.y = temp.y + y[temp.d];	
				tmp.d = temp.d;
				tmp.n_color = (temp.n_color + 1)%5;
				tmp.c = temp.c + 1;
				qu.push(tmp);							 	
			}
			if(mark[temp.x][temp.y][(temp.d + 3)%4][temp.n_color] == 0){//左转 
				mark[temp.x][temp.y][(temp.d + 3)%4][temp.n_color] = 1;
				tmp.x = temp.x;  //位置,方向,颜色 ,层次 
			 	tmp.y = temp.y;	
				tmp.d = (temp.d + 3)%4;
				tmp.n_color = temp.n_color;
				tmp.c = temp.c + 1;
				qu.push(tmp);			
			} 
			if(mark[temp.x][temp.y][(temp.d + 1)%4][temp.n_color] == 0){//右转 
				mark[temp.x][temp.y][(temp.d + 1)%4][temp.n_color] = 1;
				tmp.x = temp.x;  //位置,方向,颜色 ,层次 
			 	tmp.y = temp.y;	
				tmp.d = (temp.d + 1)%4;
				tmp.n_color = temp.n_color;
				tmp.c = temp.c + 1;
				qu.push(tmp);	
			} 			
		}
	}
} 

int main()
{
	int sx, sy, ex, ey;
	char c1, ds, c2;
	int i, j;
	int n_d;//方向数组的下标
	int n_color;//颜色数组的下标
	scanf("%d %d %c %c",&sx, &sy, &c1, &ds);	
	scanf("%d %d %c",&ex, &ey, &c2);
	
	getchar();//处理上一个输入的换行 
	for(i=1; i<21; i++){
		for(j=1; j<21; j++){
			scanf("%c",&maze[i][j]);
		}
		getchar();
	}
	
	
	for(i=0; i<5; i++){//确定颜色数组的初始下标 
		if(color[i] == c1){
			n_color = i;
			break;
		}
	}

	for(i=0; i<4; i++){//确定方向数组的初始下标 
		if(d[i] == ds){
			n_d = i;
			break;
		}
	}
	
	bfs(sx, sy, ex, ey, n_d, n_color, c2);	
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值