我已红温——我不想写第四遍A*了!!!

不知道为什么反正很优的A*
#include <bits/stdc++.h>
using namespace std;
const int _ = 222, dx[] = {0, 1, - 1, 0}, dy[] = {1, 0, 0, - 1};
const char op[] = {'R', 'D', 'U', 'L'};
#define num(x, y) ((x - 1) * n + y)
int n, m, v[_][_], zhong;
string s;
bitset<_> t;
struct as{
	int x, y;
	char op;
};
inline int h(bitset<_> t){
	int as = 0;
	for(int i = 1; i <= n; i ++){
		for(int j = 1; j <= n; j ++){
			if(t[num(i, j)]){
				as += abs(i - (n + 1) / 2) + abs(j - (n + 1) / 2);
			}
		}
	}
	return as;
}
struct hhh{
	int f, bu;
	bitset<_> t;
	vector<as> c;
	bool operator < (const hhh &x) const {  
        return f > x.f;        
    }  
};
priority_queue<hhh> q;
unordered_map<bitset<_>, bool> mp;
inline void Astar(){
	while(q. size()){
		hhh nw = q. top();
		q. pop();
		bitset<_> t = nw. t;
//		cout<<t<<endl;
		
//		return ;
		if(t. count() == 1 && t[zhong] == 1){
			for(auto x : nw. c){
				cout << x. x << ' ' << x. y << ' ' << x. op << '\n';
			}
			return ;
		}//cout<<t[19]<<"RAIN";
		for(int i = 1; i <= n * n; i ++){
			
			if(t[i]){
				int x = (i - 1) / n + 1, y = (i - 1) % n + 1;
//				cout <<i<<' '<<x << ' ' << y<<'\n';
				for(int j = 0; j < 4; j ++){
					int xx = x + dx[j], yy = y + dy[j];
					int xxx = xx + dx[j], yyy = yy + dy[j];
					if(xxx > 0 && xxx <= n && yyy > 0 && yyy <= n && t[num(xx, yy)] && ! t[num(xxx, yyy)] && v[xxx][yyy] != - 1){
						t[num(xx, yy)] = t[i] = 0;
						t[num(xxx, yyy)] =  1;
						if(! mp[t]){
							mp[t] = 1;
							nw. c. emplace_back((as){x, y, op[j]});
							q. emplace((hhh){h(t) + nw. bu + 1, nw. bu + 1, t, nw. c});
							nw. c. pop_back();
						}
						t[num(xx, yy)] = t[i] = 1;
						t[num(xxx, yyy)] = 0;
					}
				}
			}
		}
	}
	return ;
}
int main(){
//	system("checker.exe yyychess4.in 4.out 4.out");
//	return 0;
//	freopen("yyychess1.in","r",stdin);
//	freopen("1.out","w",stdout);
	ios :: sync_with_stdio(0), cin. tie(0), cout. tie(0);
	cin >> n >> m;
	if(! m){
		for(int i = 1; i <= n; i ++){
			cin >> s;
			for(int j = 0; j < n; j ++){
				if(s[j] == 'x'){
					v[i][j + 1] = - 1;
				}
				if(s[j] == 'y'){
					t[num(i, j + 1)] = 1;
				}
			}
		}
	}
	mp[t] = 1;
	vector<as> a;
	a. clear();
	zhong = num((n + 1)/ 2, (n + 1)/ 2);
	q. emplace((hhh){0, 0, t, a});
	Astar();
	return 0;
}
让蓝色小鲸鱼优化结果反向优化的状物
#include <bits/stdc++.h>
using namespace std;
const int _ = 400, dx[] = {0, 1, - 1, 0}, dy[] = {1, 0, 0, - 1};
const char op[] = {'R', 'D', 'U', 'L'};
#define num(x, y) ((x - 1) * n + y)
int n, m, v[_][_], zhong;
string s;
bitset<_> t;
int cx, cy;
const int W = 3;

struct as{
	int x, y;
	char op;
};

inline int h(bitset<_> t){
	int as = 0;
	for(int i = 1; i <= n; i ++){
		for(int j = 1; j <= n; j ++){
			if(t[num(i, j)]){
				as += abs(i - cx) + abs(j - cy);
			}
		}
	}
	return as;
}

