我太难了qwq肝了两天,重构了一次代码。真的很菜。
算法标签:广搜
题目链接:https://www.luogu.org/problem/P1126
这题最坑的是:输入的是格子,不是点!!!需要把他转化成点图
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
bool a;
cin>>a;
if(a)
{
/*
由于这是格子图,需要把坐标的上,左,上左三个格子都给变成1
自己看看题目的那张图好好想想
*/
map[i][j]=1;
map[i-1][j-1]=1;
map[i-1][j]=1;
map[i][j-1]=1;
}
}
再好好看看那张图,你还会发现:尼玛边界也走不了于是:
for(i=1;i<=n;i++)map[i][m]=1;
for(i=1;i<=m;i++)map[n][i]=1;
有没有想过为什么只有下面和右边被封住了?因为格子变成点了,左边和上边被扩展了一个单位,所以只需要封住下面和右边就行了
其余也没什么好讲的了,就是模拟一下他的操作。
スタート:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
bool map[600][600];
int n,m;
char face;//初始朝向
int sx,sy,fx,fy;//起终点坐标
int head=1,tail=1;
bool vis[600][600][5];//n s e w 1 2 3 4
//vis[x][y][z] = 1 表示在(x,y)时z状态已经遍历过
int cnm;
struct step//结构体
{
int x,y;//坐标
char r_status;//当前状态
int step;
int father;//他的上一个状态,调试用
}a[500000];//多开点,不然第九个点会溢出
void init()
{
int i,j;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
bool a;
cin>>a;
if(a)
{
map[i][j]=1;
map[i-1][j-1]=1;
map[i-1][j]=1;
map[i][j-1]=1;
}
}
cin>>sx>>sy>>fx>>fy>>face;
}
bool in_map(int x,int y){if(x<1 || y<1 || x>n || y>m)return true;return false;}
int charts(char x)
{
//人为地将东南西北转化成1 2 3 4
if(x =='N')return 1;
else if(x =='S')return 2;
else if(x =='W')return 3;
else return 4;
}
int charts_x(int x,int status)
{
if(status == 1)return -x;
else if(status == 2)return x;
else return 0;
}
int charts_y(int x,int status)
{
if(status == 3)return -x;
else if(status == 4)return x;
else return 0;
}
bool you_can_go_there(int code)//判断当前状态地点在走code步时会不会遇到障碍
{
int i;
int tmpx=a[head].x;
int tmpy=a[head].y;
char ss =a[head].r_status;
for(i=1;i<=code;i++)
{
if(ss == 'N' && map[tmpx-i][tmpy])return 1;
if(ss == 'S' && map[tmpx+i][tmpy])return 1;
if(ss == 'W' && map[tmpx][tmpy-i])return 1;
if(ss == 'E' && map[tmpx][tmpy+i])return 1;
}
return 0;
}
void creep()//这几个操作函数一样,就是前进的步数不一样
{
//简化写代码的工作量
int xx = a[head].x;
int yy = a[head].y;
char ss = a[head].r_status;
int sss = charts(ss);//主要判断vis[][][]用
//通过这个函数直接获得下一个状态的坐标,少些4个if
int n_xx = xx+charts_x(1,sss);
int n_yy = yy+charts_y(1,sss);
if(vis[n_xx][n_yy][sss] || in_map(n_xx,n_yy) || you_can_go_there(1))return;
vis[n_xx][n_yy][sss] = 1;
a[tail].r_status = ss;
a[tail].x = n_xx;
a[tail].y = n_yy;
a[tail].step = a[head].step + 1;
// just to save load
a[tail].father = head;
tail++;
}
void walk()
{
int xx = a[head].x;
int yy = a[head].y;
char ss = a[head].r_status;
int sss = charts(ss);
int n_xx = xx+charts_x(2,sss);
int n_yy = yy+charts_y(2,sss);
if(vis[n_xx][n_yy][sss] || in_map(n_xx,n_yy) || you_can_go_there(2))return;
vis[n_xx][n_yy][sss] = 1;
a[tail].r_status = ss;
a[tail].x = n_xx;
a[tail].y = n_yy;
a[tail].step = a[head].step + 1;
// just to save load
a[tail].father = head;
tail++;
}
void run()
{
int xx = a[head].x;
int yy = a[head].y;
char ss = a[head].r_status;
int sss = charts(ss);
int n_xx = xx+charts_x(3,sss);
int n_yy = yy+charts_y(3,sss);
if(vis[n_xx][n_yy][sss] || in_map(n_xx,n_yy) || you_can_go_there(3))return;
vis[n_xx][n_yy][sss] = 1;
a[tail].r_status = ss;
a[tail].x = n_xx;
a[tail].y = n_yy;
a[tail].step = a[head].step + 1;
// just to save load
a[tail].father = head;
tail++;
}
char turn_it_right(int sss)
{
if(sss == 1)return 'E';
else if(sss == 2)return 'W';
else if(sss == 3)return 'N';
else return 'S';
}
char turn_it_left(int sss)
{
if(sss == 1)return 'W';
else if(sss == 2)return 'E';
else if(sss == 3)return 'S';
else return 'N';
}
void turn_left()
{
int xx = a[head].x;
int yy = a[head].y;
char ss = a[head].r_status;
int sss = charts(ss);
char n_s= turn_it_left(sss);//寻找下一个坐标
sss = charts(n_s);
if(vis[xx][yy][sss])return;
vis[xx][yy][sss] = 1;
a[tail].r_status = n_s;
a[tail].x = xx;
a[tail].y = yy;
a[tail].step = a[head].step + 1;
// just to save load
a[tail].father = head;
tail++;
}
void turn_right()
{
int xx = a[head].x;
int yy = a[head].y;
char ss = a[head].r_status;
int sss = charts(ss);
char n_s= turn_it_right(sss);
sss = charts(n_s);
if(vis[xx][yy][sss])return;
vis[xx][yy][sss] = 1;
a[tail].r_status = n_s;
a[tail].x = xx;
a[tail].y = yy;
a[tail].step = a[head].step + 1;
// just to save load
a[tail].father = head;
tail++;
}
void bfs()
{
//初始化队列
a[1].x = sx;
a[1].y = sy;
a[1].r_status = face;
vis[sx][sy][charts(face)]=1;
tail++;
while(head<tail)
{
int xx = a[head].x;
int yy = a[head].y;
char ss = a[head].r_status;
int sss = charts(ss);
vis[xx][yy][sss] = 1;
if(a[head].x == fx && a[head].y == fy){printf("%d",a[head].step);cnm=2;return;}
creep();
walk();
run();
turn_right();
turn_left();
head++;
}
}
void fuck()
{
int i;
for(i=1;i<=n;i++)map[i][m]=1;
for(i=1;i<=m;i++)map[n][i]=1;
}
int main()
{
init();
fuck();
if(map[sx][sy]){printf("-1");return 0;}
bfs();
if(cnm==2)return 0;//有结果就直接结束
printf("-1");//第八个点是-1
return 0;
}