题意:
给一个n*m的图。
'@'代表你的位置,'.'代表空地,'*'代表墙,'$'代表钻石。
在每一秒钟你有四种选择。
1、站着不动。
2、光标往左移动一格。
3、光标往右移动一格。
4、点击光标让自己按光标的方向移动一格。
然后题目还给了一个k,代表每k秒光标整体循环右移一格。
现在问你拿到钻石的最少步数。
思路:
本弱开了一个四维数组判重use[x][y][f][l] 在(x,y)位置光标在f,面板移动了l次。
然后搜就可以了~
代码:
#include"stdio.h"
#include"algorithm"
#include"string.h"
#include"iostream"
#include"queue"
#include"map"
#include"string"
#define mod 1000000007
using namespace std;
int n,m,k;
char mp[12][12];
int used[12][12][5][5];
int dis[4][2]= {{0,-1},{0,1},{-1,0},{1,0}};
struct node
{
int x,y,f,l,t;
};
int ok(node a)
{
if(a.x<0 || a.y<0 || a.x>=n || a.y>=m) return 0;
if(used[a.x][a.y][a.f][a.l]==1) return 0;
if(mp[a.x][a.y]=='*') return 0;
return 1;
}
int bfs(int x,int y)
{
memset(used,0,sizeof(used));
node cur,next;
cur.x=x;
cur.y=y;
cur.t=0;
cur.f=0;
cur.l=0;
queue<node>q;
q.push(cur);
used[x][y][0][0]=1;
while(!q.empty())
{
cur=q.front();
q.pop();
if(mp[cur.x][cur.y]=='$') return cur.t;
//不动
next=cur;
next.t=cur.t+1;
next.l=(next.t/k)%4;
if(ok(next))
{
used[next.x][next.y][next.f][next.l]=1;
q.push(next);
}
//光标
next=cur;
next.t=cur.t+1;
next.f=(next.f+1)%4;
next.l=(next.t/k)%4;
if(ok(next))
{
used[next.x][next.y][next.f][next.l]=1;
q.push(next);
}
next=cur;
next.t=cur.t+1;
next.f=(next.f-1+4)%4;
next.l=(next.t/k)%4;
if(ok(next))
{
used[next.x][next.y][next.f][next.l]=1;
q.push(next);
}
//移动
next.x=cur.x+dis[(cur.f-cur.l+4)%4][0];
next.y=cur.y+dis[(cur.f-cur.l+4)%4][1];
next.f=cur.f;
next.t=cur.t+1;
next.l=(next.t/k)%4;
if(ok(next))
{
used[next.x][next.y][next.f][next.l]=1;
q.push(next);
}
}
return -1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int sx,sy;
scanf("%d%d%d",&n,&m,&k);
for(int i=0; i<n; i++) scanf("%s",mp[i]);
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(mp[i][j]=='@')
{
sx=i;
sy=j;
}
}
}
int ans=bfs(sx,sy);
if(ans==-1) puts("YouBadbad");
else printf("%d\n",ans);
}
return 0;
}