struct hhh{
	int f, bu, hh;
	int fa;
	int from_x, from_y;
	char mv;
	bitset<_> t;
	bool operator < (const hhh &x) const {
		return f > x. f;
	}
};

vector<hhh> pool;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
unordered_map<bitset<_>, int> mp;

inline void Astar(){
	while(q. size()){
		int idx = q. top(). second;
		q. pop();
		hhh nw = pool[idx];

		if(nw. t. count() == 1 && nw. t[zhong] == 1){
			vector<as> path;
			int i = idx;
			while(pool[i]. fa != -1){
				as tmp;
				tmp. x = pool[i]. from_x;
				tmp. y = pool[i]. from_y;
				tmp. op = pool[i]. mv;
				path. push_back(tmp);
				i = pool[i]. fa;
			}
			reverse(path. begin(), path. end());
			for(int k = 0; k < (int)path. size(); k ++){
				cout << path[k]. x << ' ' << path[k]. y << ' ' << path[k]. op << '\n';
			}
			return ;
		}

		bitset<_> t = nw. t;
		for(int i = 1; i <= n * n; i ++){
			if(t[i]){
				int x = (i - 1) / n + 1, y = (i - 1) % n + 1;
				for(int j = 0; j < 4; j ++){
					int xx = x + dx[j], yy = y + dy[j];
					int xxx = xx + dx[j], yyy = yy + dy[j];
					if(xxx > 0 && xxx <= n && yyy > 0 && yyy <= n && t[num(xx, yy)] && ! t[num(xxx, yyy)] && v[xxx][yyy] != - 1){
						t[num(xx, yy)] = t[i] = 0;
						t[num(xxx, yyy)] =  1;

						int nhh = nw. hh
							- abs(x   - cx) - abs(y   - cy)
							- abs(xx  - cx) - abs(yy  - cy)
							+ abs(xxx - cx) + abs(yyy - cy);
						int nbu = nw. bu + 1;

						unordered_map<bitset<_>, int> :: iterator it = mp. find(t);
						if(it == mp. end() || it-> second > nbu){
							mp[t] = nbu;

							hhh nn;
							nn. f = nbu + W * nhh;
							nn. bu = nbu;
							nn. hh = nhh;
							nn. fa = idx;
							nn. from_x = x;
							nn. from_y = y;
							nn. mv = op[j];
							nn. t = t;
							pool. push_back(nn);
							q. push(make_pair(nn. f, (int)pool. size() - 1));
						}

						t[num(xx, yy)] = t[i] = 1;
						t[num(xxx, yyy)] = 0;
					}
				}
			}
		}
	}
	return ;
}

int main(){
	system("checker.exe yyychess4.in 4.out 4.out");
	return 0;
	freopen("yyychess4.in","r",stdin);
	freopen("4.out","w",stdout);
	ios :: sync_with_stdio(0), cin. tie(0), cout. tie(0);
	cin >> n >> m;
	if(! m){
		for(int i = 1; i <= n; i ++){
			cin >> s;
			for(int j = 0; j < n; j ++){
				if(s[j] == 'x'){
					v[i][j + 1] = - 1;
				}
				if(s[j] == 'y'){
					t[num(i, j + 1)] = 1;
				}
			}
		}
	}

	cx = (n + 1) / 2;
	cy = (n + 1) / 2;
	zhong = num(cx, cy);

	mp[t] = 0;
	int h0 = h(t);

	hhh start;
	start. f = W * h0;
	start. bu = 0;
	start. hh = h0;
	start. fa = -1;
	start. from_x = 0;
	start. from_y = 0;
	start. mv = 0;
	start. t = t;
	pool. push_back(start);
	q. push(make_pair(start. f, 0));

	Astar();
	return 0;
}
让蓝色小鲸鱼写的(2.0)
#include <bits/stdc++.h>
using namespace std;
const int _ = 400, dx[] = {0, 1, -1, 0}, dy[] = {1, 0, 0, -1};
const char op[] = {'R', 'D', 'U', 'L'};
#define num(x, y) ((x - 1) * n + y)
int n, m, v[_][_], zhong;
string s;
bitset<_> t;
int cx, cy, total;
const int W = 3;

