RE了无数次,貌似是数组开小了。
状态为坐标+下一个方向
代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int r,c;
char map[501+10][501+10];
struct state
{
int x,y,dir;
int step;
state()
{}
state(int xx,int yy,int dirr,int stepp):x(xx),y(yy),dir(dirr),step(stepp)
{
}
};
state s;
state q[500*500*11];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int mark[510][510][10];
void bfs()
{
bool flag = false;
memset(mark,0,sizeof(mark));
state cur;
int front = -1;
int rear = -1;
q[++rear] = s;
while(front<rear)
{
cur = q[++front];
int curx = cur.x;
int cury = cur.y;
int curStep = cur.step;
int curDir = cur.dir;
if(mark[curx][cury][curDir])
{
continue;
}
if(curDir==4&&map[curx][cury]=='O')
{
flag = true;
printf("%d\n",curStep);
break;
}
mark[curx][cury][curDir] =1;
if(curDir==4)
{
for(int i=0;i<4;i++)
{
int ttx = curx + dx[i];
int tty = cury + dy[i];
int ttx1 = ttx + dx[i];
int tty1 = tty + dy[i];
if(ttx>=0&&ttx<r&&tty>=0&&tty<c&&map[ttx][tty]!='#'&&
ttx1>=0&&ttx1<r&&tty1>=0&&tty1<c&&map[ttx1][tty1]!='#')
{
q[++rear] = state(ttx,tty,i,curStep+1);
}
}
}
else
{
int ttx1 = curx;
int tty1 = cury;
int ttx2 = curx + dx[curDir];
int tty2 = cury + dy[curDir];
ttx1 += dx[(curDir+2)%4];
tty1 += dy[(curDir+2)%4];
ttx2 += dx[curDir];
tty2 += dy[curDir];
if(ttx1>=0&&ttx1<r&&tty1>=0&&tty1<c&&map[ttx1][tty1]!='#'&&map[ttx1][tty1]!='E')
{
q[++rear] = state(ttx1,tty1,4,curStep+1);
}
if(ttx2>=0&&ttx2<r&&tty2>=0&&tty2<c&&map[ttx2][tty2]!='#'&&map[ttx2][tty2]!='E')
{
q[++rear] = state(ttx2,tty2,4,curStep+1);
}
int tt =(curDir+1)%4;
ttx1 = curx;
tty1 = cury;
ttx2 = curx + dx[curDir];
tty2 = cury + dy[curDir];
ttx1 += dx[tt];
tty1 += dy[tt];
ttx2 += dx[tt];
tty2 += dy[tt];
if(ttx1>=0&&ttx1<r&&tty1>=0&&tty1<c&&map[ttx1][tty1]!='#'
&&ttx2>=0&&ttx2<r&&tty2>=0&&tty2<c&&map[ttx2][tty2]!='#')
{
q[++rear] = state(ttx1,tty1,curDir,curStep+1);
}
ttx1 += 2*dx[(tt+2)%4];
tty1 += 2*dy[(tt+2)%4];
ttx2 += 2*dx[(tt+2)%4];
tty2 += 2*dy[(tt+2)%4];
if(ttx1>=0&&ttx1<r&&tty1>=0&&tty1<c&&map[ttx1][tty1]!='#'&&ttx2>=0&&ttx2<r&&
tty2>=0&&tty2<c&&map[ttx2][tty2]!='#')
{
q[++rear] = state(ttx1,tty1,curDir,curStep+1);
}
}
}
if(!flag)
{
printf("Impossible\n");
}
}
int main()
{
int tx1,ty1,tx2,ty2,tdir;
while(scanf("%d%d",&r,&c)!=EOF&&!(r==0&&c==0))
{
int tcnt = 0;
for(int i=0;i<r;i++)
{
scanf("%s",map[i]);
for(int j=0;j<c;j++)
{
if(map[i][j]=='X')
{
if(tcnt==0)
{
tx1 = i;ty1= j;
tcnt++;
}
else
{
tx2 = i;ty2=j;
tcnt++;
}
}
}
}
if(tcnt==1)
{
s = state(tx1,ty1,4,0);
}
else
{
int t1 = tx2 - tx1;int t2 = ty2-ty1;
if(t1==-1&&t2==0)
{
tdir = 0;
}
else if(t1==0&&t2==1)
{
tdir = 1;
}
else if(t1==1&&t2==0)
{
tdir = 2;
}
else if(t1==0&&t2==-1)
{
tdir = 3;
}
s = state(tx1,ty1,tdir,0);
}
bfs();
}
return 0;
}
#include <cstring>
#include <cstdio>
using namespace std;
int r,c;
char map[501+10][501+10];
struct state
{
int x,y,dir;
int step;
state()
{}
state(int xx,int yy,int dirr,int stepp):x(xx),y(yy),dir(dirr),step(stepp)
{
}
};
state s;
state q[500*500*11];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int mark[510][510][10];
void bfs()
{
bool flag = false;
memset(mark,0,sizeof(mark));
state cur;
int front = -1;
int rear = -1;
q[++rear] = s;
while(front<rear)
{
cur = q[++front];
int curx = cur.x;
int cury = cur.y;
int curStep = cur.step;
int curDir = cur.dir;
if(mark[curx][cury][curDir])
{
continue;
}
if(curDir==4&&map[curx][cury]=='O')
{
flag = true;
printf("%d\n",curStep);
break;
}
mark[curx][cury][curDir] =1;
if(curDir==4)
{
for(int i=0;i<4;i++)
{
int ttx = curx + dx[i];
int tty = cury + dy[i];
int ttx1 = ttx + dx[i];
int tty1 = tty + dy[i];
if(ttx>=0&&ttx<r&&tty>=0&&tty<c&&map[ttx][tty]!='#'&&
ttx1>=0&&ttx1<r&&tty1>=0&&tty1<c&&map[ttx1][tty1]!='#')
{
q[++rear] = state(ttx,tty,i,curStep+1);
}
}
}
else
{
int ttx1 = curx;
int tty1 = cury;
int ttx2 = curx + dx[curDir];
int tty2 = cury + dy[curDir];
ttx1 += dx[(curDir+2)%4];
tty1 += dy[(curDir+2)%4];
ttx2 += dx[curDir];
tty2 += dy[curDir];
if(ttx1>=0&&ttx1<r&&tty1>=0&&tty1<c&&map[ttx1][tty1]!='#'&&map[ttx1][tty1]!='E')
{
q[++rear] = state(ttx1,tty1,4,curStep+1);
}
if(ttx2>=0&&ttx2<r&&tty2>=0&&tty2<c&&map[ttx2][tty2]!='#'&&map[ttx2][tty2]!='E')
{
q[++rear] = state(ttx2,tty2,4,curStep+1);
}
int tt =(curDir+1)%4;
ttx1 = curx;
tty1 = cury;
ttx2 = curx + dx[curDir];
tty2 = cury + dy[curDir];
ttx1 += dx[tt];
tty1 += dy[tt];
ttx2 += dx[tt];
tty2 += dy[tt];
if(ttx1>=0&&ttx1<r&&tty1>=0&&tty1<c&&map[ttx1][tty1]!='#'
&&ttx2>=0&&ttx2<r&&tty2>=0&&tty2<c&&map[ttx2][tty2]!='#')
{
q[++rear] = state(ttx1,tty1,curDir,curStep+1);
}
ttx1 += 2*dx[(tt+2)%4];
tty1 += 2*dy[(tt+2)%4];
ttx2 += 2*dx[(tt+2)%4];
tty2 += 2*dy[(tt+2)%4];
if(ttx1>=0&&ttx1<r&&tty1>=0&&tty1<c&&map[ttx1][tty1]!='#'&&ttx2>=0&&ttx2<r&&
tty2>=0&&tty2<c&&map[ttx2][tty2]!='#')
{
q[++rear] = state(ttx1,tty1,curDir,curStep+1);
}
}
}
if(!flag)
{
printf("Impossible\n");
}
}
int main()
{
int tx1,ty1,tx2,ty2,tdir;
while(scanf("%d%d",&r,&c)!=EOF&&!(r==0&&c==0))
{
int tcnt = 0;
for(int i=0;i<r;i++)
{
scanf("%s",map[i]);
for(int j=0;j<c;j++)
{
if(map[i][j]=='X')
{
if(tcnt==0)
{
tx1 = i;ty1= j;
tcnt++;
}
else
{
tx2 = i;ty2=j;
tcnt++;
}
}
}
}
if(tcnt==1)
{
s = state(tx1,ty1,4,0);
}
else
{
int t1 = tx2 - tx1;int t2 = ty2-ty1;
if(t1==-1&&t2==0)
{
tdir = 0;
}
else if(t1==0&&t2==1)
{
tdir = 1;
}
else if(t1==1&&t2==0)
{
tdir = 2;
}
else if(t1==0&&t2==-1)
{
tdir = 3;
}
s = state(tx1,ty1,tdir,0);
}
bfs();
}
return 0;
}