P1126 機器人搬重物
傳送門
這道題本來沒啥好說的,但細節實在比較多,被坑了好多次,
- 首先輸入的是格子圖,需要轉化成點圖,具體操作是
- 最坑的一個點在于,平時寫寬搜的時候,遇到出邊界或者不能訪問的點時,都是直接進入下一層回圈(continue),但在這道題中,由于可以走1~3步,那么當路徑上出現障礙時,則不能進行下一輪回圈,需要break,
代碼:
#include <bits/stdc++.h>
#define MAX 55
using namespace std;
int mod(int x){
return (x+4)%4;
}
struct pt{
int x, y, dir, step;
pt(){}
pt(int a, int b, int c, int d):x(a), y(b), dir(c), step(d){}
};
const int movx[] = {0,1,0,-1}, movy[] = {1,0,-1,0};
int a[MAX][MAX];
bool vis[MAX][MAX][5];
int n, m;
pt st, ed;
void bfs(){
queue<pt> q;
bool flag = false;
st.step = 0;
q.push(st);
vis[st.x][st.y][st.dir] = true;
while(!q.empty()){
pt t = q.front();
q.pop();
if(t.x == ed.x && t.y == ed.y){
cout << t.step << endl;
flag = true;
break;
}
for(int i = 1; i <= 3; i++){
int u, v;
u = t.x + i*movx[t.dir];
v = t.y + i*movy[t.dir];
if(u<=0 || u>=n || v<=0 || v>=m || a[u][v] == 1){
break;
}
if(vis[u][v][t.dir]){
continue;
}
vis[u][v][t.dir] = true;
q.push(pt(u, v, t.dir, t.step+1));
}
if(!vis[t.x][t.y][mod(t.dir+1)]){
vis[t.x][t.y][mod(t.dir+1)] = true;
q.push(pt(t.x, t.y, mod(t.dir+1), t.step+1));
}
if(!vis[t.x][t.y][mod(t.dir-1)]){
vis[t.x][t.y][mod(t.dir-1)] = true;
q.push(pt(t.x, t.y, mod(t.dir-1), t.step+1));
}
}
if(!flag){
cout << -1 << endl;
}
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
scanf("%d", &a[i][j]);
if(a[i][j] == 1){
a[i-1][j-1] = a[i-1][j] = a[i][j-1] = 1;
}
}
}
cin >> st.x >> st.y >> ed.x >> ed.y;
char c;
cin >> c;
switch(c){
case 'E':
st.dir = 0; break;
case 'S':
st.dir = 1; break;
case 'W':
st.dir = 2; break;
case 'N':
st.dir = 3; break;
}
bfs();
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/224266.html
標籤:其他
下一篇:2020-11-17
