【 ZOJ- 3865】Superbot(BFS)

Superbot is an interesting game which you need to control the robot on an N*M grid map.

As you see, it's just a simple game: there is a control panel with four direction left (1st position), right (2nd), up (3rd) and down (4th). For each second, you can do exact one of the following operations:

  • Move the cursor to left or right for one position. If the cursor is on the 1stposition and moves to left, it will move to 4th position; vice versa.
  • Press the button. It will make the robot move in the specific direction.
  • Drink a cup of hot coffee and relax. (Do nothing)

However, it's too easy to play. So there is a little trick: Every P seconds the panel will rotate its buttons right. More specifically, the 1st position moves to the 2nd position; the 2nd moves to 3rd; 3rd moves to 4th and 4th moves to 1st. The rotating starts at the beginning of the second.

Please calculate the minimum time that the robot can get the diamond on the map.

At the beginning, the buttons on the panel are "left", "right", "up", "down" respectively from left to right as the picture above, and the cursor is pointing to "left".

Input

There are multiple test cases. The first line of input contains an integer Tindicating the number of test cases. For each test case:

The first line contains three integers N, M (2 <= N, M <= 10) and P (1 <= P <= 50), which represent the height of the map, the width of the map and the period that the panel changes, respectively.

The following lines of input contains N lines with M chars for each line. In the map, "." means the empty cell, "*" means the trap which the robot cannot get in, "@" means the initial position of the robot and "$" means the diamond. There is exact one robot and one diamond on the map.

Output

For each test case, output minimum time that the robot can get the diamond. Output "YouBadbad" (without quotes) if it's impossible to get the diamond.

Sample Input

4
3 4 50
@...
***.
$...
5 5 2
.....
..@..
.*...
$.*..
.....
2 3 1
*.@
$.*
5 5 2
*****
..@..
*****
$....
.....

Sample Output

12
4
4
YouBadbad

Hint

For the first example: 
0s: start
1s: cursor move right (cursor is at "right")
2s: press button (robot move right)
3s: press button (robot move right)
4s: press button (robot move right)
5s: cursor move right (cursor is at "up")
6s: cursor move right (cursor is at "down")
7s: press button (robot move down)
8s: press button (robot move down)
9s: cursor move right (cursor is at "left")
10s: press button (robot move left)
11s: press button (robot move left)
12s: press button (robot move left)

For the second example:
0s: start
1s: press button (robot move left)
2s: press button (robot move left)
--- panel rotated ---
3s: press button (robot move down, without changing cursor)
4s: press button (robot move down)

For the third example:
0s: start
1s: press button (robot move left)
--- panel rotated ---
2s: press button (robot move down)
--- panel rotated ---
3s: cursor move left (cursor is at "right")
--- panel rotated ---
4s: press button (robot move left)

思路:

能看出来是bfs,一开始是想手动模拟nex数组交换,并且发现一个点可能走多次,所以打算设个上界,但是一直WA,后来听zyb大佬讲解后,才发现不用这么麻烦,因为nex数组总共就只有4种状况,所以可以把四种都列出了,判断在哪一个模式下就可以,记录位置的时候可以开4维,记录数组vis[i][j][m][p],指在(i,j)的坐标下,当前模式为m,并且指针指向p这个位置时,有没有走过,这个记录方法很巧妙,自己没有想到,这样就把所有的情况都可以记录下来。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<string>
#include<vector>
#define mod (1000000007)
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
struct node{
	int x,y;
	int t;
	int pos;
	int mo;
	node(){}
	node(int xx,int yy,int tt,int p,int m):x(xx),y(yy),t(tt),pos(p),mo(m){}
	friend bool operator<(node a,node b)
	{
		return a.t>b.t;
	}
};
char mp[100][100];
int stx,sty,edx,edy;
int vis[100][100][4][4];
int nex[4][4][2]={
	{{0,-1},{0,1},{-1,0},{1,0}},
	{{1,0},{0,-1},{0,1},{-1,0}} ,
	{{-1,0},{1,0},{0,-1},{0,1}} ,
	{{0,1},{-1,0},{1,0},{0,-1}},
};
int n,m,p;
void bfs()
{
	int ans=-1;
	memset(vis,0,sizeof(vis));
	priority_queue<node> que;
	que.push(node(stx,sty,0,0,0));
	while(!que.empty()){
		node nn=que.top();
		que.pop();
		if(vis[nn.x][nn.y][nn.mo][nn.pos]==1) continue;
		if(edx==nn.x&&edy==nn.y){
			ans=nn.t;break;
		}
		
		vis[nn.x][nn.y][nn.mo][nn.pos]++;
		int ad=0;
		if((nn.t+1)%p==0)
		ad=1;
		que.push(node(nn.x,nn.y,nn.t+1,nn.pos,(nn.mo+ad)%4));
		que.push(node(nn.x,nn.y,nn.t+1,(nn.pos+1)%4,(nn.mo+ad)%4));
		que.push(node(nn.x,nn.y,nn.t+1,(nn.pos-1+4)%4,(nn.mo+ad)%4));
		int tx=nn.x+nex[nn.mo][nn.pos][0];
		int ty=nn.y+nex[nn.mo][nn.pos][1]; 
		if(tx<0||tx>=n||ty<0||ty>=m||mp[tx][ty]=='*') {
			;
		}else 
		que.push(node(tx,ty,nn.t+1,nn.pos,(nn.mo+ad)%4));
	}
	if(ans==-1)puts("YouBadbad");
	else
	printf("%d\n",ans);
}
int main()
{
	
	int t;
	cin>>t;
	while(t--)
	{
		
		scanf("%d%d%d",&n,&m,&p);
		for(int i=0;i<n;i++){
			scanf("%s",mp[i]);
			for(int j=0;j<m;j++)
			{
				if(mp[i][j]=='@')
				{
					stx=i;sty=j;
				}
				else if(mp[i][j]=='$')
				{
					edx=i;edy=j;
				}
			}
		}
		bfs();
		
	}

	return 0;
}

 

内容概要:该库专为研究生入学考试计算机组成原理科目设计,涵盖名校考研真、经典教材课后习、章节库和模拟试四大核心模块。名校考研真精选多所知名高校的计算机组成原理科目及计算机联考真,并提供详尽解析,帮助考生把握考研命趋势与难度。经典教材课后习包括白中英《计算机组成原理》(第5版)和唐朔飞《计算机组成原理》(第2版)的全部课后习解答,这两部教材被众多名校列为考研指定参考书目。章节库精选代表性考,注重基础知识与重难点内容,帮助考生全面掌握考试大纲要求的知识点。模拟试依据历年考研真规律和热门考点,精心编制两套全真模拟试,并附标准答案,帮助考生检验学习成果,评估应试能力。 适用人群:计划参加研究生入学考试并报考计算机组成原理科目的考生,尤其是需要系统复习和强化训练的学生。 使用场景及目标:①通过研读名校考研真,考生可以准确把握考研命趋势与难度,有效评估复习成效;②通过经典教材课后习的练习,考生可以巩固基础知识,掌握解技巧;③通过章节库的系统练习,考生可以全面掌握考试大纲要求的各个知识点,为备考打下坚实基础;④通过模拟试的测试,考生可以检验学习成果,评估应试能力,为正式考试做好充分准备。 其他说明:该库不仅提供详细的目解析,还涵盖了计算机组成原理的各个方面,包括计算机系统概述、数据表示与运算、存储器分层、指令系统、中央处理器、总线系统和输入输出系统等。考生在使用过程中应结合理论学习与实践操作,注重理解与应用,以提高应试能力和专业知识水平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值