struct Node {
    int f, g, h;
    int fa;
    int from_x, from_y;
    char mv;
    bitset<_> st;
    bool operator < (const Node &x) const {
        return f > x.f;
    }
};

deque<Node> pool;   // 所有节点的仓库,deque 保证 push_back 不复制旧节点
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > q;
unordered_map<bitset<_>, int> mp;

// 全盘算一次 h(只在起点用一次)
inline int hval(const bitset<_> &st) {
    int r = 0;
    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= n; j ++)
            if(st[num(i,j)]) r += abs(i-cx) + abs(j-cy);
    return r;
}

inline void Astar() {
    while(q.size()) {
        int idx = q.top().second;
        q.pop();

        // 惰性删除:如果之前已用更小 g 到达过该局面,跳过
        unordered_map<bitset<_>, int>::iterator it0 = mp.find(pool[idx].st);
        if(it0 != mp.end() && it0->second < pool[idx].g) continue;

        Node nw = pool[idx];

        // 到达目标
        if(nw.st.count() == 1 && nw.st[zhong] == 1) {
            vector<int> ids;
            int i = idx;
            while(pool[i].fa != -1) {
                ids.push_back(i);
                i = pool[i].fa;
            }
            for(int k = (int)ids.size() - 1; k >= 0; k --) {
                cout << pool[ids[k]].from_x << ' '
                     << pool[ids[k]].from_y << ' '
                     << pool[ids[k]].mv << '\n';
            }
            return;
        }

        bitset<_> t = nw.st;
        for(int i = 1; i <= n * n; i ++) {
            if(!t[i]) continue;
            int x = (i - 1) / n + 1, y = (i - 1) % n + 1;
            for(int j = 0; j < 4; j ++) {
                int xx = x + dx[j], yy = y + dy[j];
                int xxx = xx + dx[j], yyy = yy + dy[j];
                if(xxx < 1 || xxx > n || yyy < 1 || yyy > n) continue;
                if(v[xxx][yyy] == -1) continue;
                if(!t[num(xx,yy)]) continue;
                if(t[num(xxx,yyy)]) continue;

                t[num(xx,yy)] = t[i] = 0;
                t[num(xxx,yyy)] = 1;

                int ng = nw.g + 1;

                // 增量更新 h:减起点贡献,减被吃贡献,加落点贡献
                int nh = nw.h
                    - abs(x   - cx) - abs(y   - cy)
                    - abs(xx  - cx) - abs(yy  - cy)
                    + abs(xxx - cx) + abs(yyy - cy);

                unordered_map<bitset<_>, int>::iterator it = mp.find(t);
                if(it == mp.end() || it->second > ng) {
                    mp[t] = ng;

                    Node nn;
                    nn.st = t;
                    nn.g = ng;
                    nn.h = nh;
                    nn.f = ng + W * nh;
                    nn.fa = idx;
                    nn.from_x = x;
                    nn.from_y = y;
                    nn.mv = op[j];
                    pool.push_back(nn);
                    q.push(make_pair(nn.f, (int)pool.size() - 1));
                }

                t[num(xx,yy)] = t[i] = 1;
                t[num(xxx,yyy)] = 0;
            }
        }
    }
}

