我太難了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;
}