44pts help!!!!!!
  • 板块P2802 回家
  • 楼主AU57G6
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/8/8 14:23
  • 上次更新2023/10/27 16:28:01
查看原帖
44pts help!!!!!!
575492
AU57G6楼主2022/8/8 14:23
#include <bits/stdc++.h>
using namespace std;

const int LEN = 14;
struct Point{int x, y;};
Point start;
bool vis[LEN][LEN];
int n, m, bld[LEN][LEN], stp[LEN][LEN], ans = 1e9;
int mp[LEN][LEN];
queue<Point> que;
int dx[4]={+1, -1, +0, -0};
int dy[4]={+0, -0, +1, -1};

bool isLegal(Point v){
	return v.x >= 1 && v.y >= 1
		&& v.x <= n && v.y <= n
		&& !(mp[v.x][v.y] == 0)
		&& !vis[v.x][v.y]
		&& bld[v.x][v.y] != 0;
}

void BFS(){
    que.push(start);
    while(!que.empty()){
        Point p = que.front();
        que.pop();
        if(mp[p.x][p.y] == 3){
            ans = min(ans, stp[p.x][p.y]);
            continue;
        }
        for(int i = 0; i < 4; ++i){
            Point np;
            np.x = p.x + dx[i], np.y = p.y + dy[i];
            if(isLegal(np)){
                vis[np.x][np.y] = true;
                bld[np.x][np.y] = bld[p.x][p.y] - 1;
                stp[np.x][np.y] = stp[p.x][p.y] + 1;
                if(mp[np.x][np.y] == 4) ++bld[np.x][np.y];
				que.push(np);
        	}
        }
    }
}

int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= m; ++j){
            cin >> mp[i][j];
            if(mp[i][j] == 2){
                start = Point{i, j};
                bld[i][j] = 6;
                vis[i][j] = true;
                stp[i][j] = 0;
            }
        }
    }
    BFS();
    if(ans == 1e9) cout << -1 << endl;
    else cout << ans << endl;
}
2022/8/8 14:23
加载中...