[洛谷] P1126 机器人搬重物(BFS搜图好题) Apare_xzc
程序员文章站
2024-03-17 17:52:58
...
[洛谷] P1126 机器人搬重物(BFS搜图好题)
题目链接
/*
洛谷P1126 机器人搬重物
Apare_xzc 2019.12.8
编程语言 代码长度 用时 内存
C++ 3.82KB 34ms 784.00KB
*/
#include <bits/stdc++.h>
using namespace std;
int maze[100][100],sx,sy,ex,ey,m,n,sdir;
bool vis[100][100][4];
char ddd[3];
struct Node{ //结构体用于存储bfs时队列中存储的状态(坐标,方向,时间)
int x,y,step,dir;//横坐标,总坐标,用时,方向
Node(int _x=0,int _y=0,int _step=0,int _dir=0):x(_x),y(_y),step(_step),dir(_dir){}
}node;
bool ok(int x,int y,int dir);//可以走
bool out(int x,int y);//出界了
pair<int,int> move(int x,int y,int dir,int len);//返回停下的位置
bool checkCreep(int xx,int yy,int dir,int &nx,int &ny);//走一步
bool checkWalk(int xx,int yy,int dir,int& nx,int& ny); //走两步
bool checkRun(int xx,int yy,int dir,int& nx,int &ny); //走三步
inline int toRight(int dir){return (dir+1)%4;} //右转
inline int toLeft(int dir){return (dir+3)%4;} //左转
void bfs();
int main()
{
int ca = 0;
while(cin>>n>>m)
{
if(ca++) memset(vis,0,sizeof(vis));
for(int i=1; i<=n; ++i)
for(int j=1;j<=m;++j)
scanf("%d",&maze[i][j]);
scanf("%d%d%d%d%s",&sx,&sy,&ex,&ey,ddd);
char sss[] = "ESWN666";
for(int i=0;i<4;++i) //确定开始的方向 ESWN对应0123
if(sss[i]==ddd[0]) sdir = i;
bfs();
}
return 0;
}
bool ok(int x,int y,int dir)
{
return (x>0&&x<=n&&y>0&&y<=m&&!vis[x][y][dir]&&maze[x][y]==0);
}
bool out(int x,int y)
{
return (x<=0||x>n||y<=0||y>m);
}
pair<int,int> move(int x,int y,int dir,int len)
{
switch (dir)
{
case 0: return make_pair(x,y+len);
case 1: return make_pair(x+len,y);
case 2: return make_pair(x,y-len);
case 3: return make_pair(x-len,y);
}
return make_pair(0,0);
}
bool checkCreep(int xx,int yy,int dir,int &nx,int &ny)
{
pair<int,int> pr = move(xx,yy,dir,1);
int x = pr.first, y = pr.second;
nx = x, ny = y;
if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;
if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
return !vis[x][y][dir];
}
bool checkWalk(int xx,int yy,int dir,int& nx,int& ny)
{
pair<int,int> pr = move(xx,yy,dir,1);
int x = pr.first, y = pr.second;
if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;
if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
pr = move(xx,yy,dir,2);
x = pr.first, y = pr.second;
if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;
if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
nx = x, ny = y;
return !vis[x][y][dir];
}
bool checkRun(int xx,int yy,int dir,int& nx,int &ny)
{
pair<int,int> pr = move(xx,yy,dir,1);
int x = pr.first, y = pr.second;
if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;
if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
pr = move(xx,yy,dir,2);
x = pr.first, y = pr.second;
if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;
if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
pr = move(xx,yy,dir,3);
x = pr.first, y = pr.second;
if(out(x,y)||out(x+1,y)||out(x,y+1)||out(x+1,y+1)) return false;
if(maze[x][y]||maze[x+1][y]||maze[x][y+1]||maze[x+1][y+1]) return false;
nx = x, ny = y;
return !vis[x][y][dir];
}
void bfs()
{
queue<Node> Q;
Q.push(Node(sx,sy,0,sdir));
vis[sx][sy][sdir] = true;
int ans = -1;
while(!Q.empty())
{
node = Q.front();
Q.pop();
int x = node.x, y = node.y;
int step = node.step, dir = node.dir;
if(x==ex&&y==ey)
{
ans = step;
break;
}
int nx=0,ny=0;
if(checkRun(x,y,dir,nx,ny))
{
vis[nx][ny][dir] = true;
Q.push(Node(nx,ny,step+1,dir));
}
if(checkWalk(x,y,dir,nx,ny))
{
vis[nx][ny][dir] = true;
Q.push(Node(nx,ny,step+1,dir));
}
if(checkCreep(x,y,dir,nx,ny))
{
vis[nx][ny][dir] = true;
Q.push(Node(nx,ny,step+1,dir));
}
if(!vis[x][y][toLeft(dir)])
{
vis[x][y][toLeft(dir)] = true;
Q.push(Node(x,y,step+1,toLeft(dir)));
}
if(!vis[x][y][toRight(dir)])
{
vis[x][y][toRight(dir)] = true;
Q.push(Node(x,y,step+1,toRight(dir)));
}
}
printf("%d\n",ans);
}