int main() {
//		system("checker.exe yyychess4.in 4.out 4.out");
//	return 0;
//	freopen("yyychess4.in","r",stdin);
//	freopen("4.out","w",stdout);
    ios :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    if(!m) {
        for(int i = 1; i <= n; i ++) {
            cin >> s;
            for(int j = 0; j < n; j ++) {
                if(s[j] == 'x') v[i][j+1] = -1;
                if(s[j] == 'y') t[num(i,j+1)] = 1;
            }
        }
    }

    cx = (n + 1) / 2;
    cy = (n + 1) / 2;
    zhong = num(cx, cy);
    total = t.count();

    mp.reserve(1 << 20);
    mp.max_load_factor(0.7);
    mp[t] = 0;

    int h0 = hval(t);
    Node start;
    start.st = t;
    start.g = 0;
    start.h = h0;
    start.f = W * h0;
    start.fa = -1;
    start.from_x = start.from_y = 0;
    start.mv = 0;
    pool.push_back(start);
    q.push(make_pair(start.f, 0));

    Astar();
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int _ = 1010;
int n, m, x, y;
char v[_][_];
struct hhh{
	int x, y;
}a[_ * _];
inline bool bbb(hhh x, hhh y){
	return (x. x ^ y. x ? x. x < y. x : x. y < y. y);
}
int main(){
//	freopen("8.out","w",stdout);
//	freopen("8.in", "r", stdin);
//	freopen("v8.in", "w", stdout);
	ios :: sync_with_stdio(0), cin. tie(0), cout. tie(0);
//	cin >>n >> m;
	for(int i = 1; i <= 495; i += 2){
		cout << i << ' ' << 498 << " D\n";
	}
	for(int i = 995; i >= 501; i -= 2){
		cout <<i << ' ' << 498 << " U\n";
	}
	for(int i = 1; i <= 495; i += 2){
		cout << 498 << ' ' << i << " R\n";
	}
	for(int i = 995; i >= 501; i -= 2){
		cout << 498 << ' ' << i << " L\n";
	}
	cout << "499 499 U\n497 499 L\n497 497 D\n499 497 R\n";
//	for(int i = 1; i <= n; i ++){
//		for(int j = 1; j <= n; j ++){
//			v[i][j] = ' ';
//		}
//	}
//	for(int i = 1; i <= m; i ++){
//		cin >> x >> y;
//		v[x][y] = 'X';
//	}
//	for(int i = 1; i <= n; i ++){
//		for(int j = 1; j <= n; j ++){
//			cout << v[i][j];
//		}cout<<'\n';
//	}
	for(int i = 1; i <= m; i ++){
		cin >> a[i]. x >> a[i]. y;
	}
	sort(a + 1, a + m + 1, bbb);
	for(int i = 1; i <= m; i ++){
		cout << a[i]. x << ' '<< a[i]. y << '\n';
	}
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int _ = 1010;
int n, m, x, y;
char v[_][_];
struct hhh{
	int x, y;
}a[_ * _];
inline bool bbb(hhh x, hhh y){
	return (x. x ^ y. x ? x. x < y. x : x. y < y. y);
}
int main(){
//	freopen("10.in", "r", stdin);
//	freopen("10.out", "w", stdout);
	ios :: sync_with_stdio(0), cin. tie(0), cout. tie(0);
	for(int i = 2; i <= 496; i += 2){
		for(int j = i + 1; j <= 497; j += 2){//->499;
			cout << i << ' ' << j << " R\n";
		}
		for(int j = 996 - i; j >= 500; j -= 2){//498<-;
			cout << i << ' ' << j << " L\n";
		}
	}
	for(int j = 2; j <= 496; j += 2){//脖子左拧;
		for(int i = 995 - j; i >= 499; i -= 2){
			cout << i << ' ' << j << " U\n";
		}
		for(int i = j; i <= 496; i += 2){
			cout << i << ' ' << j << " D\n";
		}
	}
	for(int i = 994; i >= 500; i -= 2){//脖子左拧;
		for(int j = i - 1; j >= 499; j -= 2){
			cout << i << ' ' << j << " L\n";
		}
		for(int j = 996 - i; j <= 496; j += 2){
			cout << i << ' ' << j << " R\n";
		}
	}
	for(int j = 994; j >= 500; j -= 2){//脖子左拧;
		for(int i = 997 - j; i <= 497; i += 2){
			cout << i << ' ' << j << " D\n";
		}
		for(int i = j; i >= 500; i -= 2){
			cout << i << ' ' << j << " U\n";
		}
	}
	for(int i = 1; i <= 495; i += 2){
		cout << i << ' ' << 498 << " D\n";
	}
	for(int i = 995; i >= 501; i -= 2){
		cout << i << ' ' << 498 << " U\n";
	}
	for(int i = 1; i <= 495; i += 2){
		cout << 498 << ' ' << i << " R\n";
	}
	for(int i = 995; i >= 501; i -= 2){
		cout << 498 << ' ' << i << " L\n";
	}
	cout << "499 499 U\n497 499 L\n497 497 D\n499 497 R\n";
	return 0;
}
posted @ 2026-09-24 20:46  养鸡大户肝硬化  阅读(22)  评论(6)    收藏  举报