题解 P2226 【[HNOI2001]遥控赛车比赛】
题目link
BFS,半裸题?(关于码风?我真的不是故意的才怪)
#include<bits/stdc++.h>
#define CHECK if(x>n||y>m||x<1||y<1||!s[x][y]||(x==sx&&y==sy))
#define FX int fx[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
#define INT int n,m,sx,sy,ex,ey,s[110][110],dis[110][110][4],f[110][110][4],res;
#define NODE struct node{int x,y,z;};
#define FRE freopen("Racing.in","r",stdin);freopen("Racing.out","w",stdout);
#define FIRST_PUSH while(!q.empty()){q.pop();}for(int i=0;i<=3;i++){q.push((node){sx,sy,i});}
#define INPUT scanf("%d%d",&n,&m);scanf("%d%d%d%d",&sx,&sy,&ex,&ey);for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){scanf("%d",&s[i][j]);}}
#define WORK for(int i=1;i<=10;i++){memset(f,0,sizeof(f));memset(dis,0,sizeof(dis));res=BFS(i);if(res==-1){break;}printf("%d %d\n",i,res);}
using namespace std;
FX
INT
NODE
int BFS(int X)
{
queue<node>q;
FIRST_PUSH
f[sx][sy][0]=0;
f[sx][sy][1]=0;
f[sx][sy][2]=0;
f[sx][sy][3]=0;
while(!q.empty())
{
node now=q.front();
q.pop();
for(int i=0;i<4;i++)
{
int x=now.x+fx[i][0];
int y=now.y+fx[i][1];
CHECK
{
continue;
}
int a=0;
if(i==now.z)
{
a=min(X,f[now.x][now.y][now.z]+1);
}
else
{
if(f[now.x][now.y][now.z]!=X)
{
continue;
}
a=1;
}
if(dis[x][y][i]&&a<=f[x][y][i])
{
continue;
}
f[x][y][i]=a;
q.push((node){x,y,i});
dis[x][y][i]=dis[now.x][now.y][now.z]+1;
if(x==ex&&y==ey)
{
return dis[x][y][i];
}
}
}
return -1;
}
int main()
{
//FRE
INPUT
WORK
}