比赛的时候题意理解错了,不想多说什么了。。
水题一道,,给你四个按钮,每次你可以选择不动,左右移动按钮,按钮每P秒右移一次,,水啊
#include "stack"
#include "cstdio"
#include "iostream"
#include "cmath"
#include "set"
#include "sstream"
#include "cctype"
#include "string"
#include "cstring"
#include "algorithm"
#include "queue"
#include "map"
using namespace std;
int n,m,k;
char mp[12][12];
int vis[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 judge(node a)
{
if(a.x<0 || a.y<0 || a.x>=n || a.y>=m)
return 0;
if(vis[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(vis,0,sizeof(vis));
node cur,next;
cur.x=x;
cur.y=y;
cur.t=0;
cur.f=0;
cur.l=0;
queue<node>q;
q.push(cur);
vis[x][y][0][0]=1;
while(!q.empty())
{
cur=q.front();
q.pop();
if(mp[cur.x][cur.y]=='$')
return cur.t;
//一共四种选择,一个个对应下来进行bfs就好了
next=cur;
next.t=cur.t+1;
next.l=(next.t/k)%4;
if(judge(next))
{
vis[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(judge(next))
{
vis[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(judge(next))
{
vis[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(judge(next))
{
vis[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;
}
好想哭啊~!@~!#~!~~~