#include <iostream>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
struct Robot {
int x, y; // position
int dir; // direction
int time; // spend time
} robot;
int main() {
queue<Robot> one;
int posX[]={0,-1,0,1};
int posY[]={-1,0,1,0};
int step[]={1,2,3};
int n, m;
int rX, rY, eX, eY;
int map[55][55], dotMap[55][55];
cin>>n>>m;
for (int i=1;i<=n;++i) {
for (int j=1;j<=m;++j) {
cin>>map[i][j];
if (map[i][j] == 1) {
map[i-1][j]=1;
map[i][j-1]=1;
map[i-1][j-1]=1;
}
}
}
cin>>rX>>rY>>eX>>eY;
int dirStep;
string dir;
cin>>dir;
if (dir=="S") dirStep=0;
if (dir=="W") dirStep=1;
if (dir=="N") dirStep=2;
if (dir=="E") dirStep=3;
one.push((Robot){rX, rY, dirStep, 0});
map[rX][rY] = 1;
while (!one.empty()) {
robot = one.front();
one.pop();
for (int st : step) {
for (int i=0;i<4;++i) {
int nx=robot.x+posX[i]*st;
int ny=robot.y+posY[i]*st;
if (nx < 1 || ny < 1 || nx > n || ny > m || map[nx][ny] == 1) continue;
if (robot.x==eX&&robot.y==eY) {
cout<<robot.time;
return 0;
}
int time = robot.time + 1 + (robot.dir != i ? 1 : 0);
if (robot.dir != i) robot.dir=i;
one.push((Robot){nx, ny, robot.dir, time});
map[nx][ny]=1;
}
}
}
cout<<-1;
return 0;
}
我看着没问题,球大佬解答(写了好久了QAQ) 虽然最基础的输入输出不对,丢进去有30分
#include <iostream>
#include <queue>
#include <string>
#include <cmath>
using namespace std;
struct Robot {
int x, y; // position
int dir; // direction
int time; // spend time
} robot;
int main() {
queue<Robot> one;
int posX[]={0,-1,0,1};
int posY[]={-1,0,1,0};
int step[]={1,2,3};
int n, m;
int rX, rY, eX, eY;
int map[55][55], dotMap[55][55];
cin>>n>>m;
for (int i=1;i<=n;++i) {
for (int j=1;j<=m;++j) {
cin>>map[i][j];
if (map[i][j] == 1) {
map[i-1][j]=1;
map[i][j-1]=1;
map[i-1][j-1]=1;
}
}
}
cin>>rX>>rY>>eX>>eY;
int dirStep;
string dir;
cin>>dir;
if (dir=="S") dirStep=0;
if (dir=="W") dirStep=1;
if (dir=="N") dirStep=2;
if (dir=="E") dirStep=3;
one.push((Robot){rX, rY, dirStep, 0});
map[rX][rY] = 1;
while (!one.empty()) {
robot = one.front();
one.pop();
for (int st : step) {
for (int i=0;i<4;++i) {
int nx=robot.x+posX[i]*st;
int ny=robot.y+posY[i]*st;
if (nx < 1 || ny < 1 || nx > n || ny > m || map[nx][ny] == 1) continue;
if (robot.x==eX&&robot.y==eY) {
cout<<robot.time;
return 0;
}
int time = robot.time + 1 + (robot.dir != i ? 1 : 0);
if (robot.dir != i) robot.dir=i;
one.push((Robot){nx, ny, robot.dir, time});
map[nx][ny]=1;
}
}
}
cout<<-1;
return 0;
}
原来代码加了个边界还是不行www