代码已经过玄学修改,WA on 8
```cpp
#include<bits/stdc++.h>
using namespace std;
int n,m,go[105][105],sx,sy,ex,ey,tot;
bool vis[105][105][10];
char ch;
struct data{
int x,y,w,ans,is;
};
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<48||ch>57){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>=48&&ch<=57){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
bool pd(int x,int y,int w,int l){
if(x<=0||y<=0)return false;
if(w==1){
if(vis[x][y+l][w]||y+l>=m)return false;
for(int i=1;i<=l;++i){
if(go[x][y+i]){
return false;
}
}
}
if(w==2){
if(vis[x+l][y][w]||x+l>=n)return false;
for(int i=1;i<=l;++i){
if(go[x+i][y]){
return false;
}
}
}
if(w==3){
if(vis[x][y-l][w]||y-l<=0||y<=0)return false;
for(int i=1;i<=l;++i){
if(go[x][y-i]){
return false;
}
}
}
if(w==4){
if(vis[x-l][y][w]||x-l<=0||x<=0)return false;
for(int i=1;i<=l;++i){
if(go[x-i][y]){
return false;
}
}
}
return true;
}
void bfs(int x1,int y1,int way){
memset(vis,false,sizeof(vis));
queue<data>q;
q.push({x1,y1,way,0,0});
vis[x1][y1][way]=true;
while(!q.empty()){
int x=q.front().x;
int y=q.front().y;
int w=q.front().w;
int ans=q.front().ans;
q.pop();
if(x==ex&&y==ey){
cout<<ans;
exit(0);
}
for(int i=1;i<=4;++i){
for(int j=3;j>=1;--j){
int is=q.front().is;
if(i==1){
if(pd(x,y,i,j)){
int yy=y+j;
int cnt=ans;
if(w==3&&x==sx&&y==sy&&is==0){
q.push({x,yy,2,cnt+1,1});
break;
}
else if(w==3)continue;
else if(w!=1)++cnt;
q.push({x,yy,i,cnt+1,1});
vis[x][yy][i]=true;
}
}
if(i==2){
if(pd(x,y,i,j)){
int xx=x+j;
int cnt=ans;
if(w==4&&x==sx&&y==sy&&is==0){
q.push({xx,y,2,cnt+1,1});
break;
}
else if(w==4)break;
else if(w!=2)++cnt;
q.push({xx,y,i,cnt+1,1});
vis[xx][y][i]=true;
}
}
if(i==3){
if(pd(x,y,i,j)){
int yy=y-j;
int cnt=ans;
if(w==1&&x==sx&&y==sy&&is==0){
q.push({x,yy,4,cnt+1,1});
break;
}
else if(w==1)break;
else if(w!=3)++cnt;
q.push({x,yy,i,cnt+1,1});
vis[x][yy][i]=true;
}
}
if(i==4){
if(pd(x,y,i,j)){
int xx=x-j;
int cnt=ans;
if(w==2&&x==sx&&y==sy&&is==0){
q.push({xx,y,1,cnt+1,1});
break;
}
else if(w==2)break;
else if(w!=4)++cnt;
q.push({xx,y,i,cnt+1,1});
vis[xx][y][i]=true;
}
}
}
}
}
}
signed main(){
n=read();m=read();
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
int a;
a=read();
if(a){
go[i][j]=1;
go[i+1][j]=1;
go[i][j+1]=1;
go[i+1][j+1]=1;
}
}
}
sx=read();sy=read();ex=read();ey=read();
cin>>ch;
int w;
if(ch=='E')w=1;
if(ch=='S')w=2;
if(ch=='W')w=3;
if(ch=='N')w=4;
bfs(sx,sy,w);
cout<<-1;
return 0;
}
```
by wsdyz2010 @ 2023-09-29 21:31:16
似乎知道错误的点了:不能一边转弯一边走路,不然答案小的反而有可能在后面
by wsdyz2010 @ 2023-09-30 23:16:03
这都能AC 8个点也是离谱的……
by wsdyz2010 @ 2023-09-30 23:16:39
```cpp
P1126 机器人搬重物 AC[代码]:
#include <bits/stdc++.h>
#define rint register int
using namespace std;
struct data{
int x,y;
char c;
}q[10005];
int n,m,sx,sy,ex,ey,dis[55][55][5],head,tail;
char w;
bool Map[55][55],vis[55][55][5];
const inline int min(int x,int y){
return x<y ? x:y;
}
const inline int max(int x,int y){
return x>y ? x:y;
}
const inline int what(char c){
if(c=='E')return 1;
if(c=='S')return 2;
if(c=='W')return 3;
return 4;
}
const inline void change(int &a,int &b,char &c,int k){
if(what(c)==1){
if(k==1)b++;
else if(k==2)b+=2;
else if(k==3)b+=3;
else if(k==4)c='N';
else c='S';
}
else if(what(c)==2){
if(k==1)a++;
else if(k==2)a+=2;
else if(k==3)a+=3;
else if(k==4)c='E';
else c='W';
}
else if(what(c)==3){
if(k==1)b--;
else if(k==2)b-=2;
else if(k==3)b-=3;
else if(k==4)c='S';
else c='N';
}
else{
if(k==1)a--;
else if(k==2)a-=2;
else if(k==3)a-=3;
else if(k==4)c='W';
else c='E';
}
return ;
}
const inline bool check(int ax,int ay,int bx,int by){
if(ax==bx){
for(rint p=min(ay,by);p<=max(ay,by);p++)if(Map[ax][p])return false;
return true;
}
else{
for(rint p=min(ax,bx);p<=max(ax,bx);p++)if(Map[p][ay])return false;
return true;
}
}
const inline void bfs(){
memset(dis,63,sizeof dis);
dis[sx][sy][what(w)]=0;
vis[sx][sy][what(w)]=true;
head=tail=1;
q[head].x=sx;
q[head].y=sy;
q[head].c=w;
rint a,b,nx,ny;
char c,nc;
while(head<=tail){
a=q[head].x;
b=q[head].y;
c=q[head].c;
for(rint i=1;i<=5;i++){
nx=a;
ny=b;
nc=c;
change(nx,ny,nc,i);
if(nx>1&&nx<n&&ny>1&&ny<m&&check(a,b,nx,ny)&&!vis[nx][ny][what(nc)]&&(dis[a][b][what(c)]+1<dis[nx][ny][what(nc)]||dis[nx][ny][what(nc)]>1e9)){
vis[nx][ny][what(nc)]=true;
dis[nx][ny][what(nc)]=dis[a][b][what(c)]+1;
if(nx==ex&&ny==ey){
cout<<dis[nx][ny][what(nc)];
exit(0);
}
else q[++tail].x=nx,q[tail].y=ny,q[tail].c=nc;
}
}
head++;
}
return ;
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
rint X;
for(rint i=1;i<=n;i++){
for(rint j=1;j<=m;j++){
cin>>X;
if(X)Map[i][j]=Map[i+1][j]=Map[i][j+1]=Map[i+1][j+1]=true;
}
}
cin>>sx>>sy>>ex>>ey>>w;
if(sx==ex&&sy==ey){
cout<<0;
return 0;
}
sx++;
sy++;
ex++;
ey++;
n++;
m++;
bfs();
cout<<-1;
return 0;
}
```
by chenyanliang @ 2023-10-01 17:25:51