F - Superbot ZOJ - 3865

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 1st position 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 T indicating 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


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn=20;
const int inf=0x3f3f3f3f;

char mp[maxn][maxn];

int stx,sty,edx,edy;

struct node{
	int x,y;
	int s,p;
	node(){}
	node(int _x,int _y,int _s,int _p):x(_x),y(_y),s(_s),p(_p){}
	bool operator<(const node& b)const{
		return s>b.s; 
	}
};

int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
int min_[maxn][maxn][4];
int n,m,P;
/*
模拟题
当前状态有四种 选择,我们按照要求写就好了
只是 这个当前的位置不明确
我们不用关下一个点的方向,我们只需要管,当前点的就好

他的这种调节比较巧妙,因为都是一秒一秒的走,
所以他是直接压进去,然后再看看我这个点到底现在的朝向到底是哪
这样来看得话,确实简单了超级多的步骤
扔进去的时候不调节,只是我们拿出来的时候调节 
*/
void bfs(){
	priority_queue<node> q;
	q.push(node(stx,sty,0,0));
	while(!q.empty()){
	//	printf("****\n");
		struct node t=q.top();
		q.pop();
	//	printf("%d %d\n",t.x,t.y);
		if(t.s!=0&&t.s%P==0) t.p=(t.p-1+4)%4;
		if(t.s>=min_[t.x][t.y][t.p])continue;
		min_[t.x][t.y][t.p]=t.s;
		if(mp[t.x][t.y]=='$') return; 
		struct node tt=t;
		tt.s++;
		tt.p=(t.p+1)%4;
		q.push(tt);
		
		tt.p=(t.p-1+4)%4;
		q.push(tt);
		
		tt=t;
		tt.s++;
		tt.x+=dir[t.p][0],tt.y+=dir[t.p][1];
	
		if(!(tt.x<1||tt.x>n||tt.y<1||tt.y>m||mp[tt.x][tt.y]=='*')) 
			q.push(tt);

		tt=t;
		tt.s++;
		q.push(tt);
	}
}

void init(){
	memset(min_,inf,sizeof(min_));
}


int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		init();
		scanf("%d %d %d",&n,&m,&P);
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				scanf(" %c",&mp[i][j]);
				if(mp[i][j]=='@'){
					stx=i,sty=j;
				}
				if(mp[i][j]=='$'){
					edx=i,edy=j;
				}
			}
		}
		bfs();
		int ans=inf;
		for(int i=0;i<4;i++){
			ans=min(ans,min_[edx][edy][i]);
		}
		if(ans==inf)
			printf("YouBadbad\n");
		else
			printf("%d\n",ans);
	}	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在MATLAB中,NURBS(非均匀有理B样条)是一种强大的数学工具,用于表示和处理复杂的曲线和曲面。NURBS在计算机图形学、CAD(计算机辅助设计)、CAM(计算机辅助制造)等领域有着广泛的应用。下面将详细探讨MATLAB中NURBS的绘制方法以及相关知识点。 我们需要理解NURBS的基本概念。NURBS是B样条(B-Spline)的一种扩展,其特殊之处在于引入了权重因子,使得曲线和曲面可以在不均匀的参数空间中进行平滑插值。这种灵活性使得NURBS在处理非均匀数据时尤为有效。 在MATLAB中,可以使用`nurbs`函数创建NURBS对象,它接受控制点、权值、 knot向量等参数。控制点定义了NURBS曲线的基本形状,而knot向量决定了曲线的平滑度和分布。权值则影响曲线通过控制点的方式,大的权值会使曲线更靠近该点。 例如,我们可以使用以下代码创建一个简单的NURBS曲线: ```matlab % 定义控制点 controlPoints = [1 1; 2 2; 3 1; 4 2]; % 定义knot向量 knotVector = [0 0 0 1 1 1]; % 定义权值(默认为1,如果未指定) weights = ones(size(controlPoints,1),1); % 创建NURBS对象 nurbsObj = nurbs(controlPoints, weights, knotVector); ``` 然后,我们可以用`plot`函数来绘制NURBS曲线: ```matlab plot(nurbsObj); grid on; ``` `data_example.mat`可能包含了一个示例的NURBS数据集,其中可能包含了控制点坐标、权值和knot向量。我们可以通过加载这个数据文件来进一步研究NURBS的绘制: ```matlab load('data_example.mat'); % 加载数据 nurbsData = struct2cell(data_example); % 转换为cell数组 % 解析数据 controlPoints = nurbsData{1}; weights = nurbsData{2}; knotVector = nurbsData{3}; % 创建并绘制NURBS曲线 nurbsObj = nurbs(controlPoints, weights, knotVector); plot(nurbsObj); grid on; ``` MATLAB还提供了其他与NURBS相关的函数,如`evalnurbs`用于评估NURBS曲线上的点,`isoparm`用于生成NURBS曲面上的等参线,以及`isocurve`用于在NURBS曲面上提取特定参数值的曲线。这些工具对于分析和操作NURBS对象非常有用。 MATLAB中的NURBS功能允许用户方便地创建、编辑和可视化复杂的曲线和曲面。通过对控制点、knot向量和权值的调整,可以精确地控制NURBS的形状和行为,从而满足各种工程和设计需求。通过深入理解和熟练掌握这些工具,可以在MATLAB环境中实现高效的NURBS建模和